small changes

This commit is contained in:
2023-01-18 14:37:34 +01:00
parent 327f570316
commit 136ad48a5e
13 changed files with 148 additions and 56 deletions

View File

@@ -16,6 +16,7 @@ namespace Core\API\ApiKey {
use Core\API\ApiKeyAPI;
use Core\API\Parameter\Parameter;
use Core\API\Traits\Pagination;
use Core\Driver\SQL\Condition\Compare;
use Core\Driver\SQL\Condition\CondAnd;
use Core\Driver\SQL\Query\Insert;
@@ -32,17 +33,16 @@ namespace Core\API\ApiKey {
public function _execute(): bool {
$sql = $this->context->getSQL();
$currentUser = $this->context->getUser();
$apiKey = new ApiKey();
$apiKey->apiKey = generateRandomString(64);
$apiKey->validUntil = (new \DateTime())->modify("+30 DAY");
$apiKey->user = $this->context->getUser();
$apiKey = ApiKey::create($currentUser);
$this->success = $apiKey->save($sql);
$this->lastError = $sql->getLastError();
if ($this->success) {
$this->result["api_key"] = $apiKey->jsonSerialize();
$this->result["apiKey"] = $apiKey->jsonSerialize(
["id", "validUntil", "token", "active"]
);
}
return $this->success;
@@ -55,10 +55,13 @@ namespace Core\API\ApiKey {
class Fetch extends ApiKeyAPI {
use Pagination;
public function __construct(Context $context, $externalCall = false) {
parent::__construct($context, $externalCall, array(
"showActiveOnly" => new Parameter("showActiveOnly", Parameter::TYPE_BOOLEAN, true, true)
));
$params = $this->getPaginationParameters(["token", "validUntil", "active"]);
$params["showActiveOnly"] = new Parameter("showActiveOnly", Parameter::TYPE_BOOLEAN, true, true);
parent::__construct($context, $externalCall, $params);
$this->loginRequired = true;
}
@@ -74,14 +77,18 @@ namespace Core\API\ApiKey {
);
}
$apiKeys = ApiKey::findAll($sql, $condition);
$this->success = ($apiKeys !== FALSE);
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["api_keys"] = array();
$this->result["apiKeys"] = [];
foreach($apiKeys as $apiKey) {
$this->result["api_keys"][$apiKey->getId()] = $apiKey->jsonSerialize();
$this->result["apiKeys"][] = $apiKey->jsonSerialize();
}
}

View File

@@ -20,7 +20,7 @@ namespace Core\API {
$settings = $req->getResult()["settings"];
if (!isset($settings["mail_enabled"]) || $settings["mail_enabled"] !== "1") {
$this->createError("Mail is not configured yet.");
$this->createError("Mailing is not configured on this server yet.");
return null;
}

View File

@@ -252,13 +252,18 @@ namespace Core\API\TFA {
// $domain = "localhost";
if (!$clientDataJSON || !$attestationObjectRaw) {
$challenge = null;
if ($twoFactorToken) {
if (!($twoFactorToken instanceof KeyBasedTwoFactorToken) || $twoFactorToken->isConfirmed()) {
if ($twoFactorToken->isConfirmed()) {
return $this->createError("You already added a two factor token");
} else {
} else if ($twoFactorToken instanceof KeyBasedTwoFactorToken) {
$challenge = $twoFactorToken->getChallenge();
} else {
$twoFactorToken->delete($sql);
}
} else {
}
if ($challenge === null) {
$twoFactorToken = KeyBasedTwoFactorToken::create();
$challenge = $twoFactorToken->getChallenge();
$this->success = ($twoFactorToken->save($sql) !== false);
@@ -307,6 +312,10 @@ namespace Core\API\TFA {
$this->success = $twoFactorToken->confirmKeyBased($sql, base64_encode($authData->getCredentialID()), $publicKey) !== false;
$this->lastError = $sql->getLastError();
if ($this->success) {
$this->result["twoFactorToken"] = $twoFactorToken->jsonSerialize();
}
}
return $this->success;

View File

@@ -1223,6 +1223,8 @@ namespace Core\API\User {
$gpgKey = $currentUser->getGPG();
if ($gpgKey) {
return $this->createError("You already added a GPG key to your account.");
} else if (!$currentUser->getEmail()) {
return $this->createError("You do not have an e-mail address");
}
// fix key first, enforce a newline after
@@ -1280,7 +1282,7 @@ namespace Core\API\User {
if ($this->success) {
$currentUser->gpgKey = $gpgKey;
if ($currentUser->save($sql, ["gpgKey"])) {
$this->result["gpg"] = $gpgKey->jsonSerialize();
$this->result["gpgKey"] = $gpgKey->jsonSerialize();
} else {
return $this->createError("Error updating user details: " . $sql->getLastError());
}

View File

@@ -149,7 +149,7 @@ abstract class SQL {
return false;
}
$logLevel = Logger::LOG_LEVEL_DEBUG;
$logLevel = Logger::LOG_LEVEL_ERROR;
if ($query instanceof Insert && $query->getTableName() === "SystemLog") {
$logLevel = Logger::LOG_LEVEL_NONE;
}

View File

@@ -44,4 +44,5 @@ return [
"confirm_error" => "Fehler beim Bestätigen der E-Mail Adresse",
"gpg_key" => "GPG-Schlüssel",
"2fa_token" => "Zwei-Faktor Authentifizierung (2FA)",
"profile_picture_of" => "Profilbild von",
];

View File

@@ -44,4 +44,5 @@ return [
"confirm_error" => "Error confirming e-mail address",
"gpg_key" => "GPG Key",
"2fa_token" => "Two-Factor Authentication (2FA)",
"profile_picture_of" => "Profile Picture of",
];

View File

@@ -9,19 +9,23 @@ use Core\Objects\DatabaseEntity\Controller\DatabaseEntity;
class ApiKey extends DatabaseEntity {
private bool $active;
#[MaxLength(64)] public String $apiKey;
#[MaxLength(64)] public String $token;
public \DateTime $validUntil;
public User $user;
public function __construct(?int $id = null) {
parent::__construct($id);
$this->active = true;
}
public function getValidUntil(): \DateTime {
return $this->validUntil;
}
public static function create(User $user, int $days = 30): ApiKey {
$apiKey = new ApiKey();
$apiKey->user = $user;
$apiKey->token = generateRandomString(64);
$apiKey->validUntil = (new \DateTime())->modify("+$days days");
$apiKey->active = true;
return $apiKey;
}
public function refresh(SQL $sql, int $days): bool {
$this->validUntil = (new \DateTime())->modify("+$days days");
return $this->save($sql, ["validUntil"]);

View File

@@ -43,7 +43,7 @@ function uuidv4(): string {
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
function generateRandomString($length, $type = "ascii"): string {
function generateRandomString(int $length, $type = "ascii"): string {
$randomString = '';
$lowercase = "abcdefghijklmnopqrstuvwxyz";