Harden VCS token refresh service

This commit is contained in:
harsh mahajan committed 2026-07-13 17:18:18 +05:30
1 parent 5701216948
commit f914a4dd91
3 files changed
+58 -8

No files matched your search

+14 -2
View File
@@ -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('');
+39 -3
View File
@@ -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',
+5 -3
View File
@@ -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