diff --git a/src/Appwrite/Auth/OAuth2/Oidc.php b/src/Appwrite/Auth/OAuth2/Oidc.php index c9810c48eb..d5b54dfc59 100644 --- a/src/Appwrite/Auth/OAuth2/Oidc.php +++ b/src/Appwrite/Auth/OAuth2/Oidc.php @@ -38,18 +38,39 @@ class Oidc extends OAuth2 protected array $wellKnownConfiguration = []; + /** + * Memoized decode of the JSON stored in appSecret. Building a single login + * URL reads the secret several times (prompt, max_age, each endpoint), so + * decode once and reuse. + * + * @var array|null + */ + protected ?array $decodedAppSecret = null; + /** * @return string */ public function getLoginURL(): string { - return $this->getAuthorizationEndpoint() . '?' . \http_build_query([ + $params = [ 'client_id' => $this->appID, 'redirect_uri' => $this->callback, 'state' => \json_encode($this->state), 'scope' => \implode(' ', $this->getScopes()), 'response_type' => 'code', - ]); + ]; + + $prompt = $this->getPrompt(); + if (!empty($prompt)) { + $params['prompt'] = $prompt; + } + + $maxAge = $this->getMaxAge(); + if ($maxAge !== null) { + $params['max_age'] = $maxAge; + } + + return $this->getAuthorizationEndpoint() . '?' . \http_build_query($params); } /** @@ -196,6 +217,40 @@ class Oidc extends OAuth2 return $secret['clientSecret'] ?? ''; } + /** + * Extracts the prompt values from the JSON stored in appSecret. + * + * @return string + */ + protected function getPrompt(): string + { + $secret = $this->getAppSecret(); + $prompt = $secret['prompt'] ?? []; + + if (!\is_array($prompt)) { + $prompt = [$prompt]; + } + + return \implode(' ', $prompt); + } + + /** + * Extracts the max_age value from the JSON stored in appSecret. + * + * @return int|null + */ + protected function getMaxAge(): ?int + { + $secret = $this->getAppSecret(); + $maxAge = $secret['maxAge'] ?? null; + + if ($maxAge === null || $maxAge === '') { + return null; + } + + return (int) $maxAge; + } + /** * Extracts the well known endpoint from the JSON stored in appSecret. * @@ -257,7 +312,9 @@ class Oidc extends OAuth2 protected function getUserinfoEndpoint(): string { $secret = $this->getAppSecret(); - $endpoint = $secret['userinfoEndpoint'] ?? ''; + // Read the legacy lowercase `userinfoEndpoint` key as a fallback so that + // OIDC configs stored via the old generic oauth2 PATCH endpoint keep working. + $endpoint = $secret['userInfoEndpoint'] ?? $secret['userinfoEndpoint'] ?? ''; if (!empty($endpoint)) { return $endpoint; } @@ -289,11 +346,16 @@ class Oidc extends OAuth2 */ protected function getAppSecret(): array { + if ($this->decodedAppSecret !== null) { + return $this->decodedAppSecret; + } + try { $secret = \json_decode($this->appSecret, true, 512, JSON_THROW_ON_ERROR); } catch (\Throwable $th) { throw new \Exception('Invalid secret'); } - return $secret; + + return $this->decodedAppSecret = \is_array($secret) ? $secret : []; } } diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Oidc/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Oidc/Update.php index 05ad535899..dbf18ad6f0 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Oidc/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Oidc/Update.php @@ -14,10 +14,14 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; +use Utopia\Platform\Enum; +use Utopia\Validator\ArrayList; use Utopia\Validator\Boolean; use Utopia\Validator\Nullable; +use Utopia\Validator\Range; use Utopia\Validator\Text; use Utopia\Validator\URL; +use Utopia\Validator\WhiteList; class Update extends Base { @@ -93,6 +97,18 @@ class Update extends Base 'example' => 'https://myoauth.com/oauth2/userinfo', 'hint' => '', ], + [ + '$id' => 'prompt', + 'name' => 'Prompt', + 'example' => '["consent"]', + 'hint' => '', + ], + [ + '$id' => 'maxAge', + 'name' => 'Max Age', + 'example' => '3600', + 'hint' => '', + ], ]); } @@ -129,6 +145,8 @@ class Update extends Base ->param('authorizationURL', null, new Nullable(new URL(allowEmpty: true)), 'OpenID Connect authorization endpoint URL. Required when wellKnownURL is not provided. For example: https://myoauth.com/oauth2/authorize', optional: true) ->param('tokenURL', null, new Nullable(new URL(allowEmpty: true)), 'OpenID Connect token endpoint URL. Required when wellKnownURL is not provided. For example: https://myoauth.com/oauth2/token', optional: true, aliases: ['tokenUrl']) ->param('userInfoURL', null, new Nullable(new URL(allowEmpty: true)), 'OpenID Connect user info endpoint URL. Required when wellKnownURL is not provided. For example: https://myoauth.com/oauth2/userinfo', optional: true, aliases: ['userInfoUrl']) + ->param('prompt', null, new Nullable(new ArrayList(new WhiteList(['none', 'login', 'consent', 'select_account'], true), 4)), 'Array of OpenID Connect prompt values controlling the authentication and consent screens. If "none" is included, it must be the only element. "none" means: don\'t display any authentication or consent screens. "login" means: prompt the user to re-authenticate. "consent" means: prompt the user for consent. "select_account" means: prompt the user to select an account.', optional: true, enum: new Enum(name: 'ProjectOAuth2OidcPrompt')) + ->param('maxAge', null, new Nullable(new Range(0, PHP_INT_MAX, Range::TYPE_INTEGER)), 'Maximum authentication age in seconds. When set, the user must have authenticated within this many seconds, otherwise they are prompted to re-authenticate.', optional: true) ->param('enabled', null, new Nullable(new Boolean()), 'OAuth2 sign-in method status. Set to true to enable new session creation. Setting to true will trigger end-to-end credentials validation, and will throw if the credentials are invalid.', true) ->inject('response') ->inject('dbForPlatform') @@ -153,6 +171,8 @@ class Update extends Base 'authorizationURL' => $decoded['authorizationEndpoint'] ?? '', 'tokenURL' => $decoded['tokenEndpoint'] ?? '', 'userInfoURL' => $decoded['userInfoEndpoint'] ?? '', + 'prompt' => $decoded['prompt'] ?? [], + 'maxAge' => $decoded['maxAge'] ?? null, ]); } @@ -176,6 +196,8 @@ class Update extends Base ?string $authorizationURL, ?string $tokenURL, ?string $userInfoURL, + ?array $prompt, + ?int $maxAge, ?bool $enabled, Response $response, Database $dbForPlatform, @@ -186,8 +208,12 @@ class Update extends Base $providerId = static::getProviderId(); $queueForEvents->setParam('providerId', $providerId); + if ($prompt !== null && \in_array('none', $prompt) && \count($prompt) > 1) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'When "none" is used as a prompt value, it must be the only element in the array.'); + } + // The secret is stored as JSON - // `{"clientSecret": "...", "wellKnownEndpoint": "...", "authorizationEndpoint": "...", "tokenEndpoint": "...", "userInfoEndpoint": "..."}` + // `{"clientSecret": "...", "wellKnownEndpoint": "...", "authorizationEndpoint": "...", "tokenEndpoint": "...", "userInfoEndpoint": "...", "prompt": [...], "maxAge": ...}` // so that the OIDC OAuth2 adapter can extract each endpoint individually. // Merge new values with what's already stored so that submitting only a // subset of fields leaves the others untouched. @@ -203,6 +229,8 @@ class Update extends Base 'authorizationEndpoint' => $authorizationURL ?? ($existing['authorizationEndpoint'] ?? ''), 'tokenEndpoint' => $tokenURL ?? ($existing['tokenEndpoint'] ?? ''), 'userInfoEndpoint' => $userInfoURL ?? ($existing['userInfoEndpoint'] ?? ''), + 'prompt' => $prompt ?? ($existing['prompt'] ?? []), + 'maxAge' => $maxAge ?? ($existing['maxAge'] ?? null), ]; // When enabling, require either wellKnownEndpoint alone, or all three diff --git a/src/Appwrite/Utopia/Response/Model/OAuth2Oidc.php b/src/Appwrite/Utopia/Response/Model/OAuth2Oidc.php index 0b18539423..e3b4191496 100644 --- a/src/Appwrite/Utopia/Response/Model/OAuth2Oidc.php +++ b/src/Appwrite/Utopia/Response/Model/OAuth2Oidc.php @@ -53,6 +53,21 @@ class OAuth2Oidc extends OAuth2Base 'description' => 'OpenID Connect user info endpoint URL.', 'default' => '', 'example' => 'https://myoauth.com/oauth2/userinfo', + ]) + ->addRule('prompt', [ + 'type' => self::TYPE_ENUM, + 'description' => 'OpenID Connect prompt values controlling the authentication and consent screens.', + 'default' => [], + 'example' => ['consent'], + 'array' => true, + 'enum' => ['none', 'login', 'consent', 'select_account'], + ]) + ->addRule('maxAge', [ + 'type' => self::TYPE_INTEGER, + 'description' => 'Maximum authentication age in seconds. When set, the user must have authenticated within this many seconds.', + 'default' => null, + 'example' => 3600, + 'required' => false, ]); } diff --git a/tests/e2e/Services/Project/OAuth2Base.php b/tests/e2e/Services/Project/OAuth2Base.php index 1bf607fc29..4f6076e581 100644 --- a/tests/e2e/Services/Project/OAuth2Base.php +++ b/tests/e2e/Services/Project/OAuth2Base.php @@ -19,7 +19,7 @@ trait OAuth2Base 'amazon' => ['clientId' => '', 'clientSecret' => '', 'enabled' => false], 'github' => ['clientId' => '', 'clientSecret' => '', 'enabled' => false], 'apple' => ['serviceId' => '', 'keyId' => '', 'teamId' => '', 'p8File' => '', 'enabled' => false], - 'oidc' => ['clientId' => '', 'clientSecret' => '', 'wellKnownURL' => '', 'authorizationURL' => '', 'tokenURL' => '', 'userInfoURL' => '', 'enabled' => false], + 'oidc' => ['clientId' => '', 'clientSecret' => '', 'wellKnownURL' => '', 'authorizationURL' => '', 'tokenURL' => '', 'userInfoURL' => '', 'prompt' => [], 'enabled' => false], 'okta' => ['clientId' => '', 'clientSecret' => '', 'domain' => '', 'authorizationServerId' => '', 'enabled' => false], 'google' => ['clientId' => '', 'clientSecret' => '', 'prompt' => ['consent'], 'enabled' => false], 'dropbox' => ['appKey' => '', 'appSecret' => '', 'enabled' => false], @@ -404,4 +404,96 @@ trait OAuth2Base $params, ); } + + public function testUpdateOAuth2OidcPromptAndMaxAge(): void + { + $response = $this->updateOAuth2('oidc', [ + 'clientId' => 'oidc-prompt-client', + 'clientSecret' => 'oidc-prompt-secret', + 'wellKnownURL' => 'https://idp.example.com/.well-known/openid-configuration', + 'prompt' => ['login', 'consent'], + 'maxAge' => 3600, + 'enabled' => false, + ]); + + $this->assertSame(200, $response['headers']['status-code']); + $this->assertSame(['login', 'consent'], $response['body']['prompt']); + $this->assertSame(3600, $response['body']['maxAge']); + + // GET reads back prompt + maxAge while hiding the clientSecret. + $get = $this->getOAuth2Provider('oidc'); + $this->assertSame(200, $get['headers']['status-code']); + $this->assertSame(['login', 'consent'], $get['body']['prompt']); + $this->assertSame(3600, $get['body']['maxAge']); + $this->assertSame('', $get['body']['clientSecret']); + + // Cleanup + $this->updateOAuth2('oidc', [ + 'clientId' => '', + 'clientSecret' => '', + 'wellKnownURL' => '', + 'authorizationURL' => '', + 'tokenURL' => '', + 'userInfoURL' => '', + 'prompt' => [], + 'enabled' => false, + ]); + } + + public function testUpdateOAuth2OidcPartialPreservesPromptAndMaxAge(): void + { + // Seed prompt + maxAge. + $this->updateOAuth2('oidc', [ + 'clientId' => 'oidc-seed-client', + 'clientSecret' => 'oidc-seed-secret', + 'wellKnownURL' => 'https://idp.example.com/.well-known/openid-configuration', + 'prompt' => ['select_account'], + 'maxAge' => 120, + 'enabled' => false, + ]); + + // Update only clientId — prompt and maxAge must be preserved. + $response = $this->updateOAuth2('oidc', [ + 'clientId' => 'oidc-rotated-client', + ]); + + $this->assertSame(200, $response['headers']['status-code']); + $this->assertSame('oidc-rotated-client', $response['body']['clientId']); + $this->assertSame(['select_account'], $response['body']['prompt']); + $this->assertSame(120, $response['body']['maxAge']); + + // Cleanup + $this->updateOAuth2('oidc', [ + 'clientId' => '', + 'clientSecret' => '', + 'wellKnownURL' => '', + 'prompt' => [], + 'enabled' => false, + ]); + } + + public function testUpdateOAuth2OidcPromptNoneAloneRejected(): void + { + $response = $this->updateOAuth2('oidc', [ + 'clientId' => 'oidc-prompt-none', + 'clientSecret' => 'oidc-prompt-none-secret', + 'prompt' => ['none', 'consent'], + 'enabled' => false, + ]); + + $this->assertSame(400, $response['headers']['status-code']); + $this->assertSame('general_argument_invalid', $response['body']['type']); + } + + public function testUpdateOAuth2OidcMaxAgeNegativeRejected(): void + { + $response = $this->updateOAuth2('oidc', [ + 'clientId' => 'oidc-maxage-negative', + 'clientSecret' => 'oidc-maxage-negative-secret', + 'maxAge' => -1, + 'enabled' => false, + ]); + + $this->assertSame(400, $response['headers']['status-code']); + } } diff --git a/tests/unit/Platform/Modules/Project/OAuth2/OAuth2ProviderTest.php b/tests/unit/Platform/Modules/Project/OAuth2/OAuth2ProviderTest.php index b8eb48f748..4c66222256 100644 --- a/tests/unit/Platform/Modules/Project/OAuth2/OAuth2ProviderTest.php +++ b/tests/unit/Platform/Modules/Project/OAuth2/OAuth2ProviderTest.php @@ -91,7 +91,7 @@ final class OAuth2ProviderTest extends TestCase yield 'google' => [GoogleUpdate::class, ['clientId', 'clientSecret', 'prompt']]; yield 'keycloak' => [KeycloakUpdate::class, ['clientId', 'clientSecret', 'endpoint', 'realmName']]; yield 'microsoft' => [MicrosoftUpdate::class, ['applicationId', 'applicationSecret', 'tenant']]; - yield 'oidc' => [OidcUpdate::class, ['clientId', 'clientSecret', 'wellKnownURL', 'authorizationURL', 'tokenURL', 'userInfoURL']]; + yield 'oidc' => [OidcUpdate::class, ['clientId', 'clientSecret', 'wellKnownURL', 'authorizationURL', 'tokenURL', 'userInfoURL', 'prompt', 'maxAge']]; yield 'okta' => [OktaUpdate::class, ['clientId', 'clientSecret', 'domain', 'authorizationServerId']]; } @@ -347,6 +347,8 @@ final class OAuth2ProviderTest extends TestCase null, null, null, + null, + null, true, ]); @@ -365,6 +367,8 @@ final class OAuth2ProviderTest extends TestCase 'https://idp.example/oauth2/authorize', 'https://idp.example/oauth2/token', 'https://idp.example/oauth2/userinfo', + null, + null, true, ]); @@ -389,6 +393,8 @@ final class OAuth2ProviderTest extends TestCase 'https://idp.example/oauth2/authorize', null, 'https://idp.example/oauth2/userinfo', + null, + null, true, ], assertQueueParam: false, @@ -417,6 +423,8 @@ final class OAuth2ProviderTest extends TestCase 'https://idp.example/oauth2/authorize', 'https://idp.example/oauth2/token', 'https://idp.example/oauth2/userinfo', + null, + null, true, ], );