-
Notifications
You must be signed in to change notification settings - Fork 127
feat(auth): Refresh Token Rotation #3112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d8bdd32
updated refresh token action to use GetTokensFromRefreshToken API ins…
ekjotmultani 9adfe5b
added unit tests and GetTokensFromRefreshToken to cognito mock factories
ekjotmultani cf2a9a9
updated generated json for tests to use new API
ekjotmultani 97ef445
fixed typo in GetTokensFromRefreshToken (no 's' at the end)
ekjotmultani 739ef8b
added test cases for device key included and not, as well as a null r…
ekjotmultani 855c746
updated smithy version to match kotlin sdk bump
ekjotmultani 24cfc12
added comment clarifying refresh token logic
ekjotmultani f69fbde
ktlint formatting conformity
ekjotmultani 1dbde13
Merge branch 'main' into ekjotmultani/refresh-token-rotation
mattcreaser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
...test/java/com/amplifyframework/auth/cognito/actions/FetchAuthSessionCognitoActionsTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| /* | ||
| * Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package com.amplifyframework.auth.cognito.actions | ||
|
|
||
| import androidx.test.core.app.ApplicationProvider | ||
| import aws.sdk.kotlin.services.cognitoidentityprovider.CognitoIdentityProviderClient | ||
| import aws.sdk.kotlin.services.cognitoidentityprovider.model.AuthenticationResultType | ||
| import aws.sdk.kotlin.services.cognitoidentityprovider.model.GetTokensFromRefreshTokenResponse | ||
| import aws.sdk.kotlin.services.cognitoidentityprovider.model.NotAuthorizedException | ||
| import com.amplifyframework.auth.cognito.AWSCognitoAuthService | ||
| import com.amplifyframework.auth.cognito.AuthConfiguration | ||
| import com.amplifyframework.auth.cognito.AuthEnvironment | ||
| import com.amplifyframework.auth.cognito.StoreClientBehavior | ||
| import com.amplifyframework.auth.cognito.mockSignedInData | ||
| import com.amplifyframework.logging.Logger | ||
| import com.amplifyframework.statemachine.EventDispatcher | ||
| import com.amplifyframework.statemachine.StateMachineEvent | ||
| import com.amplifyframework.statemachine.codegen.data.AmplifyCredential | ||
| import com.amplifyframework.statemachine.codegen.data.CognitoUserPoolTokens | ||
| import com.amplifyframework.statemachine.codegen.data.CredentialType | ||
| import com.amplifyframework.statemachine.codegen.data.DeviceMetadata | ||
| import com.amplifyframework.statemachine.codegen.data.UserPoolConfiguration | ||
| import com.amplifyframework.statemachine.codegen.events.AuthorizationEvent | ||
| import com.amplifyframework.statemachine.codegen.events.RefreshSessionEvent | ||
| import io.kotest.matchers.shouldBe | ||
| import io.kotest.matchers.types.shouldBeInstanceOf | ||
| import io.mockk.Runs | ||
| import io.mockk.coEvery | ||
| import io.mockk.every | ||
| import io.mockk.just | ||
| import io.mockk.mockk | ||
| import io.mockk.slot | ||
| import kotlinx.coroutines.test.runTest | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RobolectricTestRunner | ||
|
|
||
| @RunWith(RobolectricTestRunner::class) | ||
| class FetchAuthSessionCognitoActionsTest { | ||
|
|
||
| private val pool = mockk<UserPoolConfiguration> { | ||
| every { appClient } returns "client" | ||
| every { appClientSecret } returns "secret" | ||
| every { region } returns "us-east-1" | ||
| every { poolId } returns "pool_id" | ||
| } | ||
| private val configuration = mockk<AuthConfiguration> { | ||
| every { userPool } returns pool | ||
| every { identityPool } returns null | ||
| } | ||
| private val cognitoAuthService = mockk<AWSCognitoAuthService>() | ||
| private val credentialStoreClient = mockk<StoreClientBehavior> { | ||
| coEvery { loadCredentials(any<CredentialType.Device>()) } returns AmplifyCredential.DeviceData( | ||
| DeviceMetadata.Metadata(deviceKey = "device_key", deviceGroupKey = "device_group") | ||
| ) | ||
| } | ||
| private val logger = mockk<Logger>(relaxed = true) | ||
| private val cognitoIdentityProviderClientMock = mockk<CognitoIdentityProviderClient>() | ||
|
|
||
| private val capturedEvent = slot<StateMachineEvent>() | ||
| private val dispatcher = mockk<EventDispatcher> { | ||
| every { send(capture(capturedEvent)) } just Runs | ||
| } | ||
|
|
||
| private lateinit var authEnvironment: AuthEnvironment | ||
|
|
||
| @Before | ||
| fun setup() { | ||
| every { cognitoAuthService.cognitoIdentityProviderClient }.answers { cognitoIdentityProviderClientMock } | ||
| authEnvironment = AuthEnvironment( | ||
| ApplicationProvider.getApplicationContext(), | ||
| configuration, | ||
| cognitoAuthService, | ||
| credentialStoreClient, | ||
| null, | ||
| null, | ||
| logger | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `refreshUserPoolTokensAction calls getTokensFromRefreshToken and handles token rotation`() = runTest { | ||
| val originalRefreshToken = "original_refresh_token" | ||
| val newRefreshToken = "new_refresh_token" | ||
|
|
||
| coEvery { cognitoIdentityProviderClientMock.getTokensFromRefreshToken(any()) } returns GetTokensFromRefreshTokenResponse { | ||
| authenticationResult = AuthenticationResultType { | ||
| this.accessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o" | ||
| this.idToken = "id_token" | ||
| this.refreshToken = newRefreshToken | ||
| this.expiresIn = 3600 | ||
| } | ||
| } | ||
|
|
||
| val signedInData = mockSignedInData( | ||
| username = "username", | ||
| cognitoUserPoolTokens = CognitoUserPoolTokens( | ||
| accessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o", | ||
| idToken = "old_id", | ||
| refreshToken = originalRefreshToken, | ||
| expiration = 0 | ||
| ) | ||
| ) | ||
|
|
||
| FetchAuthSessionCognitoActions.refreshUserPoolTokensAction(signedInData).execute(dispatcher, authEnvironment) | ||
|
|
||
| val event = capturedEvent.captured.shouldBeInstanceOf<RefreshSessionEvent>() | ||
| val refreshedData = event.eventType.shouldBeInstanceOf<RefreshSessionEvent.EventType.Refreshed>().signedInData | ||
| refreshedData.cognitoUserPoolTokens.refreshToken shouldBe newRefreshToken | ||
| } | ||
|
|
||
| @Test | ||
| fun `refreshUserPoolTokensAction falls back to original refresh token when rotation is not enabled`() = runTest { | ||
| val originalRefreshToken = "original_refresh_token" | ||
|
|
||
| coEvery { cognitoIdentityProviderClientMock.getTokensFromRefreshToken(any()) } returns GetTokensFromRefreshTokenResponse { | ||
| authenticationResult = AuthenticationResultType { | ||
| this.accessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o" | ||
| this.idToken = "id_token" | ||
| this.refreshToken = null | ||
| this.expiresIn = 3600 | ||
| } | ||
| } | ||
|
|
||
| val signedInData = mockSignedInData( | ||
| username = "username", | ||
| cognitoUserPoolTokens = CognitoUserPoolTokens( | ||
| accessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o", | ||
| idToken = "old_id", | ||
| refreshToken = originalRefreshToken, | ||
| expiration = 0 | ||
| ) | ||
| ) | ||
|
|
||
| FetchAuthSessionCognitoActions.refreshUserPoolTokensAction(signedInData).execute(dispatcher, authEnvironment) | ||
|
|
||
| val event = capturedEvent.captured.shouldBeInstanceOf<RefreshSessionEvent>() | ||
| val refreshedData = event.eventType.shouldBeInstanceOf<RefreshSessionEvent.EventType.Refreshed>().signedInData | ||
| refreshedData.cognitoUserPoolTokens.refreshToken shouldBe originalRefreshToken | ||
| } | ||
|
|
||
| @Test | ||
| fun `refreshUserPoolTokensAction handles NotAuthorizedException`() = runTest { | ||
| coEvery { cognitoIdentityProviderClientMock.getTokensFromRefreshToken(any()) } throws NotAuthorizedException { | ||
| message = "Token expired" | ||
| } | ||
|
|
||
| val signedInData = mockSignedInData( | ||
| username = "username", | ||
| cognitoUserPoolTokens = CognitoUserPoolTokens( | ||
| accessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o", | ||
| idToken = "old_id", | ||
| refreshToken = "refresh_token", | ||
| expiration = 0 | ||
| ) | ||
| ) | ||
|
|
||
| FetchAuthSessionCognitoActions.refreshUserPoolTokensAction(signedInData).execute(dispatcher, authEnvironment) | ||
|
|
||
| capturedEvent.captured.shouldBeInstanceOf<AuthorizationEvent>() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.