Skip to content

Commit 81098fa

Browse files
committed
SsoAccessTokenProvider tests
1 parent dafd506 commit 81098fa

File tree

3 files changed

+51
-20
lines changed

3 files changed

+51
-20
lines changed

plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/credentials/sso/DiskCache.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,4 @@ class DiskCache(
324324
}
325325
}
326326

327-
class ClientRegistrationNotFoundException : Exception("Client registration file not found")
327+
class ClientRegistrationNotFoundException : RuntimeException("Client registration file not found")

plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/credentials/sso/SsoAccessTokenProvider.kt

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -429,26 +429,24 @@ class SsoAccessTokenProvider(
429429
}
430430

431431
stageName = RefreshCredentialStage.LOAD_REGISTRATION
432-
val registration = run {
433-
try {
434-
when (currentToken) {
435-
is DeviceAuthorizationGrantToken -> loadDagClientRegistration(SourceOfLoadRegistration.REFRESH_TOKEN.toString())
436-
is PKCEAuthorizationGrantToken -> loadPkceClientRegistration(SourceOfLoadRegistration.REFRESH_TOKEN.toString())
437-
}
438-
} catch (e: ClientRegistrationNotFoundException) {
439-
// invalidate tokens to force a reauth
440-
invalidate()
441-
null
442-
} catch (e: Exception) {
443-
val message = e.message ?: "$stageName: ${e::class.java.name}"
444-
sendRefreshCredentialsMetric(
445-
currentToken,
446-
reason = "Refresh access token request failed: $stageName",
447-
reasonDesc = message,
448-
result = Result.Failed
449-
)
450-
throw InvalidClientException.builder().message(message).cause(e).build()
432+
val registration = try {
433+
when (currentToken) {
434+
is DeviceAuthorizationGrantToken -> loadDagClientRegistration(SourceOfLoadRegistration.REFRESH_TOKEN.toString())
435+
is PKCEAuthorizationGrantToken -> loadPkceClientRegistration(SourceOfLoadRegistration.REFRESH_TOKEN.toString())
451436
}
437+
} catch (e: ClientRegistrationNotFoundException) {
438+
// invalidate tokens to force a reauth
439+
invalidate()
440+
null
441+
} catch (e: Exception) {
442+
val message = e.message ?: "$stageName: ${e::class.java.name}"
443+
sendRefreshCredentialsMetric(
444+
currentToken,
445+
reason = "Refresh access token request failed: $stageName",
446+
reasonDesc = message,
447+
result = Result.Failed
448+
)
449+
throw InvalidClientException.builder().message(message).cause(e).build()
452450
}
453451

454452
stageName = RefreshCredentialStage.VALIDATE_REGISTRATION

plugins/core/jetbrains-community/tst/software/aws/toolkits/jetbrains/core/credentials/sso/SsoAccessTokenProviderTest.kt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,39 @@ class SsoAccessTokenProviderTest {
479479
verify(ssoCache).invalidateAccessToken(ssoUrl)
480480
}
481481

482+
@Test
483+
fun `refreshToken invalidates tokens when client registration not found during refresh`() {
484+
setPkceTrue()
485+
486+
val accessToken = PKCEAuthorizationGrantToken(
487+
ssoUrl,
488+
ssoRegion,
489+
"dummyToken",
490+
"refreshToken",
491+
clock.instant(),
492+
clock.instant()
493+
)
494+
495+
ssoCache.stub {
496+
on(ssoCache.loadAccessToken(any<PKCEAccessTokenCacheKey>()))
497+
.thenReturn(accessToken)
498+
on(
499+
ssoCache.loadClientRegistration(
500+
any<PKCEClientRegistrationCacheKey>(),
501+
eq(SsoAccessTokenProvider.SourceOfLoadRegistration.REFRESH_TOKEN.toString())
502+
)
503+
).thenThrow(ClientRegistrationNotFoundException())
504+
}
505+
506+
assertThatThrownBy {
507+
runBlocking {
508+
sut.refreshToken(sut.accessToken())
509+
}
510+
}.isInstanceOf(InvalidClientException::class.java)
511+
512+
verify(ssoCache, times(2)).invalidateAccessToken(any<AccessTokenCacheKey>())
513+
}
514+
482515
private fun setupCacheStub(expirationClientRegistration: Instant) {
483516
setupCacheStub(DeviceAuthorizationClientRegistration(clientId, clientSecret, expirationClientRegistration))
484517
}

0 commit comments

Comments
 (0)