diff --git a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Operations/Helpers/FetchAuthSessionOperationHelper.swift b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Operations/Helpers/FetchAuthSessionOperationHelper.swift index 7aa1997b72..d39751956a 100644 --- a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Operations/Helpers/FetchAuthSessionOperationHelper.swift +++ b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Operations/Helpers/FetchAuthSessionOperationHelper.swift @@ -157,13 +157,21 @@ class FetchAuthSessionOperationHelper: DefaultLogger { } case .service(let error): - if let authError = (error as? AuthErrorConvertible)?.authError { - let session = AWSAuthCognitoSession(isSignedIn: isSignedIn, - identityIdResult: .failure(authError), - awsCredentialsResult: .failure(authError), - cognitoTokensResult: .failure(authError)) - return session + var authError: AuthError + if let convertedAuthError = (error as? AuthErrorConvertible)?.authError { + authError = convertedAuthError + } else { + authError = AuthError.service( + "Unknown service error occurred", + "See the attached error for more details", + error) } + let session = AWSAuthCognitoSession( + isSignedIn: isSignedIn, + identityIdResult: .failure(authError), + awsCredentialsResult: .failure(authError), + cognitoTokensResult: .failure(authError)) + return session default: break } diff --git a/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/AuthorizationTests/AWSAuthFetchSignInSessionOperationTests.swift b/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/AuthorizationTests/AWSAuthFetchSignInSessionOperationTests.swift index ed107d34d7..9bee6c05b1 100644 --- a/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/AuthorizationTests/AWSAuthFetchSignInSessionOperationTests.swift +++ b/AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/AuthorizationTests/AWSAuthFetchSignInSessionOperationTests.swift @@ -736,4 +736,61 @@ class AWSAuthFetchSignInSessionOperationTests: BaseAuthorizationTests { let identityId = try? (session as? AuthCognitoIdentityProvider)?.getIdentityId().get() XCTAssertNotNil(identityId) } + + /// Test signedIn session with invalid response for aws credentials + /// + /// - Given: Given an auth plugin with signedIn state + /// - When: + /// - I invoke fetchAuthSession and service throws NSError + /// - Then: + /// - I should get an a valid session with the following details: + /// - isSignedIn = true + /// - aws credentails = service error + /// - identity id = service error + /// - cognito tokens = service error + /// + func testSignInSessionWithNSError() async throws { + let initialState = AuthState.configured( + AuthenticationState.signedIn(.testData), + AuthorizationState.sessionEstablished( + AmplifyCredentials.testDataWithExpiredTokens)) + + let initAuth: MockIdentityProvider.MockInitiateAuthResponse = { _ in + return InitiateAuthOutput(authenticationResult: .init(accessToken: "accessToken", + expiresIn: 1000, + idToken: "idToken", + refreshToken: "refreshToke")) + } + + let awsCredentials: MockIdentity.MockGetCredentialsResponse = { _ in + throw NSError(domain: NSURLErrorDomain, code: 1, userInfo: nil) + } + let plugin = configurePluginWith( + userPool: { MockIdentityProvider(mockInitiateAuthResponse: initAuth) }, + identityPool: { MockIdentity(mockGetCredentialsResponse: awsCredentials) }, + initialState: initialState) + + let session = try await plugin.fetchAuthSession(options: AuthFetchSessionRequest.Options()) + + XCTAssertTrue(session.isSignedIn) + let credentialsResult = (session as? AuthAWSCredentialsProvider)?.getAWSCredentials() + guard case .failure(let error) = credentialsResult, case .service = error else { + XCTFail("Should return service error") + return + } + + let identityIdResult = (session as? AuthCognitoIdentityProvider)?.getIdentityId() + guard case .failure(let identityIdError) = identityIdResult, + case .service = identityIdError else { + XCTFail("Should return service error") + return + } + + let tokensResult = (session as? AuthCognitoTokensProvider)?.getCognitoTokens() + guard case .failure(let tokenError) = tokensResult, + case .service = tokenError else { + XCTFail("Should return service error") + return + } + } }