Skip to content

Commit 9067a85

Browse files
committed
fix(credentials): skip the fallback token once a provider refused it during the failed refresh
1 parent 2a94a62 commit 9067a85

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

‎apps/sim/lib/oauth/credential-service-cancellation.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,21 @@ describe('client credential token refresh after a failed mint', () => {
244244
})
245245
})
246246

247+
it('does not serve a cached token the provider refused while its failed refresh was in flight', async () => {
248+
const mint = minter()
249+
mint.mockResolvedValueOnce({ accessToken: 'current-token', expiresInSeconds: 600 })
250+
await resolveFor('ramp-refresh-rejected-midway')
251+
mint.mockImplementation(async () => {
252+
forgetRejectedClientCredentialToken('current-token')
253+
throw unavailable()
254+
})
255+
vi.advanceTimersByTime(6 * 60_000)
256+
257+
await expect(resolveFor('ramp-refresh-rejected-midway')).resolves.toEqual({
258+
error: expect.objectContaining({ code: 'provider_unavailable' }),
259+
})
260+
})
261+
247262
it('stops serving the cached token once the provider refuses the credentials', async () => {
248263
const mint = minter()
249264
mint.mockResolvedValueOnce({ accessToken: 'current-token', expiresInSeconds: 600 })

‎apps/sim/lib/oauth/credential-service.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,9 +789,16 @@ async function resolveMintedServiceAccountToken(
789789
if (cached && cached.expiresAtMs - Date.now() > CLIENT_CREDENTIAL_TOKEN_MIN_TTL_MS) {
790790
return { ...cached.token }
791791
}
792-
/** The cached token while more than the expiry margin of it is left, checked at each use. */
792+
/**
793+
* The cached token while it is still the one in the cache (a provider may refuse it while a
794+
* refresh is in flight) and more than the expiry margin of it is left, checked at each use.
795+
*/
793796
const stillValid = () =>
794-
cached && cached.expiresAtMs - Date.now() > TOKEN_EXPIRY_MARGIN_MS ? cached : undefined
797+
cached &&
798+
clientCredentialTokenCache.get(cacheIdentity) === cached &&
799+
cached.expiresAtMs - Date.now() > TOKEN_EXPIRY_MARGIN_MS
800+
? cached
801+
: undefined
795802

796803
const failedEntry = clientCredentialMintFailureCache.get(cacheIdentity)
797804
const failed = failedEntry?.secretFingerprint === secretFingerprint ? failedEntry : undefined

0 commit comments

Comments
 (0)