From f914a4dd91c63593c5fc3fe387832afbfc0437d2 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 13 Jul 2026 17:18:18 +0530 Subject: [PATCH] Harden VCS token refresh service --- src/Appwrite/Vcs/InstallationTokens.php | 16 +++++++-- tests/unit/Vcs/InstallationTokensTest.php | 42 +++++++++++++++++++++-- tests/unit/Vcs/RepositoryWebhooksTest.php | 8 +++-- 3 files changed, 58 insertions(+), 8 deletions(-) diff --git a/src/Appwrite/Vcs/InstallationTokens.php b/src/Appwrite/Vcs/InstallationTokens.php index 17790e91e7..43fcb3088c 100644 --- a/src/Appwrite/Vcs/InstallationTokens.php +++ b/src/Appwrite/Vcs/InstallationTokens.php @@ -22,7 +22,10 @@ class InstallationTokens $accessTokenExpiry = $accessTokenExpiry ?? $identity->getAttribute('providerAccessTokenExpiry'); } - $installation = $installation->setAttribute('personalAccessToken', $accessToken); + $installation = $installation + ->setAttribute('personalAccessToken', $accessToken) + ->setAttribute('personalRefreshToken', $refreshToken) + ->setAttribute('personalAccessTokenExpiry', $accessTokenExpiry); if (!$this->isExpired($accessTokenExpiry)) { return $installation; @@ -32,7 +35,16 @@ class InstallationTokens throw new Exception(Exception::GENERAL_PROVIDER_FAILURE, 'This installation has no refresh token on file. Please reconnect it.'); } - $oauth2->refreshTokens($refreshToken); + try { + $oauth2->refreshTokens($refreshToken); + } catch (\Throwable) { + $current = $this->getCurrentInstallation($dbForPlatform, $installation); + if (!$current->isEmpty() && !empty($current->getAttribute('personalAccessToken')) && !$this->isExpired($current->getAttribute('personalAccessTokenExpiry'))) { + return $current; + } + + throw new Exception(Exception::GENERAL_PROVIDER_FAILURE, 'Failed to refresh OAuth2 access token. Please reconnect the installation.'); + } $accessToken = $oauth2->getAccessToken(''); $refreshToken = $oauth2->getRefreshToken(''); diff --git a/tests/unit/Vcs/InstallationTokensTest.php b/tests/unit/Vcs/InstallationTokensTest.php index cecc11113b..96f1381eee 100644 --- a/tests/unit/Vcs/InstallationTokensTest.php +++ b/tests/unit/Vcs/InstallationTokensTest.php @@ -112,6 +112,8 @@ final class InstallationTokensTest extends TestCase $result = (new InstallationTokens())->refresh($installation, $this->db(), $oauth2, $identity); $this->assertSame('identity-token', $result->getAttribute('personalAccessToken')); + $this->assertSame('identity-refresh', $result->getAttribute('personalRefreshToken')); + $this->assertSame($identity->getAttribute('providerAccessTokenExpiry'), $result->getAttribute('personalAccessTokenExpiry')); } public function testMissingRefreshTokenThrowsClearError(): void @@ -181,13 +183,43 @@ final class InstallationTokensTest extends TestCase $this->assertSame('already-refreshed-token', $result->getAttribute('personalAccessToken')); } - protected function fakeOAuth2(bool $emptyUserId = false) + public function testRefreshExceptionReturnsCurrentInstallationWhenAnotherRequestRefreshed(): void { - return new class ($emptyUserId) extends OAuth2 { + $db = $this->createMock(Database::class); + $current = new Document([ + '$id' => 'installation1', + 'personalAccessToken' => 'already-refreshed-token', + 'personalRefreshToken' => 'already-refreshed-refresh', + 'personalAccessTokenExpiry' => DateTime::addSeconds(new \DateTime(), 3600), + ]); + + $db->expects($this->once()) + ->method('getDocument') + ->with('installations', 'installation1') + ->willReturn($current); + $db->expects($this->never())->method('updateDocument'); + + $installation = new Document([ + '$id' => 'installation1', + 'personalAccessToken' => 'stale-token', + 'personalRefreshToken' => 'stale-refresh', + 'personalAccessTokenExpiry' => DateTime::addSeconds(new \DateTime(), -3600), + ]); + + $oauth2 = $this->fakeOAuth2(throwOnRefresh: true); + + $result = (new InstallationTokens())->refresh($installation, $db, $oauth2); + + $this->assertSame('already-refreshed-token', $result->getAttribute('personalAccessToken')); + } + + protected function fakeOAuth2(bool $emptyUserId = false, bool $throwOnRefresh = false) + { + return new class ($emptyUserId, $throwOnRefresh) extends OAuth2 { public int $refreshCalls = 0; protected array $tokens = []; - public function __construct(protected bool $emptyUserId) + public function __construct(protected bool $emptyUserId, protected bool $throwOnRefresh) { parent::__construct('id', 'secret', ''); } @@ -210,6 +242,10 @@ final class InstallationTokensTest extends TestCase public function refreshTokens(string $refreshToken): array { $this->refreshCalls++; + if ($this->throwOnRefresh) { + throw new \RuntimeException('refresh token already used'); + } + $this->tokens = [ 'access_token' => 'fresh-token', 'refresh_token' => 'fresh-refresh', diff --git a/tests/unit/Vcs/RepositoryWebhooksTest.php b/tests/unit/Vcs/RepositoryWebhooksTest.php index f29e53e1b0..cb2856d807 100644 --- a/tests/unit/Vcs/RepositoryWebhooksTest.php +++ b/tests/unit/Vcs/RepositoryWebhooksTest.php @@ -111,9 +111,11 @@ final class RepositoryWebhooksTest extends TestCase { \putenv('_APP_VCS_WEBHOOK_URL=https://example.com'); - (new RepositoryWebhooks($vcsFactory))->ensure($adapter, $installation, $db, 'repo-1', 'owner', 'repo'); - - \putenv('_APP_VCS_WEBHOOK_URL'); + try { + (new RepositoryWebhooks($vcsFactory))->ensure($adapter, $installation, $db, 'repo-1', 'owner', 'repo'); + } finally { + \putenv('_APP_VCS_WEBHOOK_URL'); + } } protected function factory(string $secret): Factory