web-base/Core/API/ApiKeyAPI.class.php

166 lines
4.7 KiB
PHP
Raw Normal View History

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\Driver\SQL\Condition\Compare;
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);
}
protected function apiKeyExists(int $id): bool {
$sql = $this->context->getSQL();
2020-06-20 20:13:51 +02:00
$res = $sql->select($sql->count())
->from("ApiKey")
2022-06-20 19:52:31 +02:00
->where(new Compare("id", $id))
->where(new Compare("user_id", $this->context->getUser()->getId()))
2020-06-20 20:13:51 +02:00
->where(new Compare("valid_until", $sql->currentTimestamp(), ">"))
->where(new Compare("active", 1))
->execute();
$this->success = ($res !== FALSE);
$this->lastError = $sql->getLastError();
if($this->success && $res[0]["count"] === 0) {
return $this->createError("This API-Key does not exist.");
}
return $this->success;
}
}
}
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 {
2020-06-20 20:13:51 +02:00
$id = $this->getParam("id");
2022-06-20 19:52:31 +02:00
if (!$this->apiKeyExists($id)) {
2020-06-20 20:13:51 +02:00
return false;
2022-06-20 19:52:31 +02:00
}
2020-06-20 20:13:51 +02:00
$validUntil = (new \DateTime)->modify("+30 DAY");
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2020-06-20 20:13:51 +02:00
$this->success = $sql->update("ApiKey")
->set("valid_until", $validUntil)
2022-06-20 19:52:31 +02:00
->where(new Compare("id", $id))
->where(new Compare("user_id", $this->context->getUser()->getId()))
2020-06-20 20:13:51 +02:00
->execute();
$this->lastError = $sql->getLastError();
if ($this->success) {
2022-02-20 16:53:26 +01:00
$this->result["valid_until"] = $validUntil;
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 {
2020-06-20 20:13:51 +02:00
$id = $this->getParam("id");
2022-06-20 19:52:31 +02:00
if (!$this->apiKeyExists($id)) {
2020-06-20 20:13:51 +02:00
return false;
2022-06-20 19:52:31 +02:00
}
2020-06-20 20:13:51 +02:00
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2020-06-20 20:13:51 +02:00
$this->success = $sql->update("ApiKey")
->set("active", false)
2022-06-20 19:52:31 +02:00
->where(new Compare("id", $id))
->where(new Compare("user_id", $this->context->getUser()->getId()))
2020-06-20 20:13:51 +02:00
->execute();
$this->lastError = $sql->getLastError();
return $this->success;
}
}
}