From 4253efeff84a722c8ea8ab35b919c2e949c2d567 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 2 Jul 2026 14:35:52 +0200 Subject: [PATCH 1/2] Add test proving the bug 1) Tests\E2E\Services\Account\AccountCustomClientTest::testOAuth2TokenSessionProviderAccessToken Failed asserting that a string is not empty. /usr/src/code/tests/e2e/Services/Account/AccountCustomClientTest.php:2320 --- .../Account/AccountCustomClientTest.php | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/tests/e2e/Services/Account/AccountCustomClientTest.php b/tests/e2e/Services/Account/AccountCustomClientTest.php index b6ce35eda4..e59e6752ae 100644 --- a/tests/e2e/Services/Account/AccountCustomClientTest.php +++ b/tests/e2e/Services/Account/AccountCustomClientTest.php @@ -2229,6 +2229,98 @@ final class AccountCustomClientTest extends Scope $this->assertEquals(412, $response['headers']['status-code']); } + public function testOAuth2TokenSessionProviderAccessToken(): void + { + // Just ensure we have a session set up + $this->setupAccountWithSession(); + + $provider = 'mock'; + $appId = '1'; + $secret = '123456'; + + // Enable the mock OAuth2 provider + $response = $this->client->call(Client::METHOD_PATCH, '/projects/' . $this->getProject()['$id'] . '/oauth2', array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console', + 'cookie' => 'a_session_console=' . $this->getRoot()['session'], + ]), [ + 'provider' => $provider, + 'appId' => $appId, + 'secret' => $secret, + 'enabled' => true, + ]); + + $this->assertEquals(200, $response['headers']['status-code']); + + // Start the OAuth2 *token* flow (createOAuth2Token), NOT createOAuth2Session + $response = $this->client->call(Client::METHOD_GET, '/account/tokens/oauth2/' . $provider, array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ]), [ + 'success' => 'http://localhost/v1/mock/tests/general/oauth2/success', + 'failure' => 'http://localhost/v1/mock/tests/general/oauth2/failure', + ], followRedirects: false); + + $this->assertEquals(301, $response['headers']['status-code']); + $this->assertStringStartsWith('http://localhost/v1/mock/tests/general/oauth2', $response['headers']['location']); + + $oauthClient = new Client(); + $oauthClient->setEndpoint(''); + + $response = $oauthClient->call(Client::METHOD_GET, $response['headers']['location'], followRedirects: false); + $this->assertEquals(301, $response['headers']['status-code']); + $this->assertStringStartsWith('http://appwrite:/v1/account/sessions/oauth2/callback/mock/' . $this->getProject()['$id'] . '?code=', $response['headers']['location']); + + $response = $oauthClient->call(Client::METHOD_GET, $response['headers']['location'], followRedirects: false); + $this->assertEquals(301, $response['headers']['status-code']); + $this->assertStringStartsWith('http://appwrite:/v1/account/sessions/oauth2/mock/redirect?code=', $response['headers']['location']); + + $response = $oauthClient->call(Client::METHOD_GET, $response['headers']['location'], followRedirects: false); + $this->assertEquals(301, $response['headers']['status-code']); + $this->assertStringStartsWith('http://localhost/v1/mock/tests/general/oauth2/success?secret=', $response['headers']['location']); + + $oauthParamsString = \parse_url($response['headers']['location'], PHP_URL_QUERY); + $oauthParams = []; + \parse_str($oauthParamsString, $oauthParams); + + $this->assertNotEmpty($oauthParams['secret']); + $this->assertNotEmpty($oauthParams['userId']); + + // Exchange the token for a session via createSession (POST /account/sessions/token) + $response = $this->client->call(Client::METHOD_POST, '/account/sessions/token', [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], [ + 'userId' => $oauthParams['userId'], + 'secret' => $oauthParams['secret'], + ]); + + $this->assertEquals(201, $response['headers']['status-code']); + $this->assertEquals('mock', $response['body']['provider']); + + $sessionCookieKey = 'a_session_' . $this->getProject()['$id']; + $this->assertArrayHasKey($sessionCookieKey, $response['cookies']); + $oauthUserCookie = $response['cookies'][$sessionCookieKey]; + $this->assertNotEmpty($oauthUserCookie); + + // Get the current session and ensure the provider access token was persisted + $response = $this->client->call(Client::METHOD_GET, '/account/sessions/current', [ + 'x-appwrite-project' => $this->getProject()['$id'], + 'cookie' => $sessionCookieKey . '=' . $oauthUserCookie, + ]); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals('mock', $response['body']['provider']); + + // The bug: session created via createOAuth2Token + createSession has an empty + // providerAccessToken, whereas createOAuth2Session persists it (see mock provider '123456'). + $this->assertNotEmpty($response['body']['providerAccessToken']); + $this->assertEquals('123456', $response['body']['providerAccessToken']); + } + public function testCreateOidcOAuth2Token(): void { $provider = 'oidc'; From 38533b2f5cf32fafa9f6cab026a1cf65f2c5bd92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 2 Jul 2026 14:37:56 +0200 Subject: [PATCH 2/2] Fix the bug --- app/controllers/api/account.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 9acd819520..9e78ce5b69 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -141,6 +141,26 @@ $createSession = function (string $userId, string $secret, Request $request, Res TOKEN_TYPE_OAUTH2 => $oauthProvider, default => SESSION_PROVIDER_TOKEN, }; + + // For OAuth2 tokens, carry the provider credentials from the identity onto the session, + // mirroring the cookie-based createOAuth2Session flow. + $providerCredentials = []; + if ($verifiedToken->getAttribute('type') === TOKEN_TYPE_OAUTH2) { + $identity = $authorization->skip(fn () => $dbForProject->findOne('identities', [ + Query::equal('userInternalId', [$user->getSequence()]), + Query::equal('provider', [$oauthProvider]), + ])); + + if (!$identity->isEmpty()) { + $providerCredentials = [ + 'providerUid' => $identity->getAttribute('providerUid'), + 'providerAccessToken' => $identity->getAttribute('providerAccessToken'), + 'providerRefreshToken' => $identity->getAttribute('providerRefreshToken'), + 'providerAccessTokenExpiry' => $identity->getAttribute('providerAccessTokenExpiry'), + ]; + } + } + $session = new Document(array_merge( [ '$id' => ID::unique(), @@ -154,6 +174,7 @@ $createSession = function (string $userId, string $secret, Request $request, Res 'countryCode' => ($record) ? \strtolower($record['country']['iso_code']) : '--', 'expire' => DateTime::addSeconds(new \DateTime(), $duration) ], + $providerCredentials, $detector->getOS(), $detector->getClient(), $detector->getDevice()