diff --git a/app/config/oAuthProviders.php b/app/config/oAuthProviders.php index 3b492fd8bf..31f1585b5c 100644 --- a/app/config/oAuthProviders.php +++ b/app/config/oAuthProviders.php @@ -24,6 +24,17 @@ return [ 'mock' => false, 'class' => 'Appwrite\\Auth\\OAuth2\\Apple', ], + 'appwrite' => [ + 'name' => 'Appwrite', + 'developers' => 'https://appwrite.io/docs/products/auth', + 'icon' => 'icon-appwrite', + 'enabled' => true, + 'sandbox' => false, + 'form' => false, + 'beta' => false, + 'mock' => false, + 'class' => 'Appwrite\\Auth\\OAuth2\\Appwrite', + ], 'auth0' => [ 'name' => 'Auth0', 'developers' => 'https://auth0.com/developers', diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index bde3ff427b..e331e3f882 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -190,6 +190,24 @@ $createSession = function (string $userId, string $secret, Request $request, Res $detector->getDevice() )); + if ($verifiedToken->getAttribute('type') === TOKEN_TYPE_OAUTH2) { + $identity = $authorization->skip(fn () => $dbForProject->findOne('identities', [ + Query::equal('provider', [$oauthProvider]), + Query::equal('userInternalId', [$user->getSequence()]), + ])); + + if ($identity->isEmpty()) { + throw new Exception(Exception::USER_INVALID_TOKEN); + } + + $session + ->setAttribute('providerUid', $identity->getAttribute('providerUid')) + ->setAttribute('providerRefreshToken', $identity->getAttribute('providerRefreshToken')) + ->setAttribute('providerAccessToken', $identity->getAttribute('providerAccessToken')) + ->setAttribute('providerAccessTokenExpiry', $identity->getAttribute('providerAccessTokenExpiry')) + ->setAttribute('factors', \array_merge($session->getAttribute('factors', []), ['oauth2'])); + } + $authorization->addRole(Role::user($user->getId())->toString()); $session = $dbForProject->createDocument('sessions', $session diff --git a/app/init/models.php b/app/init/models.php index cdb2a9b9b6..05136566d2 100644 --- a/app/init/models.php +++ b/app/init/models.php @@ -118,6 +118,7 @@ use Appwrite\Utopia\Response\Model\None; use Appwrite\Utopia\Response\Model\Notification; use Appwrite\Utopia\Response\Model\OAuth2Amazon; use Appwrite\Utopia\Response\Model\OAuth2Apple; +use Appwrite\Utopia\Response\Model\OAuth2Appwrite; use Appwrite\Utopia\Response\Model\OAuth2Auth0; use Appwrite\Utopia\Response\Model\OAuth2Authentik; use Appwrite\Utopia\Response\Model\OAuth2Autodesk; @@ -454,6 +455,7 @@ Response::setModel(new OAuth2Facebook()); Response::setModel(new OAuth2Tradeshift()); Response::setModel(new OAuth2Paypal()); Response::setModel(new OAuth2Gitlab()); +Response::setModel(new OAuth2Appwrite()); Response::setModel(new OAuth2Authentik()); Response::setModel(new OAuth2Auth0()); Response::setModel(new OAuth2FusionAuth()); diff --git a/composer.json b/composer.json index 0439d13034..a9d1a8e6f1 100644 --- a/composer.json +++ b/composer.json @@ -83,7 +83,7 @@ "utopia-php/lock": "0.2.*", "utopia-php/logger": "0.8.*", "utopia-php/messaging": "^1.2", - "utopia-php/migration": "1.16.*", + "utopia-php/migration": "1.17.*", "utopia-php/platform": "^1.0@RC", "utopia-php/pools": "1.*", "utopia-php/span": "3.0.*", diff --git a/composer.lock b/composer.lock index d337d7e4ea..224a249486 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "50b6e587a70f02a5896790012248f50f", + "content-hash": "d67184492e8ec8670d0e31f02d10b421", "packages": [ { "name": "adhocore/jwt", @@ -4409,16 +4409,16 @@ }, { "name": "utopia-php/migration", - "version": "1.16.0", + "version": "1.17.2", "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "3bb4428e4fb7a29f120f73f87d0a2519a2a414a4" + "reference": "80f7f49e79ad57a0b5200b043e2b1bfeade8a190" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/3bb4428e4fb7a29f120f73f87d0a2519a2a414a4", - "reference": "3bb4428e4fb7a29f120f73f87d0a2519a2a414a4", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/80f7f49e79ad57a0b5200b043e2b1bfeade8a190", + "reference": "80f7f49e79ad57a0b5200b043e2b1bfeade8a190", "shasum": "" }, "require": { @@ -4458,9 +4458,9 @@ ], "support": { "issues": "https://github.com/utopia-php/migration/issues", - "source": "https://github.com/utopia-php/migration/tree/1.16.0" + "source": "https://github.com/utopia-php/migration/tree/1.17.2" }, - "time": "2026-07-09T05:46:44+00:00" + "time": "2026-07-09T19:53:43+00:00" }, { "name": "utopia-php/mongo", diff --git a/src/Appwrite/Auth/OAuth2/Appwrite.php b/src/Appwrite/Auth/OAuth2/Appwrite.php new file mode 100644 index 0000000000..827b4da540 --- /dev/null +++ b/src/Appwrite/Auth/OAuth2/Appwrite.php @@ -0,0 +1,236 @@ +state; + $state[self::PKCE_STATE_KEY] = $this->getPKCEVerifier(); + + return $this->endpoint . '/authorize?' . \http_build_query([ + 'client_id' => $this->appID, + 'redirect_uri' => $this->callback, + 'state' => \json_encode($state), + 'scope' => \implode(' ', $this->getScopes()), + 'response_type' => 'code', + 'code_challenge' => $this->getPKCEChallenge(), + 'code_challenge_method' => 'S256', + ]); + } + + /** + * @param string $code + * + * @return array + */ + protected function getTokens(string $code): array + { + if (empty($this->tokens)) { + $this->tokens = \json_decode($this->request( + 'POST', + $this->endpoint . '/token', + ['Content-Type: application/x-www-form-urlencoded'], + \http_build_query([ + 'code' => $code, + 'client_id' => $this->appID, + 'client_secret' => $this->appSecret, + 'redirect_uri' => $this->callback, + 'scope' => \implode(' ', $this->getScopes()), + 'grant_type' => 'authorization_code', + 'code_verifier' => $this->getPKCEVerifier(), + ]) + ), true); + } + + return $this->tokens; + } + + /** + * @param string $refreshToken + * + * @return array + */ + public function refreshTokens(string $refreshToken): array + { + $this->tokens = \json_decode($this->request( + 'POST', + $this->endpoint . '/token', + ['Content-Type: application/x-www-form-urlencoded'], + \http_build_query([ + 'refresh_token' => $refreshToken, + 'client_id' => $this->appID, + 'client_secret' => $this->appSecret, + 'grant_type' => 'refresh_token' + ]) + ), true); + + if (empty($this->tokens['refresh_token'])) { + $this->tokens['refresh_token'] = $refreshToken; + } + + return $this->tokens; + } + + /** + * @param string $accessToken + * + * @return string + */ + public function getUserID(string $accessToken): string + { + $user = $this->getUser($accessToken); + + return $user['sub'] ?? ''; + } + + /** + * @param string $accessToken + * + * @return string + */ + public function getUserEmail(string $accessToken): string + { + $user = $this->getUser($accessToken); + + return $user['email'] ?? ''; + } + + /** + * Check if the User email is verified + * + * @param string $accessToken + * + * @return bool + */ + public function isEmailVerified(string $accessToken): bool + { + $user = $this->getUser($accessToken); + + return $user['email_verified'] ?? false; + } + + /** + * @param string $accessToken + * + * @return string + */ + public function getUserName(string $accessToken): string + { + $user = $this->getUser($accessToken); + + return $user['name'] ?? ''; + } + + /** + * @param string $accessToken + * + * @return array + */ + protected function getUser(string $accessToken): array + { + if (empty($this->user)) { + $user = $this->request( + 'GET', + $this->endpoint . '/userinfo', + ['Authorization: Bearer ' . \urlencode($accessToken)] + ); + $this->user = \json_decode($user, true); + } + + return $this->user; + } + + /** + * Extract the PKCE verifier from the state on the callback so the same + * value generated in getLoginURL() can be sent to the token endpoint. + * + * @param string $state + * + * @return array|null + */ + public function parseState(string $state): ?array + { + $parsed = \json_decode($state, true); + + if (!\is_array($parsed)) { + return null; + } + + $verifier = $parsed[self::PKCE_STATE_KEY] ?? null; + if (\is_string($verifier)) { + $this->pkceVerifier = $verifier; + } + + unset($parsed[self::PKCE_STATE_KEY]); + + return $parsed; + } + + private function getPKCEVerifier(): string + { + if ($this->pkceVerifier === '') { + $this->pkceVerifier = \rtrim(\strtr(\base64_encode(\random_bytes(64)), '+/', '-_'), '='); + } + + return $this->pkceVerifier; + } + + private function getPKCEChallenge(): string + { + return \rtrim(\strtr(\base64_encode(\hash('sha256', $this->getPKCEVerifier(), true)), '+/', '-_'), '='); + } +} diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Appwrite/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Appwrite/Update.php new file mode 100644 index 0000000000..3f043825fe --- /dev/null +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Appwrite/Update.php @@ -0,0 +1,55 @@ + Paypal\Update::class, 'paypalSandbox' => PaypalSandbox\Update::class, 'gitlab' => Gitlab\Update::class, + 'appwrite' => Appwrite\Update::class, 'authentik' => Authentik\Update::class, 'auth0' => Auth0\Update::class, 'fusionauth' => FusionAuth\Update::class, diff --git a/src/Appwrite/Platform/Modules/Project/Services/Http.php b/src/Appwrite/Platform/Modules/Project/Services/Http.php index a8eec3abd0..2ffa9eed8b 100644 --- a/src/Appwrite/Platform/Modules/Project/Services/Http.php +++ b/src/Appwrite/Platform/Modules/Project/Services/Http.php @@ -20,6 +20,7 @@ use Appwrite\Platform\Modules\Project\Http\Project\MockPhone\Update as UpdateMoc use Appwrite\Platform\Modules\Project\Http\Project\MockPhone\XList as ListMockPhones; use Appwrite\Platform\Modules\Project\Http\Project\OAuth2\Amazon\Update as UpdateOAuth2Amazon; use Appwrite\Platform\Modules\Project\Http\Project\OAuth2\Apple\Update as UpdateOAuth2Apple; +use Appwrite\Platform\Modules\Project\Http\Project\OAuth2\Appwrite\Update as UpdateOAuth2Appwrite; use Appwrite\Platform\Modules\Project\Http\Project\OAuth2\Auth0\Update as UpdateOAuth2Auth0; use Appwrite\Platform\Modules\Project\Http\Project\OAuth2\Authentik\Update as UpdateOAuth2Authentik; use Appwrite\Platform\Modules\Project\Http\Project\OAuth2\Autodesk\Update as UpdateOAuth2Autodesk; @@ -216,6 +217,7 @@ class Http extends Service $this->addAction(UpdateOAuth2Paypal::getName(), new UpdateOAuth2Paypal()); $this->addAction(UpdateOAuth2PaypalSandbox::getName(), new UpdateOAuth2PaypalSandbox()); $this->addAction(UpdateOAuth2Gitlab::getName(), new UpdateOAuth2Gitlab()); + $this->addAction(UpdateOAuth2Appwrite::getName(), new UpdateOAuth2Appwrite()); $this->addAction(UpdateOAuth2Authentik::getName(), new UpdateOAuth2Authentik()); $this->addAction(UpdateOAuth2Auth0::getName(), new UpdateOAuth2Auth0()); $this->addAction(UpdateOAuth2FusionAuth::getName(), new UpdateOAuth2FusionAuth()); diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index e6b713d530..19b5f4d120 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -325,6 +325,7 @@ class Response extends SwooleResponse public const MODEL_OAUTH2_TRADESHIFT = 'oAuth2Tradeshift'; public const MODEL_OAUTH2_PAYPAL = 'oAuth2Paypal'; public const MODEL_OAUTH2_GITLAB = 'oAuth2Gitlab'; + public const MODEL_OAUTH2_APPWRITE = 'oAuth2Appwrite'; public const MODEL_OAUTH2_AUTHENTIK = 'oAuth2Authentik'; public const MODEL_OAUTH2_AUTH0 = 'oAuth2Auth0'; public const MODEL_OAUTH2_FUSIONAUTH = 'oAuth2FusionAuth'; diff --git a/src/Appwrite/Utopia/Response/Model/OAuth2Appwrite.php b/src/Appwrite/Utopia/Response/Model/OAuth2Appwrite.php new file mode 100644 index 0000000000..da6317036d --- /dev/null +++ b/src/Appwrite/Utopia/Response/Model/OAuth2Appwrite.php @@ -0,0 +1,47 @@ + 'appwrite', + ]; + + public function getProviderLabel(): string + { + return 'Appwrite'; + } + + public function getClientIdExample(): string + { + return '6a42000000000000b5a0'; + } + + public function getClientSecretExample(): string + { + return 'b86afd000000000000000000000000000000000000000000000000000ced5f93'; + } + + /** + * Get Name + * + * @return string + */ + public function getName(): string + { + return 'OAuth2Appwrite'; + } + + /** + * Get Type + * + * @return string + */ + public function getType(): string + { + return Response::MODEL_OAUTH2_APPWRITE; + } +} diff --git a/src/Appwrite/Utopia/Response/Model/OAuth2ProviderList.php b/src/Appwrite/Utopia/Response/Model/OAuth2ProviderList.php index 81c23c803c..733b03ba3b 100644 --- a/src/Appwrite/Utopia/Response/Model/OAuth2ProviderList.php +++ b/src/Appwrite/Utopia/Response/Model/OAuth2ProviderList.php @@ -49,6 +49,7 @@ class OAuth2ProviderList extends Model Response::MODEL_OAUTH2_TRADESHIFT, Response::MODEL_OAUTH2_PAYPAL, Response::MODEL_OAUTH2_GITLAB, + Response::MODEL_OAUTH2_APPWRITE, Response::MODEL_OAUTH2_AUTHENTIK, Response::MODEL_OAUTH2_AUTH0, Response::MODEL_OAUTH2_FUSIONAUTH, diff --git a/tests/unit/Platform/Modules/Project/OAuth2/OAuth2ProviderTest.php b/tests/unit/Platform/Modules/Project/OAuth2/OAuth2ProviderTest.php index 4c66222256..63c1b1012f 100644 --- a/tests/unit/Platform/Modules/Project/OAuth2/OAuth2ProviderTest.php +++ b/tests/unit/Platform/Modules/Project/OAuth2/OAuth2ProviderTest.php @@ -7,6 +7,7 @@ namespace Tests\Unit\Platform\Modules\Project\OAuth2; use Appwrite\Event\Event as QueueEvent; use Appwrite\Extend\Exception; use Appwrite\Platform\Modules\Project\Http\Project\OAuth2\Apple\Update as AppleUpdate; +use Appwrite\Platform\Modules\Project\Http\Project\OAuth2\Appwrite\Update as AppwriteUpdate; use Appwrite\Platform\Modules\Project\Http\Project\OAuth2\Auth0\Update as Auth0Update; use Appwrite\Platform\Modules\Project\Http\Project\OAuth2\Authentik\Update as AuthentikUpdate; use Appwrite\Platform\Modules\Project\Http\Project\OAuth2\Base; @@ -36,13 +37,14 @@ final class OAuth2ProviderTest extends TestCase \sort($ids); $expected = [ - 'amazon', 'apple', 'auth0', 'authentik', 'autodesk', 'bitbucket', - 'bitly', 'box', 'dailymotion', 'discord', 'disqus', 'dropbox', - 'etsy', 'facebook', 'figma', 'fusionauth', 'github', 'gitlab', - 'google', 'keycloak', 'kick', 'linkedin', 'microsoft', 'notion', - 'oidc', 'okta', 'paypal', 'paypalSandbox', 'podio', 'salesforce', - 'slack', 'spotify', 'stripe', 'tradeshift', 'tradeshiftBox', - 'twitch', 'wordpress', 'x', 'yahoo', 'yandex', 'zoho', 'zoom', + 'amazon', 'apple', 'appwrite', 'auth0', 'authentik', 'autodesk', + 'bitbucket', 'bitly', 'box', 'dailymotion', 'discord', 'disqus', + 'dropbox', 'etsy', 'facebook', 'figma', 'fusionauth', 'github', + 'gitlab', 'google', 'keycloak', 'kick', 'linkedin', 'microsoft', + 'notion', 'oidc', 'okta', 'paypal', 'paypalSandbox', 'podio', + 'salesforce', 'slack', 'spotify', 'stripe', 'tradeshift', + 'tradeshiftBox', 'twitch', 'wordpress', 'x', 'yahoo', 'yandex', + 'zoho', 'zoom', ]; \sort($expected); @@ -84,6 +86,7 @@ final class OAuth2ProviderTest extends TestCase yield 'default provider' => [Base::getProviderActions()['amazon'], ['clientId', 'clientSecret']]; yield 'custom id and secret names' => [DropboxUpdate::class, ['appKey', 'appSecret']]; yield 'apple' => [AppleUpdate::class, ['serviceId', 'keyId', 'teamId', 'p8File']]; + yield 'appwrite' => [AppwriteUpdate::class, ['clientId', 'clientSecret']]; yield 'auth0' => [Auth0Update::class, ['clientId', 'clientSecret', 'endpoint']]; yield 'authentik' => [AuthentikUpdate::class, ['clientId', 'clientSecret', 'endpoint']]; yield 'fusionauth' => [FusionAuthUpdate::class, ['clientId', 'clientSecret', 'endpoint']];