mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Merge pull request #12719 from appwrite/feat-appwrite-oauth
Feat: In-project sign-in with Appwrite Cloud OAuth2 server
This commit is contained in:
13 files changed
+392
-15
No files matched your search
@@ -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',
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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());
|
||||
|
||||
+1
-1
@@ -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.*",
|
||||
|
||||
Generated
+7
-7
@@ -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",
|
||||
|
||||
@@ -0,0 +1,236 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Auth\OAuth2;
|
||||
|
||||
use Appwrite\Auth\OAuth2;
|
||||
|
||||
// Reference Material
|
||||
// https://appwrite.io/docs/products/auth
|
||||
// Appwrite Cloud exposes an OpenID Connect provider for the console project at
|
||||
// https://cloud.appwrite.io/v1/oauth2/console/.well-known/openid-configuration
|
||||
|
||||
class Appwrite extends OAuth2
|
||||
{
|
||||
private const PKCE_STATE_KEY = '_pkce';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private string $endpoint = 'https://cloud.appwrite.io/v1/oauth2/console';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private string $pkceVerifier = '';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected array $user = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected array $tokens = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected array $scopes = [
|
||||
'openid',
|
||||
'profile',
|
||||
'email',
|
||||
];
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return 'appwrite';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLoginURL(): string
|
||||
{
|
||||
// The Appwrite OAuth2 provider requires PKCE. Stash the verifier in the
|
||||
// state so it survives the redirect, since the adapter is reconstructed
|
||||
// statelessly on the callback before the token exchange.
|
||||
$state = $this->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<string, mixed>|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)), '+/', '-_'), '=');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Platform\Modules\Project\Http\Project\OAuth2\Appwrite;
|
||||
|
||||
use Appwrite\Auth\OAuth2\Appwrite;
|
||||
use Appwrite\Platform\Modules\Project\Http\Project\OAuth2\Base;
|
||||
use Appwrite\Utopia\Response;
|
||||
|
||||
class Update extends Base
|
||||
{
|
||||
public static function getProviderId(): string
|
||||
{
|
||||
return 'appwrite';
|
||||
}
|
||||
|
||||
public static function getProviderClass(): string
|
||||
{
|
||||
return Appwrite::class;
|
||||
}
|
||||
|
||||
public static function getProviderLabel(): string
|
||||
{
|
||||
return 'Appwrite';
|
||||
}
|
||||
|
||||
public static function getProviderSDKMethod(): string
|
||||
{
|
||||
return 'updateOAuth2Appwrite';
|
||||
}
|
||||
|
||||
public static function getResponseModel(): string
|
||||
{
|
||||
return Response::MODEL_OAUTH2_APPWRITE;
|
||||
}
|
||||
|
||||
public static function getClientIdName(): string
|
||||
{
|
||||
return 'Client ID';
|
||||
}
|
||||
|
||||
public static function getClientIdExample(): string
|
||||
{
|
||||
return '6a42000000000000b5a0';
|
||||
}
|
||||
|
||||
public static function getClientSecretName(): string
|
||||
{
|
||||
return 'Client Secret';
|
||||
}
|
||||
|
||||
public static function getClientSecretExample(): string
|
||||
{
|
||||
return 'b86afd000000000000000000000000000000000000000000000000000ced5f93';
|
||||
}
|
||||
}
|
||||
@@ -309,6 +309,7 @@ abstract class Base extends Action
|
||||
'paypal' => 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,
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Utopia\Response\Model;
|
||||
|
||||
use Appwrite\Utopia\Response;
|
||||
|
||||
class OAuth2Appwrite extends OAuth2Base
|
||||
{
|
||||
public array $conditions = [
|
||||
'$id' => '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;
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
@@ -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']];
|
||||
|
||||
Reference in new issue
Block a user