Skip to content

Commit dafa8db

Browse files
committed
Added reauth for expired client registration
1 parent e74072a commit dafa8db

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,15 @@ class SsoAccessTokenProvider(
393393
throw InvalidClientException.builder().message(message).cause(e).build()
394394
}
395395

396+
var isExpired = currentToken.expiresAt.isBefore(Instant.now(clock))
397+
if (isExpired){
398+
registration = reauthExpiredRegistration(currentToken)
399+
isExpired = accessToken().expiresAt.isBefore(Instant.now(clock))
400+
}
396401
if (registration == null) {
397402
val (message, reason) = when {
398-
currentToken.expiresAt.isBefore(Instant.now(clock)) -> Pair(
399-
"Client registration has expired",
403+
isExpired -> Pair(
404+
"Client registration has expired and reauth failed",
400405
"Expired client registration"
401406
)
402407
else -> Pair(
@@ -407,7 +412,7 @@ class SsoAccessTokenProvider(
407412
sendRefreshCredentialsMetric(
408413
currentToken,
409414
reason = reason,
410-
reasonDesc = "Step: Load Registration - $message",
415+
reasonDesc = "Step: Check Registration - $message",
411416
result = Result.Failed
412417
)
413418
throw InvalidClientException.builder().message(message).build()
@@ -537,6 +542,28 @@ class SsoAccessTokenProvider(
537542
}
538543
}
539544

545+
private fun reauthExpiredRegistration(expiredToken: AccessToken): ClientRegistration? {
546+
when (expiredToken) {
547+
is DeviceAuthorizationGrantToken -> registerDAGClient()
548+
is PKCEAuthorizationGrantToken -> registerPkceClient()
549+
}
550+
try {
551+
return when (expiredToken) {
552+
is DeviceAuthorizationGrantToken -> loadDagClientRegistration()
553+
is PKCEAuthorizationGrantToken -> loadPkceClientRegistration()
554+
}
555+
} catch (e: Exception) {
556+
val message = "Error loading client registration: ${e.message}"
557+
sendRefreshCredentialsMetric(
558+
expiredToken,
559+
reason = "Failed to load client registration",
560+
reasonDesc = "Step: Load Registration after reauth - $message",
561+
result = Result.Failed
562+
)
563+
throw InvalidClientException.builder().message(message).cause(e).build()
564+
}
565+
}
566+
540567
private fun saveAccessToken(token: AccessToken) {
541568
try {
542569
when (token) {

0 commit comments

Comments
 (0)