Add expired property directly to key

This commit is contained in:
Jake Barnby
2025-02-12 21:22:08 +13:00
parent 775e6ec9cf
commit 9427b07580
2 changed files with 19 additions and 7 deletions
+7 -4
View File
@@ -203,12 +203,15 @@ App::init()
$scopes = $roles[$role]['scopes'];
if (!empty($apiKey) && !$user->isEmpty()) {
throw new Exception(Exception::USER_API_KEY_AND_SESSION_SET);
}
// API Key authentication
if (!empty($apiKey)) {
if (!$user->isEmpty()) {
throw new Exception(Exception::USER_API_KEY_AND_SESSION_SET);
}
if ($apiKey->isExpired()) {
throw new Exception(Exception::PROJECT_KEY_EXPIRED);
}
$role = $apiKey->getRole();
$scopes = $apiKey->getScopes();
+12 -3
View File
@@ -18,6 +18,7 @@ class Key
protected string $role,
protected array $scopes,
protected string $name,
protected bool $expired = false,
protected bool $usage = true,
) {
}
@@ -47,6 +48,11 @@ class Key
return $this->name;
}
public function isExpired(): bool
{
return $this->expired;
}
public function isUsageEnabled(): bool
{
return $this->usage;
@@ -75,6 +81,7 @@ class Key
$role = Auth::USER_ROLE_APPS;
$roles = Config::getParam('roles', []);
$scopes = $roles[Auth::USER_ROLE_APPS]['scopes'] ?? [];
$expired = false;
$guestKey = new Key(
$project->getId(),
@@ -96,7 +103,7 @@ class Key
try {
$payload = $jwtObj->decode($secret);
} catch (JWTException) {
throw new Exception(Exception::API_KEY_EXPIRED);
$expired = true;
}
$name = $payload['name'] ?? 'Dynamic Key';
@@ -114,6 +121,7 @@ class Key
$role,
$scopes,
$name,
$expired,
$usage
);
case API_KEY_STANDARD:
@@ -129,7 +137,7 @@ class Key
$expire = $key->getAttribute('expire');
if (!empty($expire) && $expire < DateTime::formatTz(DateTime::now())) {
throw new Exception(Exception::PROJECT_KEY_EXPIRED);
$expired = true;
}
$name = $key->getAttribute('name', 'UNKNOWN');
@@ -140,7 +148,8 @@ class Key
$type,
$role,
$scopes,
$name
$name,
$expired
);
default:
return $guestKey;