Skip to content

Commit 3abc90b

Browse files
authored
fix(Auth): Resolve AuthZ state correctly when in error state (#3762)
1 parent 540fba2 commit 3abc90b

File tree

2 files changed

+62
-11
lines changed

2 files changed

+62
-11
lines changed

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,21 @@ class FetchAuthSessionOperationHelper: DefaultLogger {
6666
authStateMachine: AuthStateMachine,
6767
forceRefresh: Bool) async throws -> AuthSession {
6868

69-
var event: AuthorizationEvent
7069
if forceRefresh || !credentials.areValid() {
71-
if case .identityPoolWithFederation(
72-
let federatedToken,
73-
let identityId,
74-
_
75-
) = credentials {
76-
event = AuthorizationEvent(
77-
eventType: .startFederationToIdentityPool(federatedToken, identityId)
78-
)
79-
} else {
70+
var event: AuthorizationEvent
71+
switch credentials {
72+
case .identityPoolWithFederation(let federatedToken, let identityId, _):
73+
event = AuthorizationEvent(eventType: .startFederationToIdentityPool(federatedToken, identityId))
74+
case .noCredentials:
75+
event = AuthorizationEvent(eventType: .fetchUnAuthSession)
76+
case .userPoolOnly, .identityPoolOnly, .userPoolAndIdentityPool:
8077
event = AuthorizationEvent(eventType: .refreshSession(forceRefresh))
8178
}
8279
await authStateMachine.send(event)
8380
return try await listenForSession(authStateMachine: authStateMachine)
81+
} else {
82+
return credentials.cognitoSession
8483
}
85-
return credentials.cognitoSession
8684
}
8785

8886
func listenForSession(authStateMachine: AuthStateMachine) async throws -> AuthSession {

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,59 @@ class AWSAuthFetchSignInSessionOperationTests: BaseAuthorizationTests {
584584
}
585585
}
586586

587+
/// Test fetch session with authorization in error state
588+
///
589+
/// - Given: An auth plugin with signedOut state
590+
/// - When:
591+
/// - I invoke fetchAuthSession and mock notSignedIn for getTokens
592+
/// - Then:
593+
/// - I should get an a valid session with the following details:
594+
/// - isSignedIn = false
595+
/// - aws credentails = valid values
596+
/// - identity id = valid values
597+
/// - cognito tokens = signedOut
598+
///
599+
func testFetchSessionWithAuthorizationInErrorState() async throws {
600+
601+
let initialState = AuthState.configured(
602+
AuthenticationState.signedOut(.testData),
603+
AuthorizationState.error(.sessionError(.service(AuthError.unknown("error")), .noCredentials)))
604+
605+
let getId: MockIdentity.MockGetIdResponse = { _ in
606+
return .init(identityId: "mockIdentityId")
607+
}
608+
609+
let getCredentials: MockIdentity.MockGetCredentialsResponse = { _ in
610+
let credentials = CognitoIdentityClientTypes.Credentials(accessKeyId: "accessKey",
611+
expiration: Date(),
612+
secretKey: "secret",
613+
sessionToken: "session")
614+
return .init(credentials: credentials, identityId: "responseIdentityID")
615+
}
616+
617+
let plugin = configurePluginWith(identityPool: {
618+
MockIdentity(mockGetIdResponse: getId,
619+
mockGetCredentialsResponse: getCredentials) },
620+
initialState: initialState)
621+
622+
let session = try await plugin.fetchAuthSession(options: AuthFetchSessionRequest.Options())
623+
XCTAssertFalse(session.isSignedIn)
624+
625+
let creds = try? (session as? AuthAWSCredentialsProvider)?.getAWSCredentials().get()
626+
XCTAssertNotNil(creds?.accessKeyId)
627+
XCTAssertNotNil(creds?.secretAccessKey)
628+
629+
let identityId = try? (session as? AuthCognitoIdentityProvider)?.getIdentityId().get()
630+
XCTAssertNotNil(identityId)
631+
632+
let tokensResult = (session as? AuthCognitoTokensProvider)?.getCognitoTokens()
633+
guard case .failure(let error) = tokensResult,
634+
case .signedOut = error else {
635+
XCTFail("Should return signed out error")
636+
return
637+
}
638+
}
639+
587640
/// Test signedOut state credential refresh
588641
///
589642
/// - Given: Given an auth plugin with signedOut state and expired AWS credentials

0 commit comments

Comments
 (0)