2020-06-20 20:13:51 +02:00
|
|
|
<?php
|
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
namespace Core\API {
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
use Core\Objects\Context;
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2020-07-01 21:10:25 +02:00
|
|
|
abstract class ApiKeyAPI extends Request {
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
public function __construct(Context $context, bool $externalCall = false, array $params = array()) {
|
|
|
|
parent::__construct($context, $externalCall, $params);
|
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
namespace Core\API\ApiKey {
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
use Core\API\ApiKeyAPI;
|
|
|
|
use Core\API\Parameter\Parameter;
|
|
|
|
use Core\Driver\SQL\Condition\Compare;
|
|
|
|
use Core\Driver\SQL\Condition\CondAnd;
|
|
|
|
use Core\Objects\Context;
|
|
|
|
use Core\Objects\DatabaseEntity\ApiKey;
|
2020-06-20 20:13:51 +02:00
|
|
|
|
|
|
|
class Create extends ApiKeyAPI {
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
public function __construct(Context $context, $externalCall = false) {
|
|
|
|
parent::__construct($context, $externalCall, array());
|
2020-06-20 20:13:51 +02:00
|
|
|
$this->apiKeyAllowed = false;
|
|
|
|
$this->loginRequired = true;
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2022-06-20 19:52:31 +02:00
|
|
|
$sql = $this->context->getSQL();
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
$apiKey = new ApiKey();
|
|
|
|
$apiKey->apiKey = generateRandomString(64);
|
|
|
|
$apiKey->validUntil = (new \DateTime())->modify("+30 DAY");
|
|
|
|
$apiKey->user = $this->context->getUser();
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
$this->success = $apiKey->save($sql);
|
2020-06-20 20:13:51 +02:00
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
|
|
|
|
if ($this->success) {
|
2022-06-20 19:52:31 +02:00
|
|
|
$this->result["api_key"] = $apiKey->jsonSerialize();
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
2022-02-20 16:53:26 +01:00
|
|
|
|
2020-06-20 20:13:51 +02:00
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Fetch extends ApiKeyAPI {
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
public function __construct(Context $context, $externalCall = false) {
|
|
|
|
parent::__construct($context, $externalCall, array(
|
2022-02-20 16:53:26 +01:00
|
|
|
"showActiveOnly" => new Parameter("showActiveOnly", Parameter::TYPE_BOOLEAN, true, true)
|
|
|
|
));
|
2020-06-20 20:13:51 +02:00
|
|
|
$this->loginRequired = true;
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2022-06-20 19:52:31 +02:00
|
|
|
$sql = $this->context->getSQL();
|
2022-02-20 16:53:26 +01:00
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
$condition = new Compare("user_id", $this->context->getUser()->getId());
|
2022-02-20 16:53:26 +01:00
|
|
|
if ($this->getParam("showActiveOnly")) {
|
2022-06-20 19:52:31 +02:00
|
|
|
$condition = new CondAnd(
|
|
|
|
$condition,
|
|
|
|
new Compare("valid_until", $sql->currentTimestamp(), ">"),
|
|
|
|
new Compare("active", true)
|
|
|
|
);
|
2022-02-20 16:53:26 +01:00
|
|
|
}
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
$apiKeys = ApiKey::findAll($sql, $condition);
|
|
|
|
$this->success = ($apiKeys !== FALSE);
|
2020-06-20 20:13:51 +02:00
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
if ($this->success) {
|
2020-06-20 20:13:51 +02:00
|
|
|
$this->result["api_keys"] = array();
|
2022-06-20 19:52:31 +02:00
|
|
|
foreach($apiKeys as $apiKey) {
|
|
|
|
$this->result["api_keys"][$apiKey->getId()] = $apiKey->jsonSerialize();
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Refresh extends ApiKeyAPI {
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
public function __construct(Context $context, $externalCall = false) {
|
|
|
|
parent::__construct($context, $externalCall, array(
|
2020-06-20 20:13:51 +02:00
|
|
|
"id" => new Parameter("id", Parameter::TYPE_INT),
|
|
|
|
));
|
|
|
|
$this->loginRequired = true;
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2022-11-29 14:17:11 +01:00
|
|
|
$sql = $this->context->getSQL();
|
2020-06-20 20:13:51 +02:00
|
|
|
$id = $this->getParam("id");
|
2022-11-29 14:17:11 +01:00
|
|
|
$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");
|
2022-06-20 19:52:31 +02:00
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2022-11-29 14:17:11 +01:00
|
|
|
$this->success = $apiKey->refresh($sql, 30) !== false;
|
2020-06-20 20:13:51 +02:00
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
|
|
|
|
if ($this->success) {
|
2022-11-29 14:17:11 +01:00
|
|
|
$this->result["validUntil"] = $apiKey->getValidUntil()->getTimestamp();
|
2020-06-20 20:13:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2022-11-29 14:17:11 +01:00
|
|
|
$sql = $this->context->getSQL();
|
2020-06-20 20:13:51 +02:00
|
|
|
$id = $this->getParam("id");
|
2022-11-29 14:17:11 +01:00
|
|
|
$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");
|
2022-06-20 19:52:31 +02:00
|
|
|
}
|
2020-06-20 20:13:51 +02:00
|
|
|
|
2022-11-29 14:17:11 +01:00
|
|
|
$this->success = $apiKey->revoke($sql);
|
2020-06-20 20:13:51 +02:00
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|