feat: Add X OAuth 2.0 provider

This commit is contained in:
Harsh Mahajan committed 2026-03-23 13:21:04 +05:30
1 parent 6e59e1307d
commit 0fe906c538
3 files changed
+293 -2

No files matched your search

+11
View File
@@ -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/',
+46 -2
View File
@@ -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')
+236
View File
@@ -0,0 +1,236 @@
<?php
namespace Appwrite\Auth\OAuth2;
use Appwrite\Auth\OAuth2;
// Reference Material
// https://docs.x.com/fundamentals/authentication/oauth-2-0/authorization-code
// https://docs.x.com/x-api/users/get-me
class X extends OAuth2
{
/**
* @var array
*/
protected array $user = [];
/**
* @var array
*/
protected array $tokens = [];
/**
* @var array
*/
protected array $scopes = [
'users.read',
'users.email',
'offline.access',
];
/**
* @var string
*/
private string $pkceVerifier = '';
/**
* @return string
*/
public function getName(): string
{
return 'x';
}
/**
* @return string
*/
public function getLoginURL(): string
{
return 'https://x.com/i/oauth2/authorize?' . \http_build_query([
'response_type' => '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), '+/', '-_'), '=');
}
}