Skip to content

Commit eeeb561

Browse files
authored
fix(auth): Unblock fetchAuthSession call during a signIn flow (#2687)
1 parent 8dfb56a commit eeeb561

File tree

6 files changed

+68
-35
lines changed

6 files changed

+68
-35
lines changed

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/StateMachine/CodeGen/States/AuthorizationState.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ enum AuthorizationState: State {
1313

1414
case configured
1515

16-
case signingIn
17-
1816
case signingOut(AmplifyCredentials?)
1917

2018
case clearingFederation
@@ -47,8 +45,6 @@ extension AuthorizationState {
4745
return "AuthorizationState.notConfigured"
4846
case .configured:
4947
return "AuthorizationState.configured"
50-
case .signingIn:
51-
return "AuthorizationState.signingIn"
5248
case .signingOut:
5349
return "AuthorizationState.signingOut"
5450
case .federatingToIdentityPool:

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/StateMachine/CodeGen/States/DebugInfo/AuthorizationState+Debug.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ extension AuthorizationState: CustomDebugDictionaryConvertible {
1414
switch self {
1515
case .notConfigured,
1616
.configured,
17-
.signingIn,
1817
.signingOut,
1918
.clearingFederation,
2019
.deletingUser:

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/StateMachine/Resolvers/Authorization/AuthorizationState+Resolver.swift

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ extension AuthorizationState {
2020
byApplying event: StateMachineEvent
2121
) -> StateResolution<StateType> {
2222

23+
if let authEvent = event.isAuthenticationEvent {
24+
switch authEvent {
25+
case .signInCompleted(let signedInData):
26+
let action = InitializeFetchAuthSessionWithUserPool(
27+
signedInData: signedInData)
28+
return .init(
29+
newState: .fetchingAuthSessionWithUserPool(.notStarted, signedInData),
30+
actions: [action])
31+
case .error(let error):
32+
return .init(newState: .error(AuthorizationError.service(error: error)))
33+
case .cancelSignIn:
34+
return .init(newState: .configured)
35+
default: break
36+
}
37+
}
38+
2339
switch oldState {
2440
case .notConfigured:
2541
guard let authZEvent = event.isAuthorizationEvent else {
@@ -29,11 +45,6 @@ extension AuthorizationState {
2945

3046
case .configured:
3147

32-
if let authNEvent = event.isAuthenticationEvent,
33-
case .signInRequested = authNEvent {
34-
return .init(newState: .signingIn)
35-
}
36-
3748
if case .fetchUnAuthSession = event.isAuthorizationEvent {
3849
let action = InitializeFetchUnAuthSession()
3950
return .init(newState: .fetchingUnAuthSession(.notStarted), actions: [action])
@@ -57,9 +68,7 @@ extension AuthorizationState {
5768

5869
case .sessionEstablished(let credentials):
5970
if let authNEvent = event.isAuthenticationEvent {
60-
if case .signInRequested = authNEvent {
61-
return .from(.signingIn)
62-
} else if case .signOutRequested = authNEvent {
71+
if case .signOutRequested = authNEvent {
6372
return .from(.signingOut(credentials))
6473
} else if case .clearFederationToIdentityPool = authNEvent {
6574
return .from(.clearingFederation)
@@ -126,23 +135,6 @@ extension AuthorizationState {
126135
existingCredentials: credentials),
127136
actions: resolution.actions)
128137

129-
case .signingIn:
130-
if let authEvent = event.isAuthenticationEvent {
131-
switch authEvent {
132-
case .signInCompleted(let signedInData):
133-
let action = InitializeFetchAuthSessionWithUserPool(
134-
signedInData: signedInData)
135-
return .init(newState: .fetchingAuthSessionWithUserPool(.notStarted, signedInData),
136-
actions: [action])
137-
case .error(let error):
138-
return .init(newState: .error(AuthorizationError.service(error: error)))
139-
case .cancelSignIn:
140-
return .init(newState: .configured)
141-
default: return .from(.signingIn)
142-
}
143-
}
144-
return .from(.signingIn)
145-
146138
case .signingOut(let credentials):
147139
if let signOutEvent = event.isSignOutEvent,
148140
case .signedOutSuccess = signOutEvent {
@@ -267,7 +259,7 @@ extension AuthorizationState {
267259

268260
switch authNEvent {
269261
case .signInRequested:
270-
return .from(.signingIn)
262+
return .from(.configured)
271263
case .signOutRequested:
272264
return .from(.signingOut(nil))
273265
case .cancelSignIn:

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,4 +690,52 @@ class AWSAuthFetchSignInSessionOperationTests: BaseAuthorizationTests {
690690
return
691691
}
692692
}
693+
694+
/// Test fetchAuthSession on a in progress signIn. The library is waiting for confirm signIn to complete and require user input
695+
///
696+
/// - Given: Given an auth plugin with waiting for confirm signIn input and identityPool enabled
697+
/// - When:
698+
/// - I invoke fetchAuthSession
699+
/// - Then:
700+
/// - I should get an a valid session with the following details:
701+
/// - isSignedIn = false
702+
/// - aws credentails = valid values
703+
/// - identity id = valid values
704+
///
705+
func testSessionWhenWaitingConfirmSignIn() async throws {
706+
let signInMethod = SignInMethod.apiBased(.userSRP)
707+
let challenge = SignInChallengeState.waitingForAnswer(.testData, signInMethod)
708+
let initialState = AuthState.configured(
709+
AuthenticationState.signingIn(
710+
.resolvingChallenge(challenge, .smsMfa, signInMethod)),
711+
AuthorizationState.configured
712+
)
713+
714+
let getId: MockIdentity.MockGetIdResponse = { _ in
715+
return .init(identityId: "mockIdentityId")
716+
}
717+
718+
let getCredentials: MockIdentity.MockGetCredentialsResponse = { _ in
719+
let credentials = CognitoIdentityClientTypes.Credentials(accessKeyId: "accessKey",
720+
expiration: Date(),
721+
secretKey: "secret",
722+
sessionToken: "session")
723+
return .init(credentials: credentials, identityId: "responseIdentityID")
724+
}
725+
726+
let plugin = configurePluginWith(identityPool: {
727+
MockIdentity(mockGetIdResponse: getId,
728+
mockGetCredentialsResponse: getCredentials) },
729+
initialState: initialState)
730+
731+
let session = try await plugin.fetchAuthSession(options: AuthFetchSessionRequest.Options())
732+
XCTAssertFalse(session.isSignedIn)
733+
734+
let creds = try? (session as? AuthAWSCredentialsProvider)?.getAWSCredentials().get()
735+
XCTAssertNotNil(creds?.accessKeyId)
736+
XCTAssertNotNil(creds?.secretAccessKey)
737+
738+
let identityId = try? (session as? AuthCognitoIdentityProvider)?.getIdentityId().get()
739+
XCTAssertNotNil(identityId)
740+
}
693741
}

AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TestHarness/CodableStates/AuthorizationState+Codable.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ extension AuthorizationState: Codable {
8282

8383
} else if type == "AuthorizationState.Configured" {
8484
self = .configured
85-
} else if type == "AuthorizationState.SigningIn" {
86-
self = .signingIn
8785
} else {
8886
fatalError("Decoding not supported")
8987
}

AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TestResources/states/SigningIn_SigningIn.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
}
2020
},
2121
"AuthorizationState": {
22-
"type": "AuthorizationState.SigningIn"
22+
"type": "AuthorizationState.Configured"
2323
}
24-
}
24+
}

0 commit comments

Comments
 (0)