apiKeyAllowed = false; $this->loginRequired = true; } public function _execute(): bool { $sql = $this->context->getSQL(); $currentUser = $this->context->getUser(); $apiKey = ApiKey::create($currentUser); $this->success = $apiKey->save($sql); $this->lastError = $sql->getLastError(); if ($this->success) { $this->result["apiKey"] = $apiKey->jsonSerialize( ["id", "validUntil", "token", "active"] ); } return $this->success; } public static function getDefaultACL(Insert $insert): void { $insert->addRow(self::getEndpoint(), [], "Allows users to create new API-Keys"); } } class Fetch extends ApiKeyAPI { use Pagination; public function __construct(Context $context, $externalCall = false) { $params = $this->getPaginationParameters(["token", "validUntil", "active"]); $params["showActiveOnly"] = new Parameter("showActiveOnly", Parameter::TYPE_BOOLEAN, true, true); parent::__construct($context, $externalCall, $params); $this->loginRequired = true; } public function _execute(): bool { $sql = $this->context->getSQL(); $condition = new Compare("user_id", $this->context->getUser()->getId()); if ($this->getParam("showActiveOnly")) { $condition = new CondAnd( $condition, new Compare("valid_until", $sql->currentTimestamp(), ">"), new Compare("active", true) ); } if (!$this->initPagination($sql, ApiKey::class, $condition)) { return false; } $apiKeys = $this->createPaginationQuery($sql)->execute(); $this->success = ($apiKeys !== FALSE && $apiKeys !== null); $this->lastError = $sql->getLastError(); if ($this->success) { $this->result["apiKeys"] = []; foreach($apiKeys as $apiKey) { $this->result["apiKeys"][] = $apiKey->jsonSerialize(); } } return $this->success; } public static function getDefaultACL(Insert $insert): void { $insert->addRow(self::getEndpoint(), [], "Allows users to fetch new API-Key"); } } class Refresh extends ApiKeyAPI { public function __construct(Context $context, $externalCall = false) { parent::__construct($context, $externalCall, array( "id" => new Parameter("id", Parameter::TYPE_INT), )); $this->loginRequired = true; } public function _execute(): bool { $sql = $this->context->getSQL(); $id = $this->getParam("id"); $apiKey = ApiKey::find($sql, $id); if ($apiKey === false) { return $this->createError("Error fetching API-Key details: " . $sql->getLastError()); } else if ($apiKey === null) { return $this->createError("API-Key does not exit"); } $this->success = $apiKey->refresh($sql, 30) !== false; $this->lastError = $sql->getLastError(); if ($this->success) { $this->result["validUntil"] = $apiKey->getValidUntil()->getTimestamp(); } return $this->success; } public static function getDefaultACL(Insert $insert): void { $insert->addRow(self::getEndpoint(), [], "Allows users to refresh API-Key"); } } class Revoke extends ApiKeyAPI { public function __construct($user, $externalCall = false) { parent::__construct($user, $externalCall, array( "id" => new Parameter("id", Parameter::TYPE_INT), )); $this->loginRequired = true; } public function _execute(): bool { $sql = $this->context->getSQL(); $id = $this->getParam("id"); $apiKey = ApiKey::find($sql, $id); if ($apiKey === false) { return $this->createError("Error fetching API-Key details: " . $sql->getLastError()); } else if ($apiKey === null) { return $this->createError("API-Key does not exit"); } $this->success = $apiKey->revoke($sql); $this->lastError = $sql->getLastError(); return $this->success; } public static function getDefaultACL(Insert $insert): void { $insert->addRow(self::getEndpoint(), [], "Allows users to revoke API-Key"); } } }