Skip to content

Commit 99a521c

Browse files
authored
fix(auth): add checks against random logouts (#4099)
* fix: only throw session expired for cognito provider * add more protection against random logouts * fix unit tests * fix swift format * Update AuthenticationProviderDeleteUserTests.swift * reverting protected data changes
1 parent 10c601c commit 99a521c

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Actions/FetchAuthorizationSession/InformSessionError.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct InformSessionError: Action {
2121
logVerbose("\(#fileID) Starting execution", environment: environment)
2222
let event: AuthorizationEvent = switch error {
2323
case .service(let serviceError):
24-
if isNotAuthorizedError(serviceError) {
24+
if serviceError is AWSCognitoIdentityProvider.NotAuthorizedException {
2525
.init(eventType: .throwError(
2626
.sessionExpired(error: serviceError)))
2727
} else {
@@ -34,11 +34,6 @@ struct InformSessionError: Action {
3434
logVerbose("\(#fileID) Sending event \(event.type)", environment: environment)
3535
await dispatcher.send(event)
3636
}
37-
38-
func isNotAuthorizedError(_ error: Error) -> Bool {
39-
error is AWSCognitoIdentity.NotAuthorizedException
40-
|| error is AWSCognitoIdentityProvider.NotAuthorizedException
41-
}
4237
}
4338

4439
extension InformSessionError: DefaultLogger {

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/Helpers/AuthCognitoTokens+Validation.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@ extension AWSCognitoUserPoolTokens {
1313

1414
func doesExpire(in seconds: TimeInterval = 0) -> Bool {
1515

16-
let currentTime = Date(timeIntervalSinceNow: seconds)
1716
guard let idTokenClaims = try? AWSAuthService().getTokenClaims(tokenString: idToken).get(),
1817
let accessTokenClaims = try? AWSAuthService().getTokenClaims(tokenString: accessToken).get(),
1918
let idTokenExpiration = idTokenClaims["exp"]?.doubleValue,
2019
let accessTokenExpiration = accessTokenClaims["exp"]?.doubleValue
2120
else {
22-
return currentTime > expiration
21+
// If token parsing fails, return as expired, to just force refresh
22+
return true
2323
}
2424

2525
let idTokenExpiry = Date(timeIntervalSince1970: idTokenExpiration)
2626
let accessTokenExpiry = Date(timeIntervalSince1970: accessTokenExpiration)
2727

28+
let currentTime = Date(timeIntervalSinceNow: seconds)
2829
return currentTime > idTokenExpiry || currentTime > accessTokenExpiry
2930
}
3031

AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/ClientBehaviorTests/AuthenticationProviderDeleteUserTests.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,17 @@ class AuthenticationProviderDeleteUserTests: BasePluginTest {
141141
mockIdentityProvider = MockIdentityProvider(
142142
mockRevokeTokenResponse: { _ in
143143
RevokeTokenOutput()
144-
}, mockGlobalSignOutResponse: { _ in
144+
},
145+
mockGetTokensFromRefreshTokenResponse: { _ in
146+
return GetTokensFromRefreshTokenOutput(
147+
authenticationResult: .init(
148+
accessToken: "accessTokenNew",
149+
expiresIn: 100,
150+
idToken: "idTokenNew",
151+
refreshToken: "refreshTokenNew"
152+
))
153+
},
154+
mockGlobalSignOutResponse: { _ in
145155
GlobalSignOutOutput()
146156
},
147157
mockDeleteUserOutput: { _ in

0 commit comments

Comments
 (0)