diff --git a/composer.json b/composer.json index 476a512..6a5c274 100644 --- a/composer.json +++ b/composer.json @@ -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": "*", diff --git a/composer.lock b/composer.lock index 9aef215..34296c1 100644 --- a/composer.lock +++ b/composer.lock @@ -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": [], diff --git a/index.php b/index.php index 514e639..92e014c 100644 --- a/index.php +++ b/index.php @@ -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(); diff --git a/src/Tor/Api/Api.php b/src/Tor/Api.php similarity index 78% rename from src/Tor/Api/Api.php rename to src/Tor/Api.php index 819430b..03d845b 100644 --- a/src/Tor/Api/Api.php +++ b/src/Tor/Api.php @@ -1,6 +1,8 @@ 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 diff --git a/src/Tor/Controller/BaseApi.php b/src/Tor/Controller/BaseApi.php new file mode 100644 index 0000000..acbc5c8 --- /dev/null +++ b/src/Tor/Controller/BaseApi.php @@ -0,0 +1,14 @@ +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' ]); + } + +} \ No newline at end of file diff --git a/src/Tor/Controller/Message.php b/src/Tor/Controller/MessagePage.php similarity index 86% rename from src/Tor/Controller/Message.php rename to src/Tor/Controller/MessagePage.php index b31dd19..2852f77 100644 --- a/src/Tor/Controller/Message.php +++ b/src/Tor/Controller/MessagePage.php @@ -1,7 +1,7 @@ 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; + } } diff --git a/src/Tor/Data/Grant.php b/src/Tor/Data/Grant.php index 4fa12b2..01ee8f6 100644 --- a/src/Tor/Data/Grant.php +++ b/src/Tor/Data/Grant.php @@ -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(); - } - } } diff --git a/src/Tor/Data/Service.php b/src/Tor/Data/Service.php index b4c7eb2..800c1b8 100644 --- a/src/Tor/Data/Service.php +++ b/src/Tor/Data/Service.php @@ -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(); - } - } } diff --git a/src/Tor/Data/Ticket.php b/src/Tor/Data/Ticket.php index 4ed0d8f..1e10a93 100644 --- a/src/Tor/Data/Ticket.php +++ b/src/Tor/Data/Ticket.php @@ -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(); - } - } } diff --git a/src/Tor/Data/User.php b/src/Tor/Data/User.php index 5b04db3..c9e6aa4 100644 --- a/src/Tor/Data/User.php +++ b/src/Tor/Data/User.php @@ -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(); - } - } } diff --git a/src/Tor/Tor.php b/src/Tor/Tor.php index 06efd73..76b051e 100644 --- a/src/Tor/Tor.php +++ b/src/Tor/Tor.php @@ -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) {