2019-12-15 08:33:29 +13:00
|
|
|
<?php
|
|
|
|
|
2019-12-16 07:56:44 +13:00
|
|
|
global $utopia, $response, $consoleDB, $project;
|
2019-12-15 08:33:29 +13:00
|
|
|
|
|
|
|
use Utopia\Exception;
|
|
|
|
use Utopia\Response;
|
|
|
|
use Utopia\Validator\ArrayList;
|
|
|
|
use Utopia\Validator\Text;
|
|
|
|
use Utopia\Validator\WhiteList;
|
|
|
|
use Database\Database;
|
|
|
|
use Database\Document;
|
|
|
|
use Database\Validator\UID;
|
|
|
|
|
2019-12-17 08:35:33 +13:00
|
|
|
include_once __DIR__ . '/../shared/api.php';
|
2019-12-15 08:33:29 +13:00
|
|
|
|
2019-12-16 18:11:41 +13:00
|
|
|
$scopes = include __DIR__.'/../../../app/config/scopes.php';
|
2019-12-15 08:33:29 +13:00
|
|
|
|
2019-12-16 07:56:44 +13:00
|
|
|
$utopia->get('/v1/keys')
|
2019-12-15 08:33:29 +13:00
|
|
|
->desc('List Keys')
|
2019-12-16 17:55:05 +13:00
|
|
|
->label('scope', 'keys.read')
|
|
|
|
->label('sdk.namespace', 'keys')
|
2019-12-15 08:33:29 +13:00
|
|
|
->label('sdk.method', 'listKeys')
|
|
|
|
->action(
|
2019-12-16 07:56:44 +13:00
|
|
|
function () use ($response, $consoleDB, $project) {
|
2019-12-15 08:33:29 +13:00
|
|
|
$response->json($project->getAttribute('keys', [])); //FIXME make sure array objects return correctly
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-12-16 07:56:44 +13:00
|
|
|
$utopia->get('/v1/keys/:keyId')
|
2019-12-15 08:33:29 +13:00
|
|
|
->desc('Get Key')
|
2019-12-16 17:55:05 +13:00
|
|
|
->label('scope', 'keys.read')
|
|
|
|
->label('sdk.namespace', 'keys')
|
2019-12-15 08:33:29 +13:00
|
|
|
->label('sdk.method', 'getKey')
|
|
|
|
->param('keyId', null, function () { return new UID(); }, 'Key unique ID.')
|
|
|
|
->action(
|
2019-12-16 07:56:44 +13:00
|
|
|
function ($keyId) use ($response, $consoleDB, $project) {
|
2019-12-15 08:33:29 +13:00
|
|
|
$key = $project->search('$uid', $keyId, $project->getAttribute('keys', []));
|
|
|
|
|
|
|
|
if (empty($key) && $key instanceof Document) {
|
|
|
|
throw new Exception('Key not found', 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->json($key->getArrayCopy());
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-12-16 07:56:44 +13:00
|
|
|
$utopia->post('/v1/keys')
|
2019-12-15 08:33:29 +13:00
|
|
|
->desc('Create Key')
|
2019-12-16 17:55:05 +13:00
|
|
|
->label('scope', 'keys.write')
|
|
|
|
->label('sdk.namespace', 'keys')
|
2019-12-15 08:33:29 +13:00
|
|
|
->label('sdk.method', 'createKey')
|
|
|
|
->param('name', null, function () { return new Text(256); }, 'Key name')
|
|
|
|
->param('scopes', null, function () use ($scopes) { return new ArrayList(new WhiteList($scopes)); }, 'Key scopes list')
|
|
|
|
->action(
|
2019-12-16 07:56:44 +13:00
|
|
|
function ($name, $scopes) use ($response, $consoleDB, $project) {
|
2019-12-15 08:33:29 +13:00
|
|
|
$key = $consoleDB->createDocument([
|
|
|
|
'$collection' => Database::SYSTEM_COLLECTION_KEYS,
|
|
|
|
'$permissions' => [
|
|
|
|
'read' => ['team:'.$project->getAttribute('teamId', null)],
|
|
|
|
'write' => ['team:'.$project->getAttribute('teamId', null).'/owner', 'team:'.$project->getAttribute('teamId', null).'/developer'],
|
|
|
|
],
|
|
|
|
'name' => $name,
|
|
|
|
'scopes' => $scopes,
|
|
|
|
'secret' => bin2hex(random_bytes(128)),
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (false === $key) {
|
|
|
|
throw new Exception('Failed saving key to DB', 500);
|
|
|
|
}
|
|
|
|
|
|
|
|
$project->setAttribute('keys', $key, Document::SET_TYPE_APPEND);
|
|
|
|
|
|
|
|
$project = $consoleDB->updateDocument($project->getArrayCopy());
|
|
|
|
|
|
|
|
if (false === $project) {
|
|
|
|
throw new Exception('Failed saving project to DB', 500);
|
|
|
|
}
|
|
|
|
|
|
|
|
$response
|
|
|
|
->setStatusCode(Response::STATUS_CODE_CREATED)
|
|
|
|
->json($key->getArrayCopy())
|
|
|
|
;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-12-16 07:56:44 +13:00
|
|
|
$utopia->put('/v1/keys/:keyId')
|
2019-12-15 08:33:29 +13:00
|
|
|
->desc('Update Key')
|
2019-12-16 17:55:05 +13:00
|
|
|
->label('scope', 'keys.write')
|
|
|
|
->label('sdk.namespace', 'keys')
|
2019-12-15 08:33:29 +13:00
|
|
|
->label('sdk.method', 'updateKey')
|
|
|
|
->param('keyId', null, function () { return new UID(); }, 'Key unique ID.')
|
|
|
|
->param('name', null, function () { return new Text(256); }, 'Key name')
|
|
|
|
->param('scopes', null, function () use ($scopes) { return new ArrayList(new WhiteList($scopes)); }, 'Key scopes list')
|
|
|
|
->action(
|
2019-12-16 07:56:44 +13:00
|
|
|
function ($keyId, $name, $scopes) use ($response, $consoleDB, $project) {
|
2019-12-15 08:33:29 +13:00
|
|
|
$key = $project->search('$uid', $keyId, $project->getAttribute('keys', []));
|
|
|
|
|
|
|
|
if (empty($key) && $key instanceof Document) {
|
|
|
|
throw new Exception('Key not found', 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
$key
|
|
|
|
->setAttribute('name', $name)
|
|
|
|
->setAttribute('scopes', $scopes)
|
|
|
|
;
|
|
|
|
|
|
|
|
if (false === $consoleDB->updateDocument($key->getArrayCopy())) {
|
|
|
|
throw new Exception('Failed saving key to DB', 500);
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->json($key->getArrayCopy());
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-12-16 07:56:44 +13:00
|
|
|
$utopia->delete('/v1/keys/:keyId')
|
2019-12-15 08:33:29 +13:00
|
|
|
->desc('Delete Key')
|
2019-12-16 17:55:05 +13:00
|
|
|
->label('scope', 'keys.write')
|
|
|
|
->label('sdk.namespace', 'keys')
|
2019-12-15 08:33:29 +13:00
|
|
|
->label('sdk.method', 'deleteKey')
|
|
|
|
->param('keyId', null, function () { return new UID(); }, 'Key unique ID.')
|
|
|
|
->action(
|
2019-12-16 07:56:44 +13:00
|
|
|
function ($keyId) use ($response, $consoleDB, $project) {
|
2019-12-15 08:33:29 +13:00
|
|
|
$key = $project->search('$uid', $keyId, $project->getAttribute('keys', []));
|
|
|
|
|
|
|
|
if (empty($key) && $key instanceof Document) {
|
|
|
|
throw new Exception('Key not found', 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$consoleDB->deleteDocument($key->getUid())) {
|
|
|
|
throw new Exception('Failed to remove key from DB', 500);
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->noContent();
|
|
|
|
}
|
|
|
|
);
|