@@ -17,11 +17,9 @@ package com.amplifyframework.auth.cognito.actions
17
17
18
18
import aws.sdk.kotlin.services.cognitoidentity.model.GetCredentialsForIdentityRequest
19
19
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
22
21
import aws.smithy.kotlin.runtime.time.Instant
23
22
import com.amplifyframework.auth.cognito.AuthEnvironment
24
- import com.amplifyframework.auth.cognito.helpers.AuthHelper
25
23
import com.amplifyframework.auth.cognito.helpers.SessionHelper
26
24
import com.amplifyframework.auth.exceptions.NotAuthorizedException
27
25
import com.amplifyframework.auth.exceptions.SessionExpiredException
@@ -40,44 +38,27 @@ import com.amplifyframework.statemachine.codegen.events.RefreshSessionEvent
40
38
import kotlin.time.Duration.Companion.seconds
41
39
42
40
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"
46
41
47
42
override fun refreshUserPoolTokensAction (signedInData : SignedInData ) =
48
43
Action <AuthEnvironment >(" RefreshUserPoolTokens" ) { id, dispatcher ->
49
44
logger.verbose(" $id Starting execution" )
50
45
val evt = try {
51
46
val username = signedInData.username
52
47
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)
64
48
val deviceMetadata: DeviceMetadata .Metadata ? = getDeviceMetadata(username)
65
- deviceMetadata?.let { authParameters[KEY_DEVICE_KEY ] = it.deviceKey }
66
- val pinpointEndpointId = getPinpointEndpointId()
67
49
68
- val response = cognitoAuthService.cognitoIdentityProviderClient?.initiateAuth {
69
- authFlow = AuthFlowType . RefreshToken
50
+ val response = cognitoAuthService.cognitoIdentityProviderClient?.getTokensFromRefreshToken {
51
+ refreshToken = tokens.refreshToken
70
52
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
74
55
}
75
56
76
57
val expiresIn = response?.authenticationResult?.expiresIn?.toLong() ? : 0
77
58
val refreshedUserPoolTokens = CognitoUserPoolTokens (
78
59
idToken = response?.authenticationResult?.idToken,
79
60
accessToken = response?.authenticationResult?.accessToken,
80
- refreshToken = tokens.refreshToken,
61
+ refreshToken = response?.authenticationResult?.refreshToken ? : tokens.refreshToken,
81
62
expiration = Instant .now().plus(expiresIn.seconds).epochSeconds
82
63
)
83
64
0 commit comments