mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Merge pull request #13621 from appwrite/feat/organization-project-keys
Move project API key management under the Organization API
This commit is contained in:
32 files changed
+2154
-1047
No files matched your search
@@ -221,6 +221,7 @@ jobs:
|
||||
{ name: 'Notifications', runner: runner4, functional: true, processes: null },
|
||||
{ name: 'Migrations', runner: runner4, functional: true, processes: 1, documentsdb: true },
|
||||
{ name: 'Project', runner: runner4, functional: true, processes: null },
|
||||
{ name: 'Organization', runner: runner4, functional: true, processes: null },
|
||||
{ name: 'Presences', runner: runner4, functional: true, processes: null },
|
||||
];
|
||||
core.setOutput('services', JSON.stringify(services));
|
||||
|
||||
@@ -13,7 +13,8 @@ return [
|
||||
'project.createWindowsPlatform' => true,
|
||||
'project.createLinuxPlatform' => true,
|
||||
// Connect — API key
|
||||
'project.createKey' => true,
|
||||
'project.createKey' => true, // TODO: Remove with the project-scoped keys endpoints
|
||||
'organization.createProjectKey' => true,
|
||||
|
||||
// Auth
|
||||
'users.create' => true,
|
||||
|
||||
@@ -92,6 +92,8 @@ $admins = [
|
||||
'projects.write',
|
||||
'keys.read',
|
||||
'keys.write',
|
||||
'organization.projects.keys.read',
|
||||
'organization.projects.keys.write',
|
||||
'devKeys.read',
|
||||
'devKeys.write',
|
||||
'webhooks.read',
|
||||
|
||||
@@ -12,6 +12,15 @@ return [
|
||||
"Access to create, update, and delete organization projects",
|
||||
"category" => "Projects",
|
||||
],
|
||||
"organization.projects.keys.read" => [
|
||||
"description" => 'Access to read organization projects\' API keys',
|
||||
"category" => "Projects",
|
||||
],
|
||||
"organization.projects.keys.write" => [
|
||||
"description" =>
|
||||
"Access to create, update, and delete organization projects' API keys",
|
||||
"category" => "Projects",
|
||||
],
|
||||
"devKeys.read" => [
|
||||
"description" => 'Access to read project\'s development keys',
|
||||
"category" => "Other",
|
||||
|
||||
@@ -17,6 +17,8 @@ return [
|
||||
'description' => 'Access to read project usage metrics',
|
||||
'category' => 'Project',
|
||||
],
|
||||
// TODO: Remove keys.read and keys.write from project key scopes once the Console, CLI and SDKs
|
||||
// manage project keys through the Organization API (organization.projects.keys.*)
|
||||
"keys.read" => [
|
||||
"description" =>
|
||||
"Access to read project\'s keys",
|
||||
|
||||
@@ -1190,7 +1190,7 @@ Http::shutdown()
|
||||
* cannot suppress RequestCompleted or usage metrics on the same request.
|
||||
*/
|
||||
$statusCode = $response->getStatusCode();
|
||||
if ($statusCode < 200 || $statusCode >= 300 || $project->getId() === 'console') {
|
||||
if ($statusCode < 200 || $statusCode >= 300) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1228,6 +1228,19 @@ Http::shutdown()
|
||||
return;
|
||||
}
|
||||
|
||||
// Organization routes act on the project named in the path, not on the console project.
|
||||
if ($project->getId() === 'console') {
|
||||
$projectId = (string) ($route->getParamsValues()['projectId'] ?? '');
|
||||
if ($projectId === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $projectId));
|
||||
if ($project->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$byMethod = $project->getAttribute('onboarding', []);
|
||||
$status = \is_array($byMethod) ? ($byMethod[$method]['status'] ?? null) : null;
|
||||
if ($status === ONBOARDING_STATUS_COMPLETED || $status === ONBOARDING_STATUS_SKIPPED) {
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
<directory>./tests/e2e/Services/Migrations</directory>
|
||||
<directory>./tests/e2e/Services/Notifications</directory>
|
||||
<directory>./tests/e2e/Services/Project</directory>
|
||||
<directory>./tests/e2e/Services/Organization</directory>
|
||||
<directory>./tests/e2e/Services/Advisor</directory>
|
||||
<directory>./tests/e2e/Services/Usage</directory>
|
||||
<file>./tests/e2e/Services/Functions/FunctionsBase.php</file>
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Platform\Modules\Organization\Http\Projects\Keys;
|
||||
|
||||
use Appwrite\Auth\Key;
|
||||
use Appwrite\Extend\Exception;
|
||||
use Appwrite\Platform\Action as AppwriteAction;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
|
||||
class Action extends AppwriteAction
|
||||
{
|
||||
/**
|
||||
* Resolve the project whose keys are managed and make sure it belongs to the organization.
|
||||
*
|
||||
* A key bound to one project must not reach the keys of a sibling project in the same organization.
|
||||
*/
|
||||
protected function getProject(string $projectId, Document $team, Database $dbForPlatform, ?Key $apiKey): Document
|
||||
{
|
||||
$project = $dbForPlatform->getDocument('projects', $projectId);
|
||||
|
||||
if ($project->isEmpty() || $project->getId() === 'console') {
|
||||
throw new Exception(Exception::PROJECT_NOT_FOUND);
|
||||
}
|
||||
|
||||
if ($project->getAttribute('teamInternalId') !== $team->getSequence()) {
|
||||
throw new Exception(Exception::PROJECT_NOT_FOUND);
|
||||
}
|
||||
|
||||
if ($apiKey !== null && !$apiKey->isProjectCheckDisabled() && $apiKey->getProjectId() !== '' && $apiKey->getProjectId() !== $project->getId()) {
|
||||
throw new Exception(Exception::USER_UNAUTHORIZED);
|
||||
}
|
||||
|
||||
return $project;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Platform\Modules\Organization\Http\Projects\Keys;
|
||||
|
||||
use Appwrite\Auth\Key;
|
||||
use Appwrite\Event\Context\Audit as AuditContext;
|
||||
use Appwrite\Event\Event as QueueEvent;
|
||||
use Appwrite\Extend\Exception;
|
||||
use Appwrite\SDK\AuthType;
|
||||
use Appwrite\SDK\Method;
|
||||
use Appwrite\SDK\Response as SDKResponse;
|
||||
use Appwrite\Utopia\Database\Validator\CustomId;
|
||||
use Appwrite\Utopia\Response;
|
||||
use Utopia\Config\Config;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Exception\Duplicate as DuplicateException;
|
||||
use Utopia\Database\Helpers\ID;
|
||||
use Utopia\Database\Validator\Authorization;
|
||||
use Utopia\Database\Validator\Datetime;
|
||||
use Utopia\Database\Validator\UID;
|
||||
use Utopia\Platform\Enum;
|
||||
use Utopia\Platform\Scope\HTTP;
|
||||
use Utopia\Validator\ArrayList;
|
||||
use Utopia\Validator\Nullable;
|
||||
use Utopia\Validator\Text;
|
||||
use Utopia\Validator\WhiteList;
|
||||
|
||||
class Create extends Action
|
||||
{
|
||||
use HTTP;
|
||||
|
||||
public static function getName()
|
||||
{
|
||||
return 'createProjectKey';
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this
|
||||
->setHttpMethod(Action::HTTP_REQUEST_METHOD_POST)
|
||||
->setHttpPath('/v1/organization/projects/:projectId/keys')
|
||||
->desc('Create project key')
|
||||
->groups(['api', 'organization'])
|
||||
->label('scope', ['organization.projects.keys.write', 'keys.write'])
|
||||
->label('event', 'keys.[keyId].create')
|
||||
->label('audits.event', 'project.key.create')
|
||||
->label('audits.resource', 'project.key/{response.$id}')
|
||||
->label('sdk', new Method(
|
||||
namespace: 'organization',
|
||||
group: 'keys',
|
||||
name: 'createProjectKey',
|
||||
description: <<<EOT
|
||||
Create a new API key for a project in your organization. It's recommended to have multiple API keys with strict scopes for separate functions within your project.
|
||||
|
||||
You can also create an ephemeral API key if you need a short-lived key instead.
|
||||
EOT,
|
||||
auth: [AuthType::ADMIN, AuthType::ORGANIZATION],
|
||||
responses: [
|
||||
new SDKResponse(
|
||||
code: Response::STATUS_CODE_CREATED,
|
||||
model: Response::MODEL_KEY,
|
||||
)
|
||||
],
|
||||
))
|
||||
->param('projectId', '', new UID(), 'Project unique ID.')
|
||||
->param('keyId', '', fn (Database $dbForPlatform) => new CustomId(false, $dbForPlatform->getAdapter()->getMaxUIDLength()), 'Key ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.', false, ['dbForPlatform'])
|
||||
->param('name', null, new Text(128), 'Key name. Max length: 128 chars.')
|
||||
->param('scopes', [], new ArrayList(new WhiteList(array_keys(Config::getParam('projectScopes')), true), APP_LIMIT_ARRAY_SCOPES_SIZE), 'Key scopes list. Maximum of ' . APP_LIMIT_ARRAY_SCOPES_SIZE . ' scopes are allowed.', optional: false, enum: new Enum(name: 'ProjectKeyScopes'))
|
||||
->param('expire', null, new Nullable(new Datetime()), 'Expiration time in [ISO 8601](https://www.iso.org/iso-8601-date-and-time-format.html) format. Use null for unlimited expiration.', true)
|
||||
->inject('response')
|
||||
->inject('queueForEvents')
|
||||
->inject('dbForPlatform')
|
||||
->inject('team')
|
||||
->inject('authorization')
|
||||
->inject('apiKey')
|
||||
->inject('auditContext')
|
||||
->callback($this->action(...));
|
||||
}
|
||||
|
||||
public function action(
|
||||
string $projectId,
|
||||
string $keyId,
|
||||
string $name,
|
||||
array $scopes,
|
||||
?string $expire,
|
||||
Response $response,
|
||||
QueueEvent $queueForEvents,
|
||||
Database $dbForPlatform,
|
||||
Document $team,
|
||||
Authorization $authorization,
|
||||
?Key $apiKey,
|
||||
AuditContext $auditContext,
|
||||
) {
|
||||
// Dynamic supported for backwards compatibility
|
||||
if ($apiKey !== null && \in_array($apiKey->getType(), [API_KEY_STANDARD, API_KEY_EPHEMERAL, 'dynamic'], true)) {
|
||||
throw new Exception(Exception::KEY_CREATION_DENIED);
|
||||
}
|
||||
|
||||
$project = $this->getProject($projectId, $team, $dbForPlatform, $apiKey);
|
||||
|
||||
// The request may run through the console project; events and audits belong to the resolved project
|
||||
$queueForEvents->setProject($project);
|
||||
$auditContext->project = $project;
|
||||
$keyId = ($keyId == 'unique()') ? ID::unique() : $keyId;
|
||||
|
||||
$key = new Document([
|
||||
'$id' => $keyId,
|
||||
'$permissions' => [],
|
||||
'resourceInternalId' => $project->getSequence(),
|
||||
'resourceId' => $project->getId(),
|
||||
'resourceType' => 'projects',
|
||||
'name' => $name,
|
||||
'scopes' => $scopes,
|
||||
'expire' => $expire,
|
||||
'sdks' => [],
|
||||
'accessedAt' => null,
|
||||
'secret' => API_KEY_STANDARD . '_' . \bin2hex(\random_bytes(128)),
|
||||
]);
|
||||
|
||||
try {
|
||||
$key = $authorization->skip(fn () => $dbForPlatform->createDocument('keys', $key));
|
||||
} catch (DuplicateException) {
|
||||
throw new Exception(Exception::KEY_ALREADY_EXISTS);
|
||||
}
|
||||
|
||||
$authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId()));
|
||||
|
||||
$queueForEvents->setParam('keyId', $key->getId());
|
||||
|
||||
$response
|
||||
->setStatusCode(Response::STATUS_CODE_CREATED)
|
||||
->dynamic($key, Response::MODEL_KEY);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Platform\Modules\Organization\Http\Projects\Keys;
|
||||
|
||||
use Appwrite\Auth\Key;
|
||||
use Appwrite\Event\Context\Audit as AuditContext;
|
||||
use Appwrite\Event\Event;
|
||||
use Appwrite\Extend\Exception;
|
||||
use Appwrite\SDK\AuthType;
|
||||
use Appwrite\SDK\ContentType;
|
||||
use Appwrite\SDK\Method;
|
||||
use Appwrite\SDK\Response as SDKResponse;
|
||||
use Appwrite\Utopia\Response;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Validator\Authorization;
|
||||
use Utopia\Database\Validator\UID;
|
||||
use Utopia\Platform\Scope\HTTP;
|
||||
|
||||
class Delete extends Action
|
||||
{
|
||||
use HTTP;
|
||||
|
||||
public static function getName()
|
||||
{
|
||||
return 'deleteProjectKey';
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this
|
||||
->setHttpMethod(Action::HTTP_REQUEST_METHOD_DELETE)
|
||||
->setHttpPath('/v1/organization/projects/:projectId/keys/:keyId')
|
||||
->desc('Delete project key')
|
||||
->groups(['api', 'organization'])
|
||||
->label('scope', ['organization.projects.keys.write', 'keys.write'])
|
||||
->label('event', 'keys.[keyId].delete')
|
||||
->label('audits.event', 'project.key.delete')
|
||||
->label('audits.resource', 'project.key/{request.keyId}')
|
||||
->label('sdk', new Method(
|
||||
namespace: 'organization',
|
||||
group: 'keys',
|
||||
name: 'deleteProjectKey',
|
||||
description: <<<EOT
|
||||
Delete a project key by its unique ID. Once deleted, the key can no longer be used to authenticate API calls.
|
||||
EOT,
|
||||
auth: [AuthType::ADMIN, AuthType::KEY, AuthType::ORGANIZATION],
|
||||
responses: [
|
||||
new SDKResponse(
|
||||
code: Response::STATUS_CODE_NOCONTENT,
|
||||
model: Response::MODEL_NONE,
|
||||
)
|
||||
],
|
||||
contentType: ContentType::NONE
|
||||
))
|
||||
->param('projectId', '', new UID(), 'Project unique ID.')
|
||||
->param('keyId', '', fn (Database $dbForPlatform) => new UID($dbForPlatform->getAdapter()->getMaxUIDLength()), 'Key ID.', false, ['dbForPlatform'])
|
||||
->inject('response')
|
||||
->inject('dbForPlatform')
|
||||
->inject('queueForEvents')
|
||||
->inject('team')
|
||||
->inject('authorization')
|
||||
->inject('apiKey')
|
||||
->inject('auditContext')
|
||||
->callback($this->action(...));
|
||||
}
|
||||
|
||||
public function action(
|
||||
string $projectId,
|
||||
string $keyId,
|
||||
Response $response,
|
||||
Database $dbForPlatform,
|
||||
Event $queueForEvents,
|
||||
Document $team,
|
||||
Authorization $authorization,
|
||||
?Key $apiKey,
|
||||
AuditContext $auditContext,
|
||||
) {
|
||||
$project = $this->getProject($projectId, $team, $dbForPlatform, $apiKey);
|
||||
|
||||
// The request may run through the console project; events and audits belong to the resolved project
|
||||
$queueForEvents->setProject($project);
|
||||
$auditContext->project = $project;
|
||||
$key = $authorization->skip(fn () => $dbForPlatform->getDocument('keys', $keyId));
|
||||
|
||||
if ($key->isEmpty() || $key->getAttribute('resourceType', '') !== 'projects' || $key->getAttribute('resourceInternalId', '') !== $project->getSequence()) {
|
||||
throw new Exception(Exception::KEY_NOT_FOUND);
|
||||
}
|
||||
|
||||
if (!$authorization->skip(fn () => $dbForPlatform->deleteDocument('keys', $key->getId()))) {
|
||||
throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to remove document from DB');
|
||||
}
|
||||
|
||||
$authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId()));
|
||||
|
||||
$queueForEvents->setParam('keyId', $key->getId());
|
||||
|
||||
$response->noContent();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Platform\Modules\Organization\Http\Projects\Keys\Ephemeral;
|
||||
|
||||
use Ahc\Jwt\JWT;
|
||||
use Appwrite\Auth\Key;
|
||||
use Appwrite\Event\Context\Audit as AuditContext;
|
||||
use Appwrite\Event\Event as QueueEvent;
|
||||
use Appwrite\Platform\Modules\Organization\Http\Projects\Keys\Action;
|
||||
use Appwrite\SDK\AuthType;
|
||||
use Appwrite\SDK\Method;
|
||||
use Appwrite\SDK\Response as SDKResponse;
|
||||
use Appwrite\Utopia\Response;
|
||||
use Utopia\Config\Config;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\DateTime as DatabaseDateTime;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Helpers\ID;
|
||||
use Utopia\Database\Validator\UID;
|
||||
use Utopia\Platform\Enum;
|
||||
use Utopia\Platform\Scope\HTTP;
|
||||
use Utopia\System\System;
|
||||
use Utopia\Validator\ArrayList;
|
||||
use Utopia\Validator\Range;
|
||||
use Utopia\Validator\WhiteList;
|
||||
|
||||
class Create extends Action
|
||||
{
|
||||
use HTTP;
|
||||
|
||||
public static function getName()
|
||||
{
|
||||
return 'createEphemeralProjectKey';
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this
|
||||
->setHttpMethod(Action::HTTP_REQUEST_METHOD_POST)
|
||||
->setHttpPath('/v1/organization/projects/:projectId/keys/ephemeral')
|
||||
->desc('Create ephemeral project key')
|
||||
->groups(['api', 'organization'])
|
||||
->label('scope', ['organization.projects.keys.write', 'keys.write'])
|
||||
->label('event', 'keys.[keyId].create')
|
||||
->label('audits.event', 'project.key.create')
|
||||
->label('audits.resource', 'project.key/{response.$id}')
|
||||
->label('sdk', new Method(
|
||||
namespace: 'organization',
|
||||
group: 'keys',
|
||||
name: 'createEphemeralProjectKey',
|
||||
description: <<<EOT
|
||||
Create a new ephemeral API key for a project in your organization. It's recommended to have multiple API keys with strict scopes for separate functions within your project.
|
||||
|
||||
You can also create a standard API key if you need a longer-lived key instead.
|
||||
EOT,
|
||||
auth: [AuthType::ADMIN, AuthType::KEY, AuthType::ORGANIZATION],
|
||||
responses: [
|
||||
new SDKResponse(
|
||||
code: Response::STATUS_CODE_CREATED,
|
||||
model: Response::MODEL_EPHEMERAL_KEY,
|
||||
)
|
||||
],
|
||||
))
|
||||
->param('projectId', '', new UID(), 'Project unique ID.')
|
||||
->param('scopes', [], new ArrayList(new WhiteList(array_keys(Config::getParam('projectScopes')), true), APP_LIMIT_ARRAY_SCOPES_SIZE), 'Key scopes list. Maximum of ' . APP_LIMIT_ARRAY_SCOPES_SIZE . ' scopes are allowed.', optional: false, enum: new Enum(name: 'ProjectKeyScopes'))
|
||||
->param('duration', null, new Range(1, 3600), 'Time in seconds before ephemeral key expires. Maximum duration is 3600 seconds.', optional: false, example: 600)
|
||||
->inject('response')
|
||||
->inject('queueForEvents')
|
||||
->inject('dbForPlatform')
|
||||
->inject('team')
|
||||
->inject('apiKey')
|
||||
->inject('auditContext')
|
||||
->callback($this->action(...));
|
||||
}
|
||||
|
||||
public function action(
|
||||
string $projectId,
|
||||
array $scopes,
|
||||
int $duration,
|
||||
Response $response,
|
||||
QueueEvent $queueForEvents,
|
||||
Database $dbForPlatform,
|
||||
Document $team,
|
||||
?Key $apiKey,
|
||||
AuditContext $auditContext,
|
||||
) {
|
||||
$project = $this->getProject($projectId, $team, $dbForPlatform, $apiKey);
|
||||
|
||||
// The request may run through the console project; events and audits belong to the resolved project
|
||||
$queueForEvents->setProject($project);
|
||||
$auditContext->project = $project;
|
||||
$keyId = ID::unique();
|
||||
|
||||
$jwt = new JWT(System::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256', $duration, 0);
|
||||
|
||||
$secret = $jwt->encode([
|
||||
'projectId' => $project->getId(),
|
||||
'scopes' => $scopes
|
||||
]);
|
||||
|
||||
$now = new \DateTime();
|
||||
$expire = $now->add(new \DateInterval('PT' . $duration . 'S'))->format('Y-m-d\TH:i:s.u\Z');
|
||||
|
||||
$key = new Document([
|
||||
'$id' => $keyId,
|
||||
'$createdAt' => DatabaseDateTime::now(),
|
||||
'$updatedAt' => DatabaseDateTime::now(),
|
||||
'name' => '',
|
||||
'scopes' => $scopes,
|
||||
'expire' => $expire,
|
||||
'sdks' => [],
|
||||
'accessedAt' => null,
|
||||
'secret' => API_KEY_EPHEMERAL . '_' . $secret,
|
||||
]);
|
||||
|
||||
$queueForEvents->setParam('keyId', $key->getId());
|
||||
|
||||
$response
|
||||
->setStatusCode(Response::STATUS_CODE_CREATED)
|
||||
->dynamic($key, Response::MODEL_EPHEMERAL_KEY);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Platform\Modules\Organization\Http\Projects\Keys;
|
||||
|
||||
use Appwrite\Auth\Key;
|
||||
use Appwrite\Extend\Exception;
|
||||
use Appwrite\SDK\AuthType;
|
||||
use Appwrite\SDK\Method;
|
||||
use Appwrite\SDK\Response as SDKResponse;
|
||||
use Appwrite\Utopia\Response;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Validator\Authorization;
|
||||
use Utopia\Database\Validator\UID;
|
||||
use Utopia\Platform\Scope\HTTP;
|
||||
|
||||
class Get extends Action
|
||||
{
|
||||
use HTTP;
|
||||
|
||||
public static function getName()
|
||||
{
|
||||
return 'getProjectKey';
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this
|
||||
->setHttpMethod(Action::HTTP_REQUEST_METHOD_GET)
|
||||
->setHttpPath('/v1/organization/projects/:projectId/keys/:keyId')
|
||||
->desc('Get project key')
|
||||
->groups(['api', 'organization'])
|
||||
->label('scope', ['organization.projects.keys.read', 'keys.read'])
|
||||
->label('sdk', new Method(
|
||||
namespace: 'organization',
|
||||
group: 'keys',
|
||||
name: 'getProjectKey',
|
||||
description: <<<EOT
|
||||
Get a project key by its unique ID.
|
||||
EOT,
|
||||
auth: [AuthType::ADMIN, AuthType::KEY, AuthType::ORGANIZATION],
|
||||
responses: [
|
||||
new SDKResponse(
|
||||
code: Response::STATUS_CODE_OK,
|
||||
model: Response::MODEL_KEY,
|
||||
)
|
||||
]
|
||||
))
|
||||
->param('projectId', '', new UID(), 'Project unique ID.')
|
||||
->param('keyId', '', fn (Database $dbForPlatform) => new UID($dbForPlatform->getAdapter()->getMaxUIDLength()), 'Key ID.', false, ['dbForPlatform'])
|
||||
->inject('response')
|
||||
->inject('dbForPlatform')
|
||||
->inject('team')
|
||||
->inject('authorization')
|
||||
->inject('apiKey')
|
||||
->callback($this->action(...));
|
||||
}
|
||||
|
||||
public function action(
|
||||
string $projectId,
|
||||
string $keyId,
|
||||
Response $response,
|
||||
Database $dbForPlatform,
|
||||
Document $team,
|
||||
Authorization $authorization,
|
||||
?Key $apiKey,
|
||||
) {
|
||||
$project = $this->getProject($projectId, $team, $dbForPlatform, $apiKey);
|
||||
|
||||
$key = $authorization->skip(fn () => $dbForPlatform->getDocument('keys', $keyId));
|
||||
|
||||
if ($key->isEmpty() || $key->getAttribute('resourceType', '') !== 'projects' || $key->getAttribute('resourceInternalId', '') !== $project->getSequence()) {
|
||||
throw new Exception(Exception::KEY_NOT_FOUND);
|
||||
}
|
||||
|
||||
$response->dynamic($key, Response::MODEL_KEY);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Platform\Modules\Organization\Http\Projects\Keys;
|
||||
|
||||
use Appwrite\Auth\Key;
|
||||
use Appwrite\Event\Context\Audit as AuditContext;
|
||||
use Appwrite\Event\Event as QueueEvent;
|
||||
use Appwrite\Extend\Exception;
|
||||
use Appwrite\SDK\AuthType;
|
||||
use Appwrite\SDK\Method;
|
||||
use Appwrite\SDK\Response as SDKResponse;
|
||||
use Appwrite\Utopia\Response;
|
||||
use Utopia\Config\Config;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Exception\Duplicate;
|
||||
use Utopia\Database\Validator\Authorization;
|
||||
use Utopia\Database\Validator\Datetime;
|
||||
use Utopia\Database\Validator\UID;
|
||||
use Utopia\Platform\Enum;
|
||||
use Utopia\Platform\Scope\HTTP;
|
||||
use Utopia\Validator\ArrayList;
|
||||
use Utopia\Validator\Nullable;
|
||||
use Utopia\Validator\Text;
|
||||
use Utopia\Validator\WhiteList;
|
||||
|
||||
class Update extends Action
|
||||
{
|
||||
use HTTP;
|
||||
|
||||
public static function getName()
|
||||
{
|
||||
return 'updateProjectKey';
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this
|
||||
->setHttpMethod(Action::HTTP_REQUEST_METHOD_PUT)
|
||||
->setHttpPath('/v1/organization/projects/:projectId/keys/:keyId')
|
||||
->desc('Update project key')
|
||||
->groups(['api', 'organization'])
|
||||
->label('scope', ['organization.projects.keys.write', 'keys.write'])
|
||||
->label('event', 'keys.[keyId].update')
|
||||
->label('audits.event', 'project.key.update')
|
||||
->label('audits.resource', 'project.key/{response.$id}')
|
||||
->label('sdk', new Method(
|
||||
namespace: 'organization',
|
||||
group: 'keys',
|
||||
name: 'updateProjectKey',
|
||||
description: <<<EOT
|
||||
Update a project key by its unique ID. Use this endpoint to update the name, scopes, or expiration time of an API key.
|
||||
EOT,
|
||||
auth: [AuthType::ADMIN, AuthType::KEY, AuthType::ORGANIZATION],
|
||||
responses: [
|
||||
new SDKResponse(
|
||||
code: Response::STATUS_CODE_OK,
|
||||
model: Response::MODEL_KEY,
|
||||
)
|
||||
]
|
||||
))
|
||||
->param('projectId', '', new UID(), 'Project unique ID.')
|
||||
->param('keyId', '', fn (Database $dbForPlatform) => new UID($dbForPlatform->getAdapter()->getMaxUIDLength()), 'Key ID.', false, ['dbForPlatform'])
|
||||
->param('name', null, new Text(128), 'Key name. Max length: 128 chars.')
|
||||
->param('scopes', [], new ArrayList(new WhiteList(array_keys(Config::getParam('projectScopes')), true), APP_LIMIT_ARRAY_SCOPES_SIZE), 'Key scopes list. Maximum of ' . APP_LIMIT_ARRAY_SCOPES_SIZE . ' scopes are allowed.', optional: false, enum: new Enum(name: 'ProjectKeyScopes'))
|
||||
->param('expire', null, new Nullable(new Datetime()), 'Expiration time in [ISO 8601](https://www.iso.org/iso-8601-date-and-time-format.html) format. Use null for unlimited expiration.', true)
|
||||
->inject('response')
|
||||
->inject('queueForEvents')
|
||||
->inject('dbForPlatform')
|
||||
->inject('team')
|
||||
->inject('authorization')
|
||||
->inject('apiKey')
|
||||
->inject('auditContext')
|
||||
->callback($this->action(...));
|
||||
}
|
||||
|
||||
public function action(
|
||||
string $projectId,
|
||||
string $keyId,
|
||||
string $name,
|
||||
array $scopes,
|
||||
?string $expire,
|
||||
Response $response,
|
||||
QueueEvent $queueForEvents,
|
||||
Database $dbForPlatform,
|
||||
Document $team,
|
||||
Authorization $authorization,
|
||||
?Key $apiKey,
|
||||
AuditContext $auditContext,
|
||||
) {
|
||||
$project = $this->getProject($projectId, $team, $dbForPlatform, $apiKey);
|
||||
|
||||
// The request may run through the console project; events and audits belong to the resolved project
|
||||
$queueForEvents->setProject($project);
|
||||
$auditContext->project = $project;
|
||||
$key = $authorization->skip(fn () => $dbForPlatform->getDocument('keys', $keyId));
|
||||
|
||||
if ($key->isEmpty() || $key->getAttribute('resourceType', '') !== 'projects' || $key->getAttribute('resourceInternalId', '') !== $project->getSequence()) {
|
||||
throw new Exception(Exception::KEY_NOT_FOUND);
|
||||
}
|
||||
|
||||
$updates = new Document([
|
||||
'name' => $name,
|
||||
'scopes' => $scopes,
|
||||
'expire' => $expire,
|
||||
]);
|
||||
|
||||
try {
|
||||
$key = $authorization->skip(fn () => $dbForPlatform->updateDocument('keys', $key->getId(), $updates));
|
||||
} catch (Duplicate) {
|
||||
throw new Exception(Exception::KEY_ALREADY_EXISTS);
|
||||
}
|
||||
|
||||
$authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId()));
|
||||
|
||||
$queueForEvents->setParam('keyId', $key->getId());
|
||||
|
||||
$response->dynamic($key, Response::MODEL_KEY);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Platform\Modules\Organization\Http\Projects\Keys;
|
||||
|
||||
use Appwrite\Auth\Key;
|
||||
use Appwrite\Extend\Exception;
|
||||
use Appwrite\SDK\AuthType;
|
||||
use Appwrite\SDK\Method;
|
||||
use Appwrite\SDK\Response as SDKResponse;
|
||||
use Appwrite\Utopia\Database\Validator\Queries\Keys;
|
||||
use Appwrite\Utopia\Response;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Exception\Order as OrderException;
|
||||
use Utopia\Database\Exception\Query as QueryException;
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\Database\Validator\Authorization;
|
||||
use Utopia\Database\Validator\Query\Cursor;
|
||||
use Utopia\Database\Validator\UID;
|
||||
use Utopia\Platform\Scope\HTTP;
|
||||
use Utopia\Validator\Boolean;
|
||||
|
||||
class XList extends Action
|
||||
{
|
||||
use HTTP;
|
||||
|
||||
public static function getName()
|
||||
{
|
||||
return 'listProjectKeys';
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this
|
||||
->setHttpMethod(Action::HTTP_REQUEST_METHOD_GET)
|
||||
->setHttpPath('/v1/organization/projects/:projectId/keys')
|
||||
->desc('List project keys')
|
||||
->groups(['api', 'organization'])
|
||||
->label('scope', ['organization.projects.keys.read', 'keys.read'])
|
||||
->label('sdk', new Method(
|
||||
namespace: 'organization',
|
||||
group: 'keys',
|
||||
name: 'listProjectKeys',
|
||||
description: <<<EOT
|
||||
Get a list of all API keys of a project in your organization.
|
||||
EOT,
|
||||
auth: [AuthType::ADMIN, AuthType::KEY, AuthType::ORGANIZATION],
|
||||
responses: [
|
||||
new SDKResponse(
|
||||
code: Response::STATUS_CODE_OK,
|
||||
model: Response::MODEL_KEY_LIST,
|
||||
)
|
||||
]
|
||||
))
|
||||
->param('projectId', '', new UID(), 'Project unique ID.')
|
||||
->param('queries', [], new Keys(), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' queries are allowed, each ' . APP_LIMIT_ARRAY_ELEMENT_SIZE . ' characters long. You may filter on the following attributes: ' . implode(', ', Keys::ALLOWED_ATTRIBUTES), true)
|
||||
->param('total', true, new Boolean(true), 'When set to false, the total count returned will be 0 and will not be calculated.', true)
|
||||
->inject('response')
|
||||
->inject('dbForPlatform')
|
||||
->inject('team')
|
||||
->inject('authorization')
|
||||
->inject('apiKey')
|
||||
->callback($this->action(...));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string> $queries
|
||||
*/
|
||||
public function action(
|
||||
string $projectId,
|
||||
array $queries,
|
||||
bool $includeTotal,
|
||||
Response $response,
|
||||
Database $dbForPlatform,
|
||||
Document $team,
|
||||
Authorization $authorization,
|
||||
?Key $apiKey,
|
||||
) {
|
||||
$project = $this->getProject($projectId, $team, $dbForPlatform, $apiKey);
|
||||
|
||||
try {
|
||||
$queries = Query::parseQueries($queries);
|
||||
} catch (QueryException $e) {
|
||||
throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage());
|
||||
}
|
||||
|
||||
// Backwards compatibility
|
||||
if (\count(Query::getByType($queries, [Query::TYPE_LIMIT])) === 0) {
|
||||
$queries[] = Query::limit(5000);
|
||||
}
|
||||
|
||||
$queries[] = Query::equal('resourceType', ['projects']);
|
||||
$queries[] = Query::equal('resourceInternalId', [$project->getSequence()]);
|
||||
|
||||
$cursor = Query::getCursorQueries($queries, false);
|
||||
$cursor = \reset($cursor);
|
||||
|
||||
if ($cursor !== false) {
|
||||
$validator = new Cursor();
|
||||
if (!$validator->isValid($cursor)) {
|
||||
throw new Exception(Exception::GENERAL_QUERY_INVALID, $validator->getDescription());
|
||||
}
|
||||
|
||||
$keyId = $cursor->getValue();
|
||||
$cursorDocument = $authorization->skip(fn () => $dbForPlatform->findOne('keys', [
|
||||
Query::equal('$id', [$keyId]),
|
||||
Query::equal('resourceType', ['projects']),
|
||||
Query::equal('resourceInternalId', [$project->getSequence()]),
|
||||
]));
|
||||
|
||||
if ($cursorDocument->isEmpty()) {
|
||||
throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "Key '{$keyId}' for the 'cursor' value not found.");
|
||||
}
|
||||
|
||||
$cursor->setValue($cursorDocument);
|
||||
}
|
||||
|
||||
$filterQueries = Query::groupByType($queries)['filters'];
|
||||
|
||||
try {
|
||||
$keys = $authorization->skip(fn () => $dbForPlatform->find('keys', $queries));
|
||||
$total = $includeTotal ? $authorization->skip(fn () => $dbForPlatform->count('keys', $filterQueries, APP_LIMIT_COUNT)) : 0;
|
||||
} catch (OrderException $e) {
|
||||
throw new Exception(Exception::DATABASE_QUERY_ORDER_NULL, "The order attribute '{$e->getAttribute()}' had a null value. Cursor pagination requires all documents order attribute values are non-null.");
|
||||
}
|
||||
|
||||
$response->dynamic(new Document([
|
||||
'keys' => $keys,
|
||||
'total' => $total,
|
||||
]), Response::MODEL_KEY_LIST);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,12 @@ use Appwrite\Platform\Modules\Organization\Http\Init as Init;
|
||||
use Appwrite\Platform\Modules\Organization\Http\Projects\Create as CreateProject;
|
||||
use Appwrite\Platform\Modules\Organization\Http\Projects\Delete as DeleteProject;
|
||||
use Appwrite\Platform\Modules\Organization\Http\Projects\Get as GetProject;
|
||||
use Appwrite\Platform\Modules\Organization\Http\Projects\Keys\Create as CreateProjectKey;
|
||||
use Appwrite\Platform\Modules\Organization\Http\Projects\Keys\Delete as DeleteProjectKey;
|
||||
use Appwrite\Platform\Modules\Organization\Http\Projects\Keys\Ephemeral\Create as CreateEphemeralProjectKey;
|
||||
use Appwrite\Platform\Modules\Organization\Http\Projects\Keys\Get as GetProjectKey;
|
||||
use Appwrite\Platform\Modules\Organization\Http\Projects\Keys\Update as UpdateProjectKey;
|
||||
use Appwrite\Platform\Modules\Organization\Http\Projects\Keys\XList as ListProjectKeys;
|
||||
use Appwrite\Platform\Modules\Organization\Http\Projects\Update as UpdateProject;
|
||||
use Appwrite\Platform\Modules\Organization\Http\Projects\XList as ListProjects;
|
||||
use Utopia\Platform\Service;
|
||||
@@ -25,5 +31,13 @@ class Http extends Service
|
||||
$this->addAction(GetProject::getName(), new GetProject());
|
||||
$this->addAction(UpdateProject::getName(), new UpdateProject());
|
||||
$this->addAction(DeleteProject::getName(), new DeleteProject());
|
||||
|
||||
// Project keys
|
||||
$this->addAction(CreateProjectKey::getName(), new CreateProjectKey());
|
||||
$this->addAction(CreateEphemeralProjectKey::getName(), new CreateEphemeralProjectKey());
|
||||
$this->addAction(ListProjectKeys::getName(), new ListProjectKeys());
|
||||
$this->addAction(GetProjectKey::getName(), new GetProjectKey());
|
||||
$this->addAction(UpdateProjectKey::getName(), new UpdateProjectKey());
|
||||
$this->addAction(DeleteProjectKey::getName(), new DeleteProjectKey());
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,10 @@ use Utopia\Validator\Nullable;
|
||||
use Utopia\Validator\Text;
|
||||
use Utopia\Validator\WhiteList;
|
||||
|
||||
/**
|
||||
* TODO: Remove once the Console, CLI and SDKs use the Organization API
|
||||
* (/v1/organization/projects/:projectId/keys) instead of this project-scoped route.
|
||||
*/
|
||||
class Create extends Base
|
||||
{
|
||||
use HTTP;
|
||||
|
||||
@@ -17,6 +17,10 @@ use Utopia\Database\Validator\UID;
|
||||
use Utopia\Platform\Action;
|
||||
use Utopia\Platform\Scope\HTTP;
|
||||
|
||||
/**
|
||||
* TODO: Remove once the Console, CLI and SDKs use the Organization API
|
||||
* (/v1/organization/projects/:projectId/keys) instead of this project-scoped route.
|
||||
*/
|
||||
class Delete extends Base
|
||||
{
|
||||
use HTTP;
|
||||
|
||||
@@ -21,6 +21,10 @@ use Utopia\Validator\ArrayList;
|
||||
use Utopia\Validator\Range;
|
||||
use Utopia\Validator\WhiteList;
|
||||
|
||||
/**
|
||||
* TODO: Remove once the Console, CLI and SDKs use the Organization API
|
||||
* (/v1/organization/projects/:projectId/keys) instead of this project-scoped route.
|
||||
*/
|
||||
class Create extends Base
|
||||
{
|
||||
use HTTP;
|
||||
|
||||
@@ -15,6 +15,10 @@ use Utopia\Database\Validator\UID;
|
||||
use Utopia\Platform\Action;
|
||||
use Utopia\Platform\Scope\HTTP;
|
||||
|
||||
/**
|
||||
* TODO: Remove once the Console, CLI and SDKs use the Organization API
|
||||
* (/v1/organization/projects/:projectId/keys) instead of this project-scoped route.
|
||||
*/
|
||||
class Get extends Base
|
||||
{
|
||||
use HTTP;
|
||||
|
||||
@@ -24,6 +24,10 @@ use Utopia\Validator\Nullable;
|
||||
use Utopia\Validator\Text;
|
||||
use Utopia\Validator\WhiteList;
|
||||
|
||||
/**
|
||||
* TODO: Remove once the Console, CLI and SDKs use the Organization API
|
||||
* (/v1/organization/projects/:projectId/keys) instead of this project-scoped route.
|
||||
*/
|
||||
class Update extends Base
|
||||
{
|
||||
use HTTP;
|
||||
|
||||
@@ -20,6 +20,10 @@ use Utopia\Platform\Action;
|
||||
use Utopia\Platform\Scope\HTTP;
|
||||
use Utopia\Validator\Boolean;
|
||||
|
||||
/**
|
||||
* TODO: Remove once the Console, CLI and SDKs use the Organization API
|
||||
* (/v1/organization/projects/:projectId/keys) instead of this project-scoped route.
|
||||
*/
|
||||
class XList extends Base
|
||||
{
|
||||
use HTTP;
|
||||
|
||||
@@ -141,6 +141,7 @@ class Http extends Service
|
||||
$this->addAction(UpdateVariable::getName(), new UpdateVariable());
|
||||
|
||||
// Keys
|
||||
// TODO: Remove once the Console, CLI and SDKs use the Organization API instead
|
||||
$this->addAction(CreateKey::getName(), new CreateKey());
|
||||
$this->addAction(CreateEphemeralKey::getName(), new CreateEphemeralKey());
|
||||
$this->addAction(ListKeys::getName(), new ListKeys());
|
||||
|
||||
@@ -72,6 +72,7 @@ class V21 extends Filter
|
||||
$content = $this->fillWebhookid($content);
|
||||
break;
|
||||
case 'project.createKey':
|
||||
case 'organization.createProjectKey':
|
||||
$content = $this->fillKeyId($content);
|
||||
break;
|
||||
case 'project.createVariable':
|
||||
|
||||
@@ -81,6 +81,8 @@ class V22 extends Filter
|
||||
break;
|
||||
case 'project.createKey':
|
||||
case 'project.updateKey':
|
||||
case 'organization.createProjectKey':
|
||||
case 'organization.updateProjectKey':
|
||||
$content = $this->parseKeyScopes($content);
|
||||
break;
|
||||
case 'webhooks.create':
|
||||
|
||||
@@ -196,6 +196,8 @@ final class ConsoleConsoleClientTest extends Scope
|
||||
// Well-known scopes must be present
|
||||
$this->assertContains('projects.read', $scopeIds);
|
||||
$this->assertContains('projects.write', $scopeIds);
|
||||
$this->assertContains('organization.projects.keys.read', $scopeIds);
|
||||
$this->assertContains('organization.projects.keys.write', $scopeIds);
|
||||
|
||||
// Every scope has the expected shape
|
||||
foreach ($response['body']['scopes'] as $scope) {
|
||||
|
||||
@@ -93,6 +93,8 @@ final class ConsoleCustomServerTest extends Scope
|
||||
|
||||
$scopeIds = \array_column($response['body']['scopes'], '$id');
|
||||
$this->assertContains('projects.read', $scopeIds);
|
||||
$this->assertContains('organization.projects.keys.read', $scopeIds);
|
||||
$this->assertContains('organization.projects.keys.write', $scopeIds);
|
||||
|
||||
$projectsRead = null;
|
||||
foreach ($response['body']['scopes'] as $scope) {
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\E2E\Services\Organization;
|
||||
|
||||
use Tests\E2E\Scopes\ProjectCustom;
|
||||
use Tests\E2E\Scopes\Scope;
|
||||
use Tests\E2E\Scopes\SideConsole;
|
||||
|
||||
final class KeysConsoleClientTest extends Scope
|
||||
{
|
||||
use KeysBase;
|
||||
use ProjectCustom;
|
||||
use SideConsole;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\E2E\Services\Organization;
|
||||
|
||||
use Tests\E2E\Scopes\ProjectCustom;
|
||||
use Tests\E2E\Scopes\Scope;
|
||||
use Tests\E2E\Scopes\SideServer;
|
||||
|
||||
final class KeysCustomServerTest extends Scope
|
||||
{
|
||||
use KeysBase;
|
||||
use ProjectCustom;
|
||||
use SideServer;
|
||||
}
|
||||
+2
-2
@@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\E2E\Services\Project;
|
||||
namespace Tests\E2E\Services\Organization;
|
||||
|
||||
use Tests\E2E\Client;
|
||||
use Tests\E2E\Scopes\ProjectCustom;
|
||||
@@ -37,7 +37,7 @@ final class KeysIntegrationTest extends Scope
|
||||
// Step 1: Create an ephemeral key scoped to users.read only.
|
||||
$ephemeralKey = $this->client->call(
|
||||
Client::METHOD_POST,
|
||||
'/project/keys/ephemeral',
|
||||
'/organization/projects/' . $projectId . '/keys/ephemeral',
|
||||
$serverHeaders,
|
||||
[
|
||||
'scopes' => ['users.read'],
|
||||
@@ -27,7 +27,7 @@ trait ProjectsBase
|
||||
$team = $this->createTeamFixture(array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getHeaders()), [
|
||||
], $this->getConsoleHeaders()), [
|
||||
'teamId' => $teamId,
|
||||
'name' => 'Organization Test',
|
||||
]);
|
||||
@@ -40,11 +40,22 @@ trait ProjectsBase
|
||||
return self::$cachedOrganization;
|
||||
}
|
||||
|
||||
/**
|
||||
* Console session headers. Admin mode is rejected for the console project.
|
||||
*/
|
||||
protected function getConsoleHeaders(): array
|
||||
{
|
||||
$headers = $this->getHeaders();
|
||||
unset($headers['x-appwrite-mode']);
|
||||
|
||||
return $headers;
|
||||
}
|
||||
|
||||
protected function getOrganizationHeaders(): array
|
||||
{
|
||||
$organization = $this->setupOrganization();
|
||||
|
||||
return array_merge($this->getHeaders(), [
|
||||
return array_merge($this->getConsoleHeaders(), [
|
||||
'x-appwrite-organization' => $organization['teamId'],
|
||||
]);
|
||||
}
|
||||
@@ -60,7 +71,7 @@ trait ProjectsBase
|
||||
|
||||
$project = null;
|
||||
for ($i = 0; $i < 3; $i++) {
|
||||
$project = $this->client->call(Client::METHOD_POST, '/v1/organization/projects', array_merge([
|
||||
$project = $this->client->call(Client::METHOD_POST, '/organization/projects', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()), [
|
||||
@@ -91,7 +102,7 @@ trait ProjectsBase
|
||||
/**
|
||||
* Test for SUCCESS
|
||||
*/
|
||||
$response = $this->client->call(Client::METHOD_POST, '/v1/organization/projects', array_merge([
|
||||
$response = $this->client->call(Client::METHOD_POST, '/organization/projects', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()), [
|
||||
@@ -105,28 +116,25 @@ trait ProjectsBase
|
||||
$this->assertEquals('Organization Project Test', $response['body']['name']);
|
||||
$this->assertEquals($teamId, $response['body']['teamId']);
|
||||
$this->assertEquals(PROJECT_STATUS_ACTIVE, $response['body']['status']);
|
||||
$this->assertArrayHasKey('platforms', $response['body']);
|
||||
$this->assertArrayHasKey('webhooks', $response['body']);
|
||||
$this->assertArrayHasKey('keys', $response['body']);
|
||||
|
||||
/**
|
||||
* Test for FAILURE - missing organization header
|
||||
*/
|
||||
$response = $this->client->call(Client::METHOD_POST, '/v1/organization/projects', array_merge([
|
||||
$response = $this->client->call(Client::METHOD_POST, '/organization/projects', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getHeaders()), [
|
||||
], $this->getConsoleHeaders()), [
|
||||
'projectId' => ID::unique(),
|
||||
'name' => 'Organization Project Test',
|
||||
'region' => System::getEnv('_APP_REGION', 'default'),
|
||||
]);
|
||||
|
||||
$this->assertEquals(404, $response['headers']['status-code']);
|
||||
$this->assertEquals(401, $response['headers']['status-code']);
|
||||
|
||||
/**
|
||||
* Test for FAILURE - empty name
|
||||
*/
|
||||
$response = $this->client->call(Client::METHOD_POST, '/v1/organization/projects', array_merge([
|
||||
$response = $this->client->call(Client::METHOD_POST, '/organization/projects', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()), [
|
||||
@@ -143,7 +151,7 @@ trait ProjectsBase
|
||||
$organization = $this->setupOrganization();
|
||||
$projectId = ID::unique();
|
||||
|
||||
$response = $this->client->call(Client::METHOD_POST, '/v1/organization/projects', array_merge([
|
||||
$response = $this->client->call(Client::METHOD_POST, '/organization/projects', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()), [
|
||||
@@ -157,7 +165,7 @@ trait ProjectsBase
|
||||
/**
|
||||
* Test for FAILURE - duplicate project ID
|
||||
*/
|
||||
$response = $this->client->call(Client::METHOD_POST, '/v1/organization/projects', array_merge([
|
||||
$response = $this->client->call(Client::METHOD_POST, '/organization/projects', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()), [
|
||||
@@ -179,7 +187,7 @@ trait ProjectsBase
|
||||
/**
|
||||
* Test for SUCCESS
|
||||
*/
|
||||
$response = $this->client->call(Client::METHOD_GET, '/v1/organization/projects/' . $projectId, array_merge([
|
||||
$response = $this->client->call(Client::METHOD_GET, '/organization/projects/' . $projectId, array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()));
|
||||
@@ -189,14 +197,11 @@ trait ProjectsBase
|
||||
$this->assertEquals($projectId, $response['body']['$id']);
|
||||
$this->assertEquals('Organization Project Test', $response['body']['name']);
|
||||
$this->assertEquals(PROJECT_STATUS_ACTIVE, $response['body']['status']);
|
||||
$this->assertArrayHasKey('platforms', $response['body']);
|
||||
$this->assertArrayHasKey('webhooks', $response['body']);
|
||||
$this->assertArrayHasKey('keys', $response['body']);
|
||||
|
||||
/**
|
||||
* Test for FAILURE - project not found
|
||||
*/
|
||||
$response = $this->client->call(Client::METHOD_GET, '/v1/organization/projects/' . ID::unique(), array_merge([
|
||||
$response = $this->client->call(Client::METHOD_GET, '/organization/projects/' . ID::unique(), array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()));
|
||||
@@ -209,17 +214,17 @@ trait ProjectsBase
|
||||
$otherTeam = $this->createTeamFixture(array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getHeaders()), [
|
||||
], $this->getConsoleHeaders()), [
|
||||
'teamId' => ID::unique(),
|
||||
'name' => 'Other Organization',
|
||||
]);
|
||||
$this->assertContains($otherTeam['headers']['status-code'], [200]);
|
||||
$otherTeamId = $otherTeam['body']['$id'] ?? $otherTeam['body']['teamId'];
|
||||
|
||||
$otherProject = $this->client->call(Client::METHOD_POST, '/v1/organization/projects', array_merge([
|
||||
$otherProject = $this->client->call(Client::METHOD_POST, '/organization/projects', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], array_merge($this->getHeaders(), [
|
||||
], array_merge($this->getConsoleHeaders(), [
|
||||
'x-appwrite-organization' => $otherTeamId,
|
||||
])), [
|
||||
'projectId' => ID::unique(),
|
||||
@@ -229,7 +234,7 @@ trait ProjectsBase
|
||||
$this->assertEquals(201, $otherProject['headers']['status-code']);
|
||||
$otherProjectId = $otherProject['body']['$id'];
|
||||
|
||||
$response = $this->client->call(Client::METHOD_GET, '/v1/organization/projects/' . $otherProjectId, array_merge([
|
||||
$response = $this->client->call(Client::METHOD_GET, '/organization/projects/' . $otherProjectId, array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()));
|
||||
@@ -245,7 +250,7 @@ trait ProjectsBase
|
||||
/**
|
||||
* Test for SUCCESS
|
||||
*/
|
||||
$response = $this->client->call(Client::METHOD_PATCH, '/v1/organization/projects/' . $projectId, array_merge([
|
||||
$response = $this->client->call(Client::METHOD_PATCH, '/organization/projects/' . $projectId, array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()), [
|
||||
@@ -259,7 +264,7 @@ trait ProjectsBase
|
||||
/**
|
||||
* Test for FAILURE - project not found
|
||||
*/
|
||||
$response = $this->client->call(Client::METHOD_PATCH, '/v1/organization/projects/' . ID::unique(), array_merge([
|
||||
$response = $this->client->call(Client::METHOD_PATCH, '/organization/projects/' . ID::unique(), array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()), [
|
||||
@@ -271,7 +276,7 @@ trait ProjectsBase
|
||||
/**
|
||||
* Test for FAILURE - empty name
|
||||
*/
|
||||
$response = $this->client->call(Client::METHOD_PATCH, '/v1/organization/projects/' . $projectId, array_merge([
|
||||
$response = $this->client->call(Client::METHOD_PATCH, '/organization/projects/' . $projectId, array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()), [
|
||||
@@ -285,7 +290,7 @@ trait ProjectsBase
|
||||
{
|
||||
$organization = $this->setupOrganization();
|
||||
|
||||
$project = $this->client->call(Client::METHOD_POST, '/v1/organization/projects', array_merge([
|
||||
$project = $this->client->call(Client::METHOD_POST, '/organization/projects', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()), [
|
||||
@@ -300,7 +305,7 @@ trait ProjectsBase
|
||||
/**
|
||||
* Test for SUCCESS
|
||||
*/
|
||||
$response = $this->client->call(Client::METHOD_DELETE, '/v1/organization/projects/' . $projectId, array_merge([
|
||||
$response = $this->client->call(Client::METHOD_DELETE, '/organization/projects/' . $projectId, array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()));
|
||||
@@ -308,7 +313,7 @@ trait ProjectsBase
|
||||
$this->assertEquals(204, $response['headers']['status-code']);
|
||||
|
||||
// Verify project is actually deleted
|
||||
$response = $this->client->call(Client::METHOD_GET, '/v1/organization/projects/' . $projectId, array_merge([
|
||||
$response = $this->client->call(Client::METHOD_GET, '/organization/projects/' . $projectId, array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()));
|
||||
@@ -318,7 +323,7 @@ trait ProjectsBase
|
||||
/**
|
||||
* Test for FAILURE - project not found (already deleted)
|
||||
*/
|
||||
$response = $this->client->call(Client::METHOD_DELETE, '/v1/organization/projects/' . $projectId, array_merge([
|
||||
$response = $this->client->call(Client::METHOD_DELETE, '/organization/projects/' . $projectId, array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()));
|
||||
@@ -332,7 +337,7 @@ trait ProjectsBase
|
||||
$teamId = $organization['teamId'];
|
||||
|
||||
// Create a second project in the same organization
|
||||
$project2 = $this->client->call(Client::METHOD_POST, '/v1/organization/projects', array_merge([
|
||||
$project2 = $this->client->call(Client::METHOD_POST, '/organization/projects', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()), [
|
||||
@@ -347,7 +352,7 @@ trait ProjectsBase
|
||||
/**
|
||||
* Test for SUCCESS - basic list
|
||||
*/
|
||||
$response = $this->client->call(Client::METHOD_GET, '/v1/organization/projects', array_merge([
|
||||
$response = $this->client->call(Client::METHOD_GET, '/organization/projects', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()));
|
||||
@@ -360,7 +365,7 @@ trait ProjectsBase
|
||||
/**
|
||||
* Test search queries
|
||||
*/
|
||||
$response = $this->client->call(Client::METHOD_GET, '/v1/organization/projects', array_merge([
|
||||
$response = $this->client->call(Client::METHOD_GET, '/organization/projects', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders(), [
|
||||
@@ -370,12 +375,12 @@ trait ProjectsBase
|
||||
$this->assertEquals(200, $response['headers']['status-code']);
|
||||
$this->assertGreaterThan(0, $response['body']['total']);
|
||||
$this->assertIsArray($response['body']['projects']);
|
||||
$this->assertEquals('Second Organization Project', $response['body']['projects'][0]['name']);
|
||||
$this->assertContains('Second Organization Project', \array_column($response['body']['projects'], 'name'));
|
||||
|
||||
/**
|
||||
* Test pagination with limit
|
||||
*/
|
||||
$response = $this->client->call(Client::METHOD_GET, '/v1/organization/projects', array_merge([
|
||||
$response = $this->client->call(Client::METHOD_GET, '/organization/projects', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()), [
|
||||
@@ -390,7 +395,7 @@ trait ProjectsBase
|
||||
/**
|
||||
* Test pagination with offset
|
||||
*/
|
||||
$response = $this->client->call(Client::METHOD_GET, '/v1/organization/projects', array_merge([
|
||||
$response = $this->client->call(Client::METHOD_GET, '/organization/projects', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()), [
|
||||
@@ -405,7 +410,7 @@ trait ProjectsBase
|
||||
/**
|
||||
* Test query by name
|
||||
*/
|
||||
$response = $this->client->call(Client::METHOD_GET, '/v1/organization/projects', array_merge([
|
||||
$response = $this->client->call(Client::METHOD_GET, '/organization/projects', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()), [
|
||||
@@ -416,12 +421,12 @@ trait ProjectsBase
|
||||
|
||||
$this->assertEquals(200, $response['headers']['status-code']);
|
||||
$this->assertGreaterThanOrEqual(1, count($response['body']['projects']));
|
||||
$this->assertEquals('Second Organization Project', $response['body']['projects'][0]['name']);
|
||||
$this->assertContains('Second Organization Project', \array_column($response['body']['projects'], 'name'));
|
||||
|
||||
/**
|
||||
* Test cursor pagination
|
||||
*/
|
||||
$response = $this->client->call(Client::METHOD_GET, '/v1/organization/projects', array_merge([
|
||||
$response = $this->client->call(Client::METHOD_GET, '/organization/projects', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()));
|
||||
@@ -429,7 +434,7 @@ trait ProjectsBase
|
||||
$this->assertEquals(200, $response['headers']['status-code']);
|
||||
$this->assertNotEmpty($response['body']['projects']);
|
||||
|
||||
$response = $this->client->call(Client::METHOD_GET, '/v1/organization/projects', array_merge([
|
||||
$response = $this->client->call(Client::METHOD_GET, '/organization/projects', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()), [
|
||||
@@ -444,7 +449,7 @@ trait ProjectsBase
|
||||
/**
|
||||
* Test for FAILURE - invalid cursor
|
||||
*/
|
||||
$response = $this->client->call(Client::METHOD_GET, '/v1/organization/projects', array_merge([
|
||||
$response = $this->client->call(Client::METHOD_GET, '/organization/projects', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()), [
|
||||
@@ -461,7 +466,7 @@ trait ProjectsBase
|
||||
$data = $this->setupOrganizationProject();
|
||||
$projectId = $data['projectId'];
|
||||
|
||||
$response = $this->client->call(Client::METHOD_GET, '/v1/organization/projects', array_merge([
|
||||
$response = $this->client->call(Client::METHOD_GET, '/organization/projects', array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getOrganizationHeaders()), [
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user