Skip to content

Commit 3f72bcb

Browse files
feat(auth): Refresh Token Rotation (#3112)
Co-authored-by: Matt Creaser <[email protected]>
1 parent bc95c89 commit 3f72bcb

File tree

9 files changed

+462
-41
lines changed

9 files changed

+462
-41
lines changed

aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/actions/FetchAuthSessionCognitoActions.kt

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ package com.amplifyframework.auth.cognito.actions
1717

1818
import aws.sdk.kotlin.services.cognitoidentity.model.GetCredentialsForIdentityRequest
1919
import aws.sdk.kotlin.services.cognitoidentity.model.GetIdRequest
20-
import aws.sdk.kotlin.services.cognitoidentityprovider.initiateAuth
21-
import aws.sdk.kotlin.services.cognitoidentityprovider.model.AuthFlowType
20+
import aws.sdk.kotlin.services.cognitoidentityprovider.getTokensFromRefreshToken
2221
import aws.smithy.kotlin.runtime.time.Instant
2322
import com.amplifyframework.auth.cognito.AuthEnvironment
24-
import com.amplifyframework.auth.cognito.helpers.AuthHelper
2523
import com.amplifyframework.auth.cognito.helpers.SessionHelper
2624
import com.amplifyframework.auth.exceptions.NotAuthorizedException
2725
import com.amplifyframework.auth.exceptions.SessionExpiredException
@@ -40,44 +38,27 @@ import com.amplifyframework.statemachine.codegen.events.RefreshSessionEvent
4038
import kotlin.time.Duration.Companion.seconds
4139

4240
internal object FetchAuthSessionCognitoActions : FetchAuthSessionActions {
43-
private const val KEY_SECRET_HASH = "SECRET_HASH"
44-
private const val KEY_REFRESH_TOKEN = "REFRESH_TOKEN"
45-
private const val KEY_DEVICE_KEY = "DEVICE_KEY"
4641

4742
override fun refreshUserPoolTokensAction(signedInData: SignedInData) =
4843
Action<AuthEnvironment>("RefreshUserPoolTokens") { id, dispatcher ->
4944
logger.verbose("$id Starting execution")
5045
val evt = try {
5146
val username = signedInData.username
5247
val tokens = signedInData.cognitoUserPoolTokens
53-
54-
val authParameters = mutableMapOf<String, String>()
55-
val secretHash = AuthHelper.getSecretHash(
56-
username,
57-
configuration.userPool?.appClient,
58-
configuration.userPool?.appClientSecret
59-
)
60-
tokens.refreshToken?.let { authParameters[KEY_REFRESH_TOKEN] = it }
61-
secretHash?.let { authParameters[KEY_SECRET_HASH] = it }
62-
63-
val encodedContextData = getUserContextData(username)
6448
val deviceMetadata: DeviceMetadata.Metadata? = getDeviceMetadata(username)
65-
deviceMetadata?.let { authParameters[KEY_DEVICE_KEY] = it.deviceKey }
66-
val pinpointEndpointId = getPinpointEndpointId()
6749

68-
val response = cognitoAuthService.cognitoIdentityProviderClient?.initiateAuth {
69-
authFlow = AuthFlowType.RefreshToken
50+
val response = cognitoAuthService.cognitoIdentityProviderClient?.getTokensFromRefreshToken {
51+
refreshToken = tokens.refreshToken
7052
clientId = configuration.userPool?.appClient
71-
this.authParameters = authParameters
72-
pinpointEndpointId?.let { analyticsMetadata { analyticsEndpointId = it } }
73-
encodedContextData?.let { userContextData { encodedData = it } }
53+
clientSecret = configuration.userPool?.appClientSecret
54+
deviceKey = deviceMetadata?.deviceKey
7455
}
7556

7657
val expiresIn = response?.authenticationResult?.expiresIn?.toLong() ?: 0
7758
val refreshedUserPoolTokens = CognitoUserPoolTokens(
7859
idToken = response?.authenticationResult?.idToken,
7960
accessToken = response?.authenticationResult?.accessToken,
80-
refreshToken = tokens.refreshToken,
61+
refreshToken = response?.authenticationResult?.refreshToken ?: tokens.refreshToken,
8162
expiration = Instant.now().plus(expiresIn.seconds).epochSeconds
8263
)
8364

0 commit comments

Comments
 (0)