Skip to content

Commit aa6a474

Browse files
authored
chore(auth): Add more verbose logging for easier debugging (#2713)
1 parent 5e71bf2 commit aa6a474

18 files changed

+109
-61
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import Foundation
99
import Amplify
1010

11-
class FetchAuthSessionOperationHelper {
11+
class FetchAuthSessionOperationHelper: DefaultLogger {
1212

1313
static let expiryBufferInSeconds = TimeInterval.seconds(2 * 60)
1414

@@ -23,20 +23,26 @@ class FetchAuthSessionOperationHelper {
2323
throw error
2424
}
2525

26+
log.verbose("Fetching current state")
2627
switch authorizationState {
2728
case .configured:
2829
// If session has not been established, ask statemachine to invoke fetching
2930
// a fresh session.
31+
log.verbose("No session found, fetching unauth session")
3032
let event = AuthorizationEvent(eventType: .fetchUnAuthSession)
3133
await authStateMachine.send(event)
3234
return try await listenForSession(authStateMachine: authStateMachine)
35+
3336
case .sessionEstablished(let credentials):
37+
log.verbose("Session exists, checking validity")
3438
return try await postAuthSessionEvent(
3539
forCredential: credentials,
3640
authStateMachine: authStateMachine,
3741
forceRefresh: forceRefresh)
42+
3843
case .error(let error):
3944
if case .sessionExpired = error {
45+
log.verbose("Session is expired")
4046
let session = AuthCognitoSignedInSessionHelper.makeExpiredSignedInSession()
4147
return session
4248
} else if case .sessionError(_, let credentials) = error {
@@ -45,6 +51,7 @@ class FetchAuthSessionOperationHelper {
4551
authStateMachine: authStateMachine,
4652
forceRefresh: forceRefresh)
4753
} else {
54+
log.verbose("Session is in error state \(error)")
4855
let event = AuthorizationEvent(eventType: .fetchUnAuthSession)
4956
await authStateMachine.send(event)
5057
return try await listenForSession(authStateMachine: authStateMachine)
@@ -114,6 +121,7 @@ class FetchAuthSessionOperationHelper {
114121
func listenForSession(authStateMachine: AuthStateMachine) async throws -> AuthSession {
115122

116123
let stateSequences = await authStateMachine.listen()
124+
log.verbose("Waiting for session to establish")
117125
for await state in stateSequences {
118126
guard case .configured(let authenticationState, let authorizationState) = state else {
119127
let message = "Auth state machine not in configured state: \(state)"
@@ -138,6 +146,7 @@ class FetchAuthSessionOperationHelper {
138146
func sessionResultWithError(_ error: AuthorizationError,
139147
authenticationState: AuthenticationState)
140148
throws -> AuthSession {
149+
log.verbose("Received error - \(error)")
141150

142151
var isSignedIn = false
143152
if case .signedIn = authenticationState {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import Foundation
99
import Amplify
1010

11-
struct HostedUISignInHelper {
11+
struct HostedUISignInHelper: DefaultLogger {
1212

1313
let request: AuthWebUISignInRequest
1414

@@ -26,11 +26,13 @@ struct HostedUISignInHelper {
2626

2727
func initiateSignIn() async throws -> AuthSignInResult {
2828
try await isValidState()
29+
log.verbose("Start signIn flow")
2930
return try await doSignIn()
3031
}
3132

3233
func isValidState() async throws {
3334
let stateSequences = await authStateMachine.listen()
35+
log.verbose("Wait for a valid state")
3436
for await state in stateSequences {
3537
guard case .configured(let authenticationState, _) = state else {
3638
continue
@@ -70,6 +72,7 @@ struct HostedUISignInHelper {
7072
}
7173
let stateSequences = await authStateMachine.listen()
7274
await sendSignInEvent(oauthConfiguration: oauthConfiguration)
75+
log.verbose("Wait for signIn to complete")
7376
for await state in stateSequences {
7477
guard case .configured(let authNState,
7578
let authZState) = state else { continue }
@@ -128,7 +131,9 @@ struct HostedUISignInHelper {
128131
}
129132

130133
private func waitForSignInCancel() async {
134+
log.verbose("Sending cancel signIn")
131135
await sendCancelSignInEvent()
136+
log.verbose("Wait for signIn to cancel")
132137
let stateSequences = await authStateMachine.listen()
133138
for await state in stateSequences {
134139
guard case .configured(let authenticationState, _) = state else {

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Support/Helpers/UserPoolSignInHelper.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,40 @@ import Foundation
99
import Amplify
1010
import AWSCognitoIdentityProvider
1111

12-
struct UserPoolSignInHelper {
12+
struct UserPoolSignInHelper: DefaultLogger {
1313

1414
static func checkNextStep(_ signInState: SignInState)
1515
throws -> AuthSignInResult? {
1616

17+
log.verbose("Checking next step for: \(signInState)")
18+
1719
if case .signingInWithSRP(let srpState, _) = signInState,
1820
case .error(let signInError) = srpState {
1921
return try validateError(signInError: signInError)
22+
2023
} else if case .signingInWithSRPCustom(let srpState, _) = signInState,
2124
case .error(let signInError) = srpState {
2225
return try validateError(signInError: signInError)
26+
2327
} else if case .signingInViaMigrateAuth(let migratedAuthState, _) = signInState,
2428
case .error(let signInError) = migratedAuthState {
2529
return try validateError(signInError: signInError)
30+
2631
} else if case .signingInWithCustom(let customAuthState, _) = signInState,
2732
case .error(let signInError) = customAuthState {
2833
return try validateError(signInError: signInError)
29-
} else if case .resolvingChallenge(let challengeState, let challengeType, _) = signInState,
30-
case .waitingForAnswer(let challenge, _) = challengeState {
31-
return try validateResult(for: challengeType, with: challenge)
34+
3235
} else if case .signingInWithHostedUI(let hostedUIState) = signInState,
3336
case .error(let hostedUIError) = hostedUIState {
3437
return try validateError(signInError: hostedUIError)
38+
39+
} else if case .resolvingChallenge(let challengeState, _, _) = signInState,
40+
case .error(_, _, let signInError) = challengeState {
41+
return try validateError(signInError: signInError)
42+
43+
} else if case .resolvingChallenge(let challengeState, let challengeType, _) = signInState,
44+
case .waitingForAnswer(let challenge, _) = challengeState {
45+
return try validateResult(for: challengeType, with: challenge)
3546
}
3647
return nil
3748
}

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Task/AWSAuthChangePasswordTask.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Amplify
1010
import AWSPluginsCore
1111
import AWSCognitoIdentityProvider
1212

13-
class AWSAuthChangePasswordTask: AuthChangePasswordTask {
13+
class AWSAuthChangePasswordTask: AuthChangePasswordTask, DefaultLogger {
1414
typealias CognitoUserPoolFactory = () throws -> CognitoUserPoolBehavior
1515

1616
private let request: AuthChangePasswordRequest
@@ -33,10 +33,12 @@ class AWSAuthChangePasswordTask: AuthChangePasswordTask {
3333
}
3434

3535
func execute() async throws {
36+
log.verbose("Starting execution")
3637
do {
3738
await taskHelper.didStateMachineConfigured()
3839
let accessToken = try await taskHelper.getAccessToken()
3940
try await changePassword(with: accessToken)
41+
log.verbose("Received success")
4042
} catch let error as ChangePasswordOutputError {
4143
throw error.authError
4244
} catch let error {
@@ -46,7 +48,9 @@ class AWSAuthChangePasswordTask: AuthChangePasswordTask {
4648

4749
func changePassword(with accessToken: String) async throws {
4850
let userPoolService = try userPoolFactory()
49-
let input = ChangePasswordInput(accessToken: accessToken, previousPassword: request.oldPassword, proposedPassword: request.newPassword)
51+
let input = ChangePasswordInput(accessToken: accessToken,
52+
previousPassword: request.oldPassword,
53+
proposedPassword: request.newPassword)
5054
_ = try await userPoolService.changePassword(input: input)
5155
}
5256
}

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Task/AWSAuthClearFederationToIdentityPoolTask.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public extension HubPayload.EventName.Auth {
1818
static let clearedFederationToIdentityPoolAPI = "Auth.federationToIdentityPoolCleared"
1919
}
2020

21-
public class AWSAuthClearFederationToIdentityPoolTask: AuthClearFederationToIdentityPoolTask {
21+
public class AWSAuthClearFederationToIdentityPoolTask: AuthClearFederationToIdentityPoolTask,
22+
DefaultLogger {
2223
private let authStateMachine: AuthStateMachine
2324
private let clearFederationHelper: ClearFederationOperationHelper
2425
private let taskHelper: AWSAuthTaskHelper
@@ -34,7 +35,10 @@ public class AWSAuthClearFederationToIdentityPoolTask: AuthClearFederationToIden
3435
}
3536

3637
public func execute() async throws {
38+
log.verbose("Starting execution")
3739
await taskHelper.didStateMachineConfigured()
38-
return try await clearFederationHelper.clearFederation(authStateMachine)
40+
try await clearFederationHelper.clearFederation(authStateMachine)
41+
log.verbose("Cleared federation")
42+
return
3943
}
4044
}

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Task/AWSAuthConfirmResetPasswordTask.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import AWSPluginsCore
1111
import ClientRuntime
1212
import AWSCognitoIdentityProvider
1313

14-
class AWSAuthConfirmResetPasswordTask: AuthConfirmResetPasswordTask {
14+
class AWSAuthConfirmResetPasswordTask: AuthConfirmResetPasswordTask, DefaultLogger {
1515
private let request: AuthConfirmResetPasswordRequest
1616
private let environment: AuthEnvironment
1717
private let authConfiguration: AuthConfiguration
@@ -27,10 +27,12 @@ class AWSAuthConfirmResetPasswordTask: AuthConfirmResetPasswordTask {
2727
}
2828

2929
func execute() async throws {
30+
log.verbose("Starting execution")
3031
if let validationError = request.hasError() {
3132
throw validationError
3233
}
3334
do {
35+
3436
try await confirmResetPassword()
3537
} catch let error as ConfirmForgotPasswordOutputError {
3638
throw error.authError

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Task/AWSAuthConfirmSignInTask.swift

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Foundation
88
import Amplify
99
import AWSPluginsCore
1010

11-
class AWSAuthConfirmSignInTask: AuthConfirmSignInTask {
11+
class AWSAuthConfirmSignInTask: AuthConfirmSignInTask, DefaultLogger {
1212

1313
private let request: AuthConfirmSignInRequest
1414
private let authStateMachine: AuthStateMachine
@@ -29,7 +29,7 @@ class AWSAuthConfirmSignInTask: AuthConfirmSignInTask {
2929
}
3030

3131
func execute() async throws -> AuthSignInResult {
32-
32+
log.verbose("Starting execution")
3333
await taskHelper.didStateMachineConfigured()
3434

3535
//Check if we have a user pool configuration
@@ -48,8 +48,6 @@ class AWSAuthConfirmSignInTask: AuthConfirmSignInTask {
4848
"User is not attempting signIn operation",
4949
AuthPluginErrorConstants.invalidStateError, nil)
5050

51-
52-
5351
guard case .configured(let authNState, _) = await authStateMachine.currentState,
5452
case .signingIn(let signInState) = authNState,
5553
case .resolvingChallenge(let challengeState, _, _) = signInState else {
@@ -58,12 +56,14 @@ class AWSAuthConfirmSignInTask: AuthConfirmSignInTask {
5856

5957
switch challengeState {
6058
case .waitingForAnswer, .error:
59+
log.verbose("Sending confirm signIn event: \(challengeState)")
6160
await sendConfirmSignInEvent()
6261
default:
6362
throw invalidStateError
6463
}
6564

6665
let stateSequences = await authStateMachine.listen()
66+
log.verbose("Waiting for response")
6767
for await state in stateSequences {
6868
guard case .configured(let authNState, let authZState) = state else {
6969
continue
@@ -72,38 +72,17 @@ class AWSAuthConfirmSignInTask: AuthConfirmSignInTask {
7272
case .signedIn:
7373
if case .sessionEstablished = authZState {
7474
return AuthSignInResult(nextStep: .done)
75+
} else {
76+
log.verbose("Signed In, waiting for authorization to complete")
7577
}
7678
case .error(let error):
7779
throw AuthError.unknown("Sign in reached an error state", error)
7880

7981
case .signingIn(let signInState):
80-
if case .resolvingChallenge(let challengeState, _, _) = signInState,
81-
case .error(_, _, let signInError) = challengeState {
82-
let authError = signInError.authError
83-
if case .service(_, _, let serviceError) = authError,
84-
let cognitoError = serviceError as? AWSCognitoAuthError,
85-
case .passwordResetRequired = cognitoError {
86-
return AuthSignInResult(nextStep: .resetPassword(nil))
87-
88-
} else if case .service(_, _, let serviceError) = authError,
89-
let cognitoError = serviceError as? AWSCognitoAuthError,
90-
case .userNotConfirmed = cognitoError {
91-
return AuthSignInResult(nextStep: .confirmSignUp(nil))
92-
} else {
93-
throw authError
94-
}
95-
} else if case .resolvingChallenge(let challengeState, _, _) = signInState {
96-
switch challengeState {
97-
case .waitingForAnswer:
98-
guard let result = try UserPoolSignInHelper.checkNextStep(signInState) else {
99-
continue
100-
}
101-
return result
102-
103-
default:
104-
continue
105-
}
82+
guard let result = try UserPoolSignInHelper.checkNextStep(signInState) else {
83+
continue
10684
}
85+
return result
10786
case .notConfigured:
10887
throw AuthError.configuration(
10988
"UserPool configuration is missing",

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Task/AWSAuthConfirmSignUpTask.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Foundation
99
import Amplify
1010
import AWSCognitoIdentityProvider
1111

12-
class AWSAuthConfirmSignUpTask: AuthConfirmSignUpTask {
12+
class AWSAuthConfirmSignUpTask: AuthConfirmSignUpTask, DefaultLogger {
1313

1414
private let request: AuthConfirmSignUpRequest
1515
private let authEnvironment: AuthEnvironment
@@ -24,17 +24,18 @@ class AWSAuthConfirmSignUpTask: AuthConfirmSignUpTask {
2424
}
2525

2626
func execute() async throws -> AuthSignUpResult {
27+
log.verbose("Starting execution")
2728
try request.hasError()
2829
let userPoolEnvironment = authEnvironment.userPoolEnvironment
2930
do {
30-
3131
let metadata = (request.options.pluginOptions as? AWSAuthConfirmSignUpOptions)?.metadata
3232
let client = try userPoolEnvironment.cognitoUserPoolFactory()
3333
let input = ConfirmSignUpInput(username: request.username,
3434
confirmationCode: request.code,
3535
clientMetadata: metadata,
3636
environment: userPoolEnvironment)
3737
_ = try await client.confirmSignUp(input: input)
38+
log.verbose("Received success")
3839
return AuthSignUpResult(.done)
3940
} catch let error as AuthError {
4041
throw error

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Task/AWSAuthDeleteUserTask.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Foundation
99
import Amplify
1010
import AWSPluginsCore
1111

12-
class AWSAuthDeleteUserTask: AuthDeleteUserTask {
12+
class AWSAuthDeleteUserTask: AuthDeleteUserTask, DefaultLogger {
1313
private let authStateMachine: AuthStateMachine
1414
private let taskHelper: AWSAuthTaskHelper
1515
private let configuration: AuthConfiguration
@@ -26,12 +26,15 @@ class AWSAuthDeleteUserTask: AuthDeleteUserTask {
2626
}
2727

2828
func execute() async throws {
29+
log.verbose("Starting execution")
2930
await taskHelper.didStateMachineConfigured()
3031
let accessToken = try await taskHelper.getAccessToken()
3132

3233
do {
3334
try await deleteUser(with: accessToken)
35+
log.verbose("Received Success")
3436
} catch {
37+
log.verbose("Delete user failed, reconfiguring auth state. \(error)")
3538
await waitForReConfigure()
3639
throw error
3740
}
@@ -41,6 +44,7 @@ class AWSAuthDeleteUserTask: AuthDeleteUserTask {
4144
let stateSequences = await authStateMachine.listen()
4245
let deleteUserEvent = DeleteUserEvent(eventType: .deleteUser(token))
4346
await authStateMachine.send(deleteUserEvent)
47+
log.verbose("Waiting for delete user to complete")
4448
for await state in stateSequences {
4549
guard case .configured(let authNState, _) = state else {
4650
let error = AuthError.invalidState("Auth state should be in configured state and authentication state should be in deleting user state", AuthPluginErrorConstants.invalidStateError, nil)
@@ -63,6 +67,7 @@ class AWSAuthDeleteUserTask: AuthDeleteUserTask {
6367
}
6468

6569
private func waitForReConfigure() async {
70+
6671
let event = AuthEvent(eventType: .reconfigure(configuration))
6772
await authStateMachine.send(event)
6873
await taskHelper.didStateMachineConfigured()

0 commit comments

Comments
 (0)