Skip to content

Commit

Permalink
fix(Auth): throw correct error for an unknown session error (#3591)
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh62 authored Apr 8, 2024
1 parent 235b49f commit 68c7eff
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

0 comments on commit 68c7eff

Please sign in to comment.