From 7b507f3eab9fc6e6a489a37e5fe4f9d2f5df2e4a Mon Sep 17 00:00:00 2001 From: Harsh <6162866+harsh62@users.noreply.github.com> Date: Wed, 15 Nov 2023 13:00:55 -0500 Subject: [PATCH] fix(Auth): Add underlying error details to session error (#3364) * fix(Auth): Add underlying error details to session error * updated unit test target * updating error message --- .../InformSessionError.swift | 3 ++- .../Helpers/FetchAuthSessionOperationHelper.swift | 10 ++++++---- .../CodeGen/Errors/AuthorizationError.swift | 9 ++++++--- .../Helpers/AuthCognitoSignedInSessionHelper.swift | 11 +++++++---- .../AWSAuthFederationToIdentityPoolTests.swift | 3 ++- .../TestHarness/CodableStates/CodableStates.swift | 2 +- 6 files changed, 24 insertions(+), 14 deletions(-) diff --git a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Actions/FetchAuthorizationSession/InformSessionError.swift b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Actions/FetchAuthorizationSession/InformSessionError.swift index c556bf89d4..d21f30363a 100644 --- a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Actions/FetchAuthorizationSession/InformSessionError.swift +++ b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Actions/FetchAuthorizationSession/InformSessionError.swift @@ -23,7 +23,8 @@ struct InformSessionError: Action { switch error { case .service(let serviceError): if isNotAuthorizedError(serviceError) { - event = .init(eventType: .throwError(.sessionExpired)) + event = .init(eventType: .throwError( + .sessionExpired(error: serviceError))) } else { event = .init(eventType: .receivedSessionError(error)) } diff --git a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Operations/Helpers/FetchAuthSessionOperationHelper.swift b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Operations/Helpers/FetchAuthSessionOperationHelper.swift index af3a5c87b5..b9fdbc1d88 100644 --- a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Operations/Helpers/FetchAuthSessionOperationHelper.swift +++ b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Operations/Helpers/FetchAuthSessionOperationHelper.swift @@ -41,9 +41,10 @@ class FetchAuthSessionOperationHelper: DefaultLogger { forceRefresh: forceRefresh) case .error(let error): - if case .sessionExpired = error { + if case .sessionExpired(let error) = error { log.verbose("Session is expired") - let session = AuthCognitoSignedInSessionHelper.makeExpiredSignedInSession() + let session = AuthCognitoSignedInSessionHelper.makeExpiredSignedInSession( + underlyingError: error) return session } else if case .sessionError(_, let credentials) = error { return try await refreshIfRequired( @@ -125,8 +126,9 @@ class FetchAuthSessionOperationHelper: DefaultLogger { return try sessionResultWithFetchError(fetchError, authenticationState: authenticationState, existingCredentials: credentials) - case .sessionExpired: - let session = AuthCognitoSignedInSessionHelper.makeExpiredSignedInSession() + case .sessionExpired(let error): + let session = AuthCognitoSignedInSessionHelper.makeExpiredSignedInSession( + underlyingError: error) return session default: let message = "Unknown error occurred" diff --git a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/StateMachine/CodeGen/Errors/AuthorizationError.swift b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/StateMachine/CodeGen/Errors/AuthorizationError.swift index 771c1aed5a..8c1ed9c1e0 100644 --- a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/StateMachine/CodeGen/Errors/AuthorizationError.swift +++ b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/StateMachine/CodeGen/Errors/AuthorizationError.swift @@ -15,14 +15,17 @@ enum AuthorizationError: Error { case service(error: Swift.Error) case invalidState(message: String) case sessionError(FetchSessionError, AmplifyCredentials) - case sessionExpired + case sessionExpired(error: Error) } extension AuthorizationError: AuthErrorConvertible { var authError: AuthError { switch self { - case .sessionExpired: - return .sessionExpired("", "", nil) + case .sessionExpired(let error): + return .sessionExpired( + "Session expired", + "Invoke Auth.signIn to re-authenticate the user", + error) case .configuration(let message): return .configuration(message, "") case .service(let error): diff --git a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/Helpers/AuthCognitoSignedInSessionHelper.swift b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/Helpers/AuthCognitoSignedInSessionHelper.swift index fbc1cf8d0d..2db23e0ce7 100644 --- a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/Helpers/AuthCognitoSignedInSessionHelper.swift +++ b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/Helpers/AuthCognitoSignedInSessionHelper.swift @@ -32,18 +32,21 @@ struct AuthCognitoSignedInSessionHelper { return authSession } - static func makeExpiredSignedInSession() -> AWSAuthCognitoSession { + static func makeExpiredSignedInSession(underlyingError: Error) -> AWSAuthCognitoSession { let identityIdError = AuthError.sessionExpired( AuthPluginErrorConstants.identityIdSessionExpiredError.errorDescription, - AuthPluginErrorConstants.identityIdSessionExpiredError.recoverySuggestion) + AuthPluginErrorConstants.identityIdSessionExpiredError.recoverySuggestion, + underlyingError) let awsCredentialsError = AuthError.sessionExpired( AuthPluginErrorConstants.awsCredentialsSessionExpiredError.errorDescription, - AuthPluginErrorConstants.awsCredentialsSessionExpiredError.recoverySuggestion) + AuthPluginErrorConstants.awsCredentialsSessionExpiredError.recoverySuggestion, + underlyingError) let tokensError = AuthError.sessionExpired( AuthPluginErrorConstants.cognitoTokensSessionExpiredError.errorDescription, - AuthPluginErrorConstants.cognitoTokensSessionExpiredError.recoverySuggestion) + AuthPluginErrorConstants.cognitoTokensSessionExpiredError.recoverySuggestion, + underlyingError) let authSession = AWSAuthCognitoSession(isSignedIn: true, identityIdResult: .failure(identityIdError), diff --git a/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/AuthorizationTests/AWSAuthFederationToIdentityPoolTests.swift b/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/AuthorizationTests/AWSAuthFederationToIdentityPoolTests.swift index e3612eaf69..604c0b24d8 100644 --- a/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/AuthorizationTests/AWSAuthFederationToIdentityPoolTests.swift +++ b/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/AuthorizationTests/AWSAuthFederationToIdentityPoolTests.swift @@ -85,7 +85,8 @@ class AWSAuthFederationToIdentityPoolTests: BaseAuthorizationTests { AuthorizationState.configured), AuthState.configured( AuthenticationState.signedOut(.testData), - AuthorizationState.error(.sessionExpired)) + AuthorizationState.error(.sessionExpired( + error: NotAuthorizedException(message: "message")))) ] for initialState in statesToTest { diff --git a/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TestHarness/CodableStates/CodableStates.swift b/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TestHarness/CodableStates/CodableStates.swift index aa967a353e..0dc049110d 100644 --- a/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TestHarness/CodableStates/CodableStates.swift +++ b/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TestHarness/CodableStates/CodableStates.swift @@ -107,7 +107,7 @@ extension FetchAuthSessionState: Codable { extension AuthorizationError: Codable { public init(from decoder: Decoder) throws { - self = .sessionExpired + self = .sessionExpired(error: NotAuthorizedException(message: "message")) } public func encode(to encoder: Encoder) throws {