diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 358c96d10d..c282419ec9 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -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(); diff --git a/src/Appwrite/Auth/Key.php b/src/Appwrite/Auth/Key.php index 7cd0ac91b6..2eb5b07edc 100644 --- a/src/Appwrite/Auth/Key.php +++ b/src/Appwrite/Auth/Key.php @@ -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;