Require PHP 8.0+, Create Structure of Grant API

This commit is contained in:
root 2022-11-28 21:05:24 +01:00
parent 8ca36409fd
commit e0d37e4dca
13 changed files with 139 additions and 109 deletions

View File

@ -5,7 +5,7 @@
"minimum-stability": "dev",
"version": "1.0",
"require": {
"php": ">=7.4",
"php": ">=8.0",
"gac/routing": "dev-main",
"twig/twig": "^3.0",
"ext-json": "*",

8
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "95568d064a020cc68f2baa6c387308a5",
"content-hash": "d5fef76f306691ffbfa0e708d2513deb",
"packages": [
{
"name": "161sh/seriousjson",
@ -12,11 +12,11 @@
"dist": {
"type": "path",
"url": "../SeriousJSON",
"reference": "d0e9b7aaaf0d57ca6a4d0650389c9e1a1fbda125"
"reference": "794e1747481f0ce0ae643aa0695dfa68b1ed0afd"
},
"require": {
"ext-json": "*",
"php": ">=7.4"
"php": ">=8.0"
},
"require-dev": {
"phpunit/phpunit": "^8"
@ -1786,7 +1786,7 @@
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
"php": ">=7.4",
"php": ">=8.0",
"ext-json": "*"
},
"platform-dev": [],

View File

@ -5,12 +5,9 @@ require_once __DIR__ . '/vendor/autoload.php';
// First Party
use Tor\Tor;
use Tor\Api\Api;
use Tor\Api;
use Tor\Data\Ticket;
use Tor\Data\Grant;
use Tor\Data\Service;
use Tor\Data\User;
use SeriousJSON\JsonDatabase;
// Third Party Routing Library
use Gac\Routing\Exceptions\CallbackNotFound;
@ -23,42 +20,19 @@ $routes = new Routes();
// Initialize Twig
// TODO: Only do this if needed
$loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/templates');
$loader = new \Twig\Loader\FilesystemLoader(__DIR__ . DIRECTORY_SEPARATOR . 'templates');
$twig = new \Twig\Environment($loader, [
'cache' => __DIR__. '/compilation_cache',
'cache' => __DIR__ . DIRECTORY_SEPARATOR . 'compilation_cache',
]);
$data = new JsonDatabase(__DIR__ . DIRECTORY_SEPARATOR . 'data');
$tor = new Tor();
$api = new Api();
$service = new Service();
$service->id = "minetest";
$service->token = "45678-hsjsndjs-272892-shgdzusjd-6788";
$user = new User();
$user->id = "bananafish";
$user->serial = '0C:87:64:78';
$user->cert = "Yee Haw";
$grant = new Grant();
$grant->id = uniqid();
$grant->service = $service;
$ticket = new Ticket();
$ticket->id = uniqid();
$ticket->start = 1471111;
$ticket->end = 1474567;
$ticket->user = $user;
$ticket->grant = $grant;
echo(microtime(true));
echo($ticket->Serialize(true));
echo(var_dump(Ticket::Deserialize($ticket->Serialize(true), true)));
echo(Ticket::Deserialize($ticket->Serialize(true), true))->Serialize(true);
echo(microtime(true));
try {
// Initialize Database
$data->init();
// Initialize Tor in order to register routes
$tor->init();

View File

@ -1,6 +1,8 @@
<?php
namespace Tor\Api;
namespace Tor;
use Tor\Controller\GrantApi;
use Gac\Routing\Request;
@ -75,47 +77,32 @@ class Api {
->send([ 'result' => 'ok' ]);
});
// No action specified
$routes->add('/api/v1/grant', [ GrantApi::class, 'response' ]);
// Create Ticket Granting Ticket
$routes->add('/api/v1/grant/create', function (Request $request) {
$request->status(200, 'OK')
->send([ 'result' => 'ok' ]);
});
$routes->add('/api/v1/grant/create', [ GrantApi::class, 'create' ]);
// Invalidate Ticket Granting Ticket
$routes->add('/api/v1/grant/destroy', function (Request $request) {
$request->status(200, 'OK')
->send([ 'result' => 'ok' ]);
});
$routes->add('/api/v1/grant/destroy', [ GrantApi::class, 'destroy' ]);
// Poll Authorization Status of Grant
$routes->add('/api/v1/grant/status', function (Request $request) {
$request->status(200, 'OK')
->send([ 'result' => 'ok' ]);
});
$routes->add('/api/v1/grant/status', [ GrantApi::class, 'status' ]);
// Fetch ExtraData for Grant
$routes->add('/api/v1/grant/extra', function (Request $request) {
$request->status(200, 'OK')
->send([ 'result' => 'ok' ]);
});
// $routes->add('/api/v1/grant/extra', function (Request $request) {
// $request->status(200, 'OK')
// ->send([ 'result' => 'ok' ]);
//});
// Internal: List all active Grants
$routes->add('/api/v1/grants/list', function (Request $request) {
$request->status(200, 'OK')
->send([ 'result' => 'ok' ]);
});
$routes->add('/api/v1/grant/list', [ GrantApi::class, 'list' ]);
// Internal: Approve of Grant with specified subset of extradata
$routes->add('/api/v1/grants/approve', function (Request $request) {
$request->status(200, 'OK')
->send([ 'result' => 'ok' ]);
});
$routes->add('/api/v1/grant/approve', [ GrantApi::class, 'approve' ]);
// Internal: Reject Grant
$routes->add('/api/v1/grants/reject', function (Request $request) {
$request->status(200, 'OK')
->send([ 'result' => 'ok' ]);
});
$routes->add('/api/v1/grant/reject', [ GrantApi::class, 'reject' ]);
// Fetch ID of Session Ticket by using authorized Grant ID
// Useful when polling manually, not neccessary when using callback

View File

@ -0,0 +1,14 @@
<?php
namespace Tor\Controller;
use Gac\Routing\Request;
abstract class BaseApi {
function __construct() {
}
// Default response if no action is defined
abstract function response(\Gac\Routing\Request $request);
}

View File

@ -0,0 +1,76 @@
<?php
namespace Tor\Controller;
use Tor\Data\Grant;
use Tor\Data\Service;
use Gac\Routing\Request;
class GrantApi extends BaseApi {
// Creates a new grant and sends back grant information
function create(\Gac\Routing\Request $request) {
global $data;
$uniqid = uniqid();
// Make sure uniqid isn't already used
while ($data->exists(Grant::class , $uniqid))
$uniqid = uniqid();
$grant = new Grant();
$grant->id = $uniqid;
$grant->service = new Service();
$data->save($grant);
$request->status(201, 'Created')
->send([ 'grant' => $grant->id, 'result' => 'created' ]);
}
// Destroy a grant (Only works if not authorized yet)
function destroy(\Gac\Routing\Request $request) {
global $data;
// TODO: Identifier
$identifier = 'cats';
$exists = $data->exists(Grant::class, $identifier);
if (!$exists)
$request->status(404, 'Not Found')->send(["error" => ["message" => "not found"]]);
else
$data->delete(Grant::class, $identifier);
}
// Internal: Approve Grant (Only works if not authorized yet)
function approve(\Gac\Routing\Request $request) {
$request->status(200, 'OK')
->send([ 'result' => 'ok' ]);
}
// Internal: Reject Grant (Only works if not authorized yet)
function reject(\Gac\Routing\Request $request) {
$request->status(200, 'OK')
->send([ 'result' => 'ok' ]);
}
// Fetch status of a grant
function status(\Gac\Routing\Request $request) {
$request->status(200, 'OK')
->send([ 'result' => 'ok' ]);
}
// Internal: List all grants
function list(\Gac\Routing\Request $request) {
$request->status(200, 'OK')
->send([ 'result' => 'ok' ]);
}
// Default response if no action is defined
function response(\Gac\Routing\Request $request) {
$request->status(200, 'OK')
->send([ 'result' => 'ok' ]);
}
}

View File

@ -1,7 +1,7 @@
<?php
namespace Tor\Controller;
class Message extends BasePage {
class MessagePage extends BasePage {
function render(\Gac\Routing\Request $request)
{

View File

@ -2,12 +2,27 @@
namespace Tor\Data;
abstract class BaseEntity extends \SeriousJSON\JsonSerializable {
abstract class BaseEntity extends \SeriousJSON\JsonSerializable implements \SeriousJSON\IJsonIdentifiable {
// Primary Identifier
public string $id;
public ?string $id = null;
public function flatIdentifier()
{
return $this->id;
}
public function setIdentifier($identifier)
{
// Ignore attemts to set an empty identifier
if (!isset($identifier)
|| (is_string($identifier)
&& empty(trim($identifier))))
return;
// DO throw an exception if the identifier is not a string
if (!is_string($identifier))
throw new Exception('Invalid value was provided for identifier. Can only be of type string.');
$this->id = $identifier;
}
}

View File

@ -5,13 +5,4 @@ namespace Tor\Data;
class Grant extends BaseEntity {
public Service $service;
public int $create;
public function jsonSerialize() {
if(get_class($this) == get_called_class()) {
return $this;
}
else {
return get_class($this) . ' = ' . get_called_class();
}
}
}

View File

@ -4,13 +4,4 @@ namespace Tor\Data;
class Service extends BaseEntity {
public string $token;
public function jsonSerialize() {
if(get_class($this) == get_called_class()) {
return $this;
}
else {
return get_class($this) . ' = ' . get_called_class();
}
}
}

View File

@ -7,13 +7,4 @@ class Ticket extends BaseEntity {
public User $user;
public int $start;
public int $end;
public function jsonSerialize() {
if(get_class($this) == get_called_class()) {
return $this;
}
else {
return get_class($this) . ' = ' . get_called_class();
}
}
}

View File

@ -5,13 +5,4 @@ namespace Tor\Data;
class User extends BaseEntity {
public string $serial;
public string $cert;
public function jsonSerialize() {
if(get_class($this) == get_called_class()) {
return $this;
}
else {
return get_class($this) . ' = ' . get_called_class();
}
}
}

View File

@ -3,7 +3,7 @@
namespace Tor;
use Gac\Routing\Request;
use Tor\Controller\Message;
use Tor\Controller\MessagePage;
// Implements the Dispatcher for Tor
class Tor {
@ -11,7 +11,7 @@ class Tor {
public function init() {
global $routes;
$routes->add('/', [ Message::class, 'render' ]);
$routes->add('/', [ MessagePage::class, 'render' ]);
// Generate new User Certificate
$routes->add('/generate', function (Request $request) {