diff --git a/app/config/roles.php b/app/config/roles.php index fae97895b8..6d55c1cc9d 100644 --- a/app/config/roles.php +++ b/app/config/roles.php @@ -76,6 +76,8 @@ $admins = [ 'topics.read', 'subscribers.write', 'subscribers.read', + 'tokens.read', + 'tokens.write', ]; return [ diff --git a/app/config/scopes.php b/app/config/scopes.php index 3765ab54fa..e123626c63 100644 --- a/app/config/scopes.php +++ b/app/config/scopes.php @@ -130,4 +130,10 @@ return [ // List of publicly visible scopes 'assistant.read' => [ 'description' => 'Access to read the Assistant service', ], + 'tokens.read' => [ + 'description' => 'Access to read your project\'s tokens', + ], + 'tokens.write' => [ + 'description' => 'Access to create, update, and delete your project\'s tokens', + ], ]; diff --git a/app/controllers/general.php b/app/controllers/general.php index b2a07f06f6..faf7748468 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -10,6 +10,7 @@ use Appwrite\Event\Func; use Appwrite\Event\Usage; use Appwrite\Extend\Exception as AppwriteException; use Appwrite\Network\Validator\Origin; +use Appwrite\Platform\Appwrite; use Appwrite\Utopia\Request; use Appwrite\Utopia\Request\Filters\V16 as RequestV16; use Appwrite\Utopia\Request\Filters\V17 as RequestV17; @@ -38,6 +39,7 @@ use Utopia\Logger\Adapter\Sentry; use Utopia\Logger\Log; use Utopia\Logger\Log\User; use Utopia\Logger\Logger; +use Utopia\Platform\Service; use Utopia\System\System; use Utopia\Validator\Hostname; use Utopia\Validator\Text; @@ -1100,3 +1102,6 @@ App::wildcard() foreach (Config::getParam('services', []) as $service) { include_once $service['controller']; } + +$platform = new Appwrite(); +$platform->init(Service::TYPE_HTTP); \ No newline at end of file diff --git a/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/CreateToken.php b/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/CreateToken.php index 3e28b23430..490e4e08ed 100644 --- a/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/CreateToken.php +++ b/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/CreateToken.php @@ -29,7 +29,7 @@ class CreateToken extends Action public function __construct() { - $this->setHttpMethod(Action::HTTP_REQUEST_METHOD_GET) + $this->setHttpMethod(Action::HTTP_REQUEST_METHOD_POST) ->setHttpPath('/v1/tokens') ->desc('Create token') ->groups(['api', 'token']) diff --git a/src/Appwrite/Platform/Modules/Tokens/Services/Http.php b/src/Appwrite/Platform/Modules/Tokens/Services/Http.php index 9df63a0eed..3304933a1f 100644 --- a/src/Appwrite/Platform/Modules/Tokens/Services/Http.php +++ b/src/Appwrite/Platform/Modules/Tokens/Services/Http.php @@ -2,7 +2,12 @@ namespace Appwrite\Platform\Modules\Tokens\Services; +use Appwrite\Platform\Modules\Tokens\Http\Tokens\CreateToken; +use Appwrite\Platform\Modules\Tokens\Http\Tokens\DeleteToken; +use Appwrite\Platform\Modules\Tokens\Http\Tokens\GetToken; +use Appwrite\Platform\Modules\Tokens\Http\Tokens\GetTokenJWT; use Appwrite\Platform\Modules\Tokens\Http\Tokens\ListTokens; +use Appwrite\Platform\Modules\Tokens\Http\Tokens\UpdateToken; use Utopia\Platform\Service; class Http extends Service @@ -10,6 +15,14 @@ class Http extends Service public function __construct() { $this->type = Service::TYPE_HTTP; - $this->addAction(ListTokens::getName(), new ListTokens()); + $this + ->addAction(CreateToken::getName(), new CreateToken()) + ->addAction(DeleteToken::getName(), new DeleteToken()) + ->addAction(GetToken::getName(), new GetToken()) + ->addAction(GetTokenJWT::getName(), new GetTokenJWT()) + ->addAction(ListTokens::getName(), new ListTokens()) + ->addAction(UpdateToken::getName(), new UpdateToken()) + ; + } } diff --git a/tests/e2e/Services/Tokens/TokensBase.php b/tests/e2e/Services/Tokens/TokensBase.php index 5c57677783..ae93d6164a 100644 --- a/tests/e2e/Services/Tokens/TokensBase.php +++ b/tests/e2e/Services/Tokens/TokensBase.php @@ -11,86 +11,6 @@ use Utopia\Database\Helpers\Role; trait TokensBase { - /** - * @group fileTokens - */ - public function testCreateFileToken(): array - { - - $bucket = $this->client->call(Client::METHOD_POST, '/storage/buckets', [ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-key' => $this->getProject()['apiKey'], - ], [ - 'bucketId' => ID::unique(), - 'name' => 'Test Bucket', - 'fileSecurity' => true, - 'maximumFileSize' => 2000000, //2MB - 'allowedFileExtensions' => ['jpg', 'png', 'jfif'], - 'permissions' => [ - Permission::read(Role::any()), - Permission::create(Role::any()), - Permission::update(Role::any()), - Permission::delete(Role::any()), - ], - ]); - $this->assertEquals(201, $bucket['headers']['status-code']); - $this->assertNotEmpty($bucket['body']['$id']); - - $bucketId = $bucket['body']['$id']; - - $file = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucketId . '/files', array_merge([ - 'content-type' => 'multipart/form-data', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), [ - 'fileId' => ID::unique(), - 'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'logo.png'), - 'permissions' => [ - Permission::read(Role::any()), - Permission::update(Role::any()), - Permission::delete(Role::any()), - ], - ]); - $this->assertEquals(201, $file['headers']['status-code']); - $this->assertNotEmpty($file['body']['$id']); - - $fileId = $file['body']['$id']; - - $res = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucketId . '/files/' . $fileId . '/tokens', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), []); - - $this->assertEquals(201, $res['headers']['status-code']); - $this->assertEquals('files', $res['body']['resourceType']); - - $data = []; - $data['fileId'] = $fileId; - $data['bucketId'] = $bucketId; - $data['tokenId'] = $res['body']['$id']; - return $data; - } - - /** - * @group fileTokens - * @depends testCreateFileToken - */ - public function testUpdateFileToken(array $data): array - { - $bucketId = $data['bucketId']; - $fileId = $data['fileId']; - $tokenId = $data['tokenId']; - - $expiry = DateTime::now(); - $res = $this->client->call(Client::METHOD_PUT, '/storage/buckets/'. $bucketId . '/files/'. $fileId . '/tokens/' . $tokenId, array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), [ - 'expire' => $expiry, - ]); - - $this->assertEquals($expiry, $res['body']['expire']); - return $data; - } + } diff --git a/tests/e2e/Services/Tokens/TokensConsoleClientTest.php b/tests/e2e/Services/Tokens/TokensConsoleClientTest.php new file mode 100644 index 0000000000..1ce72335dd --- /dev/null +++ b/tests/e2e/Services/Tokens/TokensConsoleClientTest.php @@ -0,0 +1,14 @@ +client->call(Client::METHOD_POST, '/storage/buckets', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'bucketId' => ID::unique(), + 'name' => 'Test Bucket', + 'fileSecurity' => true, + 'maximumFileSize' => 2000000, //2MB + 'allowedFileExtensions' => ['jpg', 'png', 'jfif'], + 'permissions' => [ + Permission::read(Role::any()), + Permission::create(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ], + ]); + $this->assertEquals(201, $bucket['headers']['status-code']); + $this->assertNotEmpty($bucket['body']['$id']); + + $bucketId = $bucket['body']['$id']; + + $file = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucketId . '/files', array_merge([ + 'content-type' => 'multipart/form-data', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'fileId' => ID::unique(), + 'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'logo.png'), + 'permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ], + ]); + $this->assertEquals(201, $file['headers']['status-code']); + $this->assertNotEmpty($file['body']['$id']); + + $fileId = $file['body']['$id']; + + $res = $this->client->call(Client::METHOD_POST, '/tokens', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], $this->getHeaders()), [ + 'resourceType' => 'files', + 'resourceId' => $bucketId . ':' . $fileId + ]); + + $this->assertEquals(201, $res['headers']['status-code']); + $this->assertEquals('files', $res['body']['resourceType']); + + $data = []; + $data['fileId'] = $fileId; + $data['bucketId'] = $bucketId; + $data['tokenId'] = $res['body']['$id']; + return $data; + } + + /** + * @depends testCreateToken + */ + public function testUpdateToken(array $data): array + { + $bucketId = $data['bucketId']; + $fileId = $data['fileId']; + $tokenId = $data['tokenId']; + + $expiry = DateTime::now(); + $res = $this->client->call(Client::METHOD_PUT, '/tokens/' . $tokenId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'expire' => $expiry, + ]); + + $this->assertEquals($expiry, $res['body']['expire']); + return $data; + } + + /** + * @depends testUpdateToken + */ + public function testDeleteToken(array $data): array + { + + return $data; + } +}