Skip to content

Commit 95b4b91

Browse files
authored
chore(auth): Refresh Token Rotation Test Fix (#4055)
* removed usage of old refresh token, updated test case to properly check for invalidTokens error in case the API returns a null token * updated authfetchsigninsession tests to use GetTokenFromRefreshToken in mock clients * updated further tests to use new API as well as auth hub event handler tests * updated RefreshUserPoolTokens to use GetTokensFromRefreshToken API to enable refresh token rotation, also updated test mock clients and added unit tests * removed refreshAuth function from initiateAuthInput * reintroduced ternary operator to fallback to previous token if a null token is returned * fixed ternary operator location * removed unnecessary test since we don't set tokens to nil anymore
1 parent 91de03a commit 95b4b91

File tree

2 files changed

+2
-40
lines changed

2 files changed

+2
-40
lines changed

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Actions/RefreshAuthorizationSession/UserPool/RefreshUserPoolTokens.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ struct RefreshUserPoolTokens: Action {
6262

6363
guard let authenticationResult = response?.authenticationResult,
6464
let idToken = authenticationResult.idToken,
65-
let accessToken = authenticationResult.accessToken,
66-
let refreshToken = authenticationResult.refreshToken
65+
let accessToken = authenticationResult.accessToken
6766
else {
6867
let event = RefreshSessionEvent(eventType: .throwError(.invalidTokens))
6968
await dispatcher.send(event)
@@ -74,7 +73,7 @@ struct RefreshUserPoolTokens: Action {
7473
let userPoolTokens = AWSCognitoUserPoolTokens(
7574
idToken: idToken,
7675
accessToken: accessToken,
77-
refreshToken: refreshToken
76+
refreshToken: authenticationResult.refreshToken ?? existingTokens.refreshToken
7877
)
7978

8079
let signedInData = SignedInData(

AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/ActionTests/FetchAuthSession/FetchUserPoolTokens/RefreshUserPoolTokensTests.swift

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -185,43 +185,6 @@ class RefreshUserPoolTokensTests: XCTestCase {
185185
userPoolFactory: identityProviderFactory)
186186
)
187187

188-
await fulfillment(
189-
of: [expectation],
190-
timeout: 0.1
191-
)
192-
}
193-
func testRefreshTokenMissing() async {
194-
195-
let expectation = expectation(description: "refreshTokenMissing")
196-
let identityProviderFactory: BasicSRPAuthEnvironment.CognitoUserPoolFactory = {
197-
MockIdentityProvider(
198-
mockGetTokensFromRefreshTokenResponse: { _ in
199-
return GetTokensFromRefreshTokenOutput(
200-
authenticationResult: .init(
201-
accessToken: "accessTokenNew",
202-
expiresIn: 100,
203-
idToken: "idTokenNew",
204-
refreshToken: nil))
205-
}
206-
)
207-
}
208-
209-
let action = RefreshUserPoolTokens(existingSignedIndata: .testData)
210-
211-
await action.execute(
212-
withDispatcher: MockDispatcher { event in
213-
214-
if let userPoolEvent = event as? RefreshSessionEvent,
215-
case let .throwError(error) = userPoolEvent.eventType
216-
{
217-
XCTAssertEqual(error, .invalidTokens)
218-
expectation.fulfill()
219-
}
220-
},
221-
environment: Defaults.makeDefaultAuthEnvironment(
222-
userPoolFactory: identityProviderFactory)
223-
)
224-
225188
await fulfillment(
226189
of: [expectation],
227190
timeout: 0.1

0 commit comments

Comments
 (0)