From 0fe906c538d4af9f76b41a520edb5024fa3f974b Mon Sep 17 00:00:00 2001 From: Harsh Mahajan Date: Mon, 23 Mar 2026 13:21:04 +0530 Subject: [PATCH] feat: Add X OAuth 2.0 provider --- app/config/oAuthProviders.php | 11 ++ app/controllers/api/account.php | 48 ++++++- src/Appwrite/Auth/OAuth2/X.php | 236 ++++++++++++++++++++++++++++++++ 3 files changed, 293 insertions(+), 2 deletions(-) create mode 100644 src/Appwrite/Auth/OAuth2/X.php diff --git a/app/config/oAuthProviders.php b/app/config/oAuthProviders.php index e6acd08c54..cda6459519 100644 --- a/app/config/oAuthProviders.php +++ b/app/config/oAuthProviders.php @@ -376,6 +376,17 @@ return [ 'mock' => false, 'class' => 'Appwrite\\Auth\\OAuth2\\Wordpress', ], + 'x' => [ + 'name' => 'X', + 'developers' => 'https://docs.x.com/fundamentals/authentication/oauth-2-0/authorization-code', + 'icon' => 'icon-twitter', + 'enabled' => true, + 'sandbox' => false, + 'form' => false, + 'beta' => false, + 'mock' => false, + 'class' => 'Appwrite\\Auth\\OAuth2\\X', + ], 'yahoo' => [ 'name' => 'Yahoo', 'developers' => 'https://developer.yahoo.com/oauth2/guide/flows_authcode/', diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 6d33b45f0b..d6f4561d65 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -1375,10 +1375,25 @@ Http::get('/v1/account/sessions/oauth2/:provider') 'token' => false, ], $scopes); + $loginURL = $oauth2->getLoginURL(); + + if ($provider === 'x' && \method_exists($oauth2, 'getPKCEVerifier')) { + $response->addCookie( + 'a_oauth2_pkce_' . $project->getId() . '_' . $provider, + $oauth2->getPKCEVerifier(), + \time() + 300, + '/', + Config::getParam('cookieDomain'), + ('https' === $protocol), + true, + Response::COOKIE_SAMESITE_LAX + ); + } + $response ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') ->addHeader('Pragma', 'no-cache') - ->redirect($oauth2->getLoginURL()); + ->redirect($loginURL); }); Http::get('/v1/account/sessions/oauth2/callback/:provider/:projectId') @@ -1511,6 +1526,20 @@ Http::get('/v1/account/sessions/oauth2/:provider/redirect') /** @var Appwrite\Auth\OAuth2 $oauth2 */ $oauth2 = new $className($appId, $appSecret, $callback); + if ($provider === 'x' && \method_exists($oauth2, 'setPKCEVerifier')) { + $oauth2->setPKCEVerifier($request->getCookie('a_oauth2_pkce_' . $project->getId() . '_' . $provider, '')); + $response->addCookie( + 'a_oauth2_pkce_' . $project->getId() . '_' . $provider, + '', + \time() - 3600, + '/', + Config::getParam('cookieDomain'), + ('https' === $protocol), + true, + Response::COOKIE_SAMESITE_LAX + ); + } + if (!empty($state)) { try { $state = \array_merge($defaultState, $oauth2->parseState($state)); @@ -2079,10 +2108,25 @@ Http::get('/v1/account/tokens/oauth2/:provider') 'token' => true, ], $scopes); + $loginURL = $oauth2->getLoginURL(); + + if ($provider === 'x' && \method_exists($oauth2, 'getPKCEVerifier')) { + $response->addCookie( + 'a_oauth2_pkce_' . $project->getId() . '_' . $provider, + $oauth2->getPKCEVerifier(), + \time() + 300, + '/', + Config::getParam('cookieDomain'), + ('https' === $protocol), + true, + Response::COOKIE_SAMESITE_LAX + ); + } + $response ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') ->addHeader('Pragma', 'no-cache') - ->redirect($oauth2->getLoginURL()); + ->redirect($loginURL); }); Http::post('/v1/account/tokens/magic-url') diff --git a/src/Appwrite/Auth/OAuth2/X.php b/src/Appwrite/Auth/OAuth2/X.php new file mode 100644 index 0000000000..b309f90c0f --- /dev/null +++ b/src/Appwrite/Auth/OAuth2/X.php @@ -0,0 +1,236 @@ + 'code', + 'client_id' => $this->appID, + 'redirect_uri' => $this->callback, + 'scope' => \implode(' ', $this->getScopes()), + 'state' => \json_encode($this->state), + 'code_challenge' => $this->getCodeChallenge(), + 'code_challenge_method' => 'S256', + ]); + } + + /** + * @return string + */ + public function getPKCEVerifier(): string + { + if (empty($this->pkceVerifier)) { + $this->pkceVerifier = $this->base64UrlEncode(\random_bytes(32)); + } + + return $this->pkceVerifier; + } + + /** + * @param string $pkceVerifier + * + * @return void + */ + public function setPKCEVerifier(string $pkceVerifier): void + { + $this->pkceVerifier = $pkceVerifier; + } + + /** + * @param string $code + * + * @return array + */ + protected function getTokens(string $code): array + { + if (empty($this->tokens)) { + if (empty($this->pkceVerifier)) { + throw new Exception(\json_encode([ + 'error' => 'invalid_request', + 'error_description' => 'Missing PKCE verifier.', + ]), 400); + } + + $headers = [ + 'Authorization: Basic ' . \base64_encode($this->appID . ':' . $this->appSecret), + 'Content-Type: application/x-www-form-urlencoded', + ]; + + $this->tokens = \json_decode($this->request( + 'POST', + 'https://api.x.com/2/oauth2/token', + $headers, + \http_build_query([ + 'code' => $code, + 'client_id' => $this->appID, + 'grant_type' => 'authorization_code', + 'redirect_uri' => $this->callback, + 'code_verifier' => $this->getPKCEVerifier(), + ]) + ), true); + } + + return $this->tokens; + } + + /** + * @param string $refreshToken + * + * @return array + */ + public function refreshTokens(string $refreshToken): array + { + $headers = [ + 'Authorization: Basic ' . \base64_encode($this->appID . ':' . $this->appSecret), + 'Content-Type: application/x-www-form-urlencoded', + ]; + + $this->tokens = \json_decode($this->request( + 'POST', + 'https://api.x.com/2/oauth2/token', + $headers, + \http_build_query([ + 'client_id' => $this->appID, + 'refresh_token' => $refreshToken, + '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['data']['id'] ?? ''; + } + + /** + * @param string $accessToken + * + * @return string + */ + public function getUserEmail(string $accessToken): string + { + $user = $this->getUser($accessToken); + + return $user['data']['confirmed_email'] ?? ''; + } + + /** + * Check if the OAuth email is verified. + * + * X returns a confirmed email only when the app has email access enabled + * and the authenticated user has a confirmed email address. + * + * @param string $accessToken + * + * @return bool + */ + public function isEmailVerified(string $accessToken): bool + { + return !empty($this->getUserEmail($accessToken)); + } + + /** + * @param string $accessToken + * + * @return string + */ + public function getUserName(string $accessToken): string + { + $user = $this->getUser($accessToken); + + return $user['data']['name'] ?? ''; + } + + /** + * @param string $accessToken + * + * @return array + */ + protected function getUser(string $accessToken): array + { + if (empty($this->user)) { + $this->user = \json_decode($this->request( + 'GET', + 'https://api.x.com/2/users/me?user.fields=confirmed_email', + ['Authorization: Bearer ' . \urlencode($accessToken)] + ), true); + } + + return $this->user; + } + + /** + * @return string + */ + private function getCodeChallenge(): string + { + return $this->base64UrlEncode(\hash('sha256', $this->getPKCEVerifier(), true)); + } + + /** + * @param string $value + * + * @return string + */ + private function base64UrlEncode(string $value): string + { + return \rtrim(\strtr(\base64_encode($value), '+/', '-_'), '='); + } +}