Skip to content

Commit 68c7eff

Browse files
authored
fix(Auth): throw correct error for an unknown session error (#3591)
1 parent 235b49f commit 68c7eff

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Operations/Helpers/FetchAuthSessionOperationHelper.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,21 @@ class FetchAuthSessionOperationHelper: DefaultLogger {
157157
}
158158

159159
case .service(let error):
160-
if let authError = (error as? AuthErrorConvertible)?.authError {
161-
let session = AWSAuthCognitoSession(isSignedIn: isSignedIn,
162-
identityIdResult: .failure(authError),
163-
awsCredentialsResult: .failure(authError),
164-
cognitoTokensResult: .failure(authError))
165-
return session
160+
var authError: AuthError
161+
if let convertedAuthError = (error as? AuthErrorConvertible)?.authError {
162+
authError = convertedAuthError
163+
} else {
164+
authError = AuthError.service(
165+
"Unknown service error occurred",
166+
"See the attached error for more details",
167+
error)
166168
}
169+
let session = AWSAuthCognitoSession(
170+
isSignedIn: isSignedIn,
171+
identityIdResult: .failure(authError),
172+
awsCredentialsResult: .failure(authError),
173+
cognitoTokensResult: .failure(authError))
174+
return session
167175
default: break
168176

169177
}

AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/AuthorizationTests/AWSAuthFetchSignInSessionOperationTests.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,4 +736,61 @@ class AWSAuthFetchSignInSessionOperationTests: BaseAuthorizationTests {
736736
let identityId = try? (session as? AuthCognitoIdentityProvider)?.getIdentityId().get()
737737
XCTAssertNotNil(identityId)
738738
}
739+
740+
/// Test signedIn session with invalid response for aws credentials
741+
///
742+
/// - Given: Given an auth plugin with signedIn state
743+
/// - When:
744+
/// - I invoke fetchAuthSession and service throws NSError
745+
/// - Then:
746+
/// - I should get an a valid session with the following details:
747+
/// - isSignedIn = true
748+
/// - aws credentails = service error
749+
/// - identity id = service error
750+
/// - cognito tokens = service error
751+
///
752+
func testSignInSessionWithNSError() async throws {
753+
let initialState = AuthState.configured(
754+
AuthenticationState.signedIn(.testData),
755+
AuthorizationState.sessionEstablished(
756+
AmplifyCredentials.testDataWithExpiredTokens))
757+
758+
let initAuth: MockIdentityProvider.MockInitiateAuthResponse = { _ in
759+
return InitiateAuthOutput(authenticationResult: .init(accessToken: "accessToken",
760+
expiresIn: 1000,
761+
idToken: "idToken",
762+
refreshToken: "refreshToke"))
763+
}
764+
765+
let awsCredentials: MockIdentity.MockGetCredentialsResponse = { _ in
766+
throw NSError(domain: NSURLErrorDomain, code: 1, userInfo: nil)
767+
}
768+
let plugin = configurePluginWith(
769+
userPool: { MockIdentityProvider(mockInitiateAuthResponse: initAuth) },
770+
identityPool: { MockIdentity(mockGetCredentialsResponse: awsCredentials) },
771+
initialState: initialState)
772+
773+
let session = try await plugin.fetchAuthSession(options: AuthFetchSessionRequest.Options())
774+
775+
XCTAssertTrue(session.isSignedIn)
776+
let credentialsResult = (session as? AuthAWSCredentialsProvider)?.getAWSCredentials()
777+
guard case .failure(let error) = credentialsResult, case .service = error else {
778+
XCTFail("Should return service error")
779+
return
780+
}
781+
782+
let identityIdResult = (session as? AuthCognitoIdentityProvider)?.getIdentityId()
783+
guard case .failure(let identityIdError) = identityIdResult,
784+
case .service = identityIdError else {
785+
XCTFail("Should return service error")
786+
return
787+
}
788+
789+
let tokensResult = (session as? AuthCognitoTokensProvider)?.getCognitoTokens()
790+
guard case .failure(let tokenError) = tokensResult,
791+
case .service = tokenError else {
792+
XCTFail("Should return service error")
793+
return
794+
}
795+
}
739796
}

0 commit comments

Comments
 (0)