Skip to content

Commit 31ce179

Browse files
authored
chore: kickoff release
2 parents e43c65c + b31292a commit 31ce179

File tree

10 files changed

+211
-554
lines changed

10 files changed

+211
-554
lines changed

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Models/AuthFlowType.swift

+15
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ public enum AuthFlowType {
3434
/// - `preferredFirstFactor`: the auth factor type the user should begin signing with if available. If the preferred first factor is not available, the flow would fallback to provide available first factors.
3535
case userAuth(preferredFirstFactor: AuthFactorType?)
3636

37+
internal init?(rawValue: String) {
38+
switch rawValue {
39+
case "CUSTOM_AUTH":
40+
self = .customWithSRP
41+
case "USER_SRP_AUTH":
42+
self = .userSRP
43+
case "USER_PASSWORD_AUTH":
44+
self = .userPassword
45+
case "USER_AUTH":
46+
self = .userAuth
47+
default:
48+
return nil
49+
}
50+
}
51+
3752
var rawValue: String {
3853
switch self {
3954
case .custom, .customWithSRP, .customWithoutSRP:

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Models/Errors/AWSCognitoAuthError.swift

+84-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// SPDX-License-Identifier: Apache-2.0
66
//
77

8+
import Foundation
9+
810
public enum AWSCognitoAuthError: Error {
911

1012
/// User not found in the system.
@@ -13,7 +15,7 @@ public enum AWSCognitoAuthError: Error {
1315
/// User not confirmed in the system.
1416
case userNotConfirmed
1517

16-
/// Username does not exists in the system.
18+
/// Username already exists in the system.
1719
case usernameExists
1820

1921
/// Alias already exists in the system.
@@ -40,7 +42,7 @@ public enum AWSCognitoAuthError: Error {
4042
/// Amazon Cognito cannot find a multi-factor authentication (MFA) method.
4143
case mfaMethodNotFound
4244

43-
/// Software token TOTP multi-factor authentication (MFA) is not enabled for the user pool.
45+
/// Software token (TOTP) multi-factor authentication (MFA) is not enabled for the user pool.
4446
case softwareTokenMFANotEnabled
4547

4648
/// Required to reset the password of the user.
@@ -55,7 +57,7 @@ public enum AWSCognitoAuthError: Error {
5557
/// The user has made too many requests for a given operation.
5658
case requestLimitExceeded
5759

58-
/// Amazon Cognito service encounters an invalid AWS Lambda response or encounters an
60+
/// Amazon Cognito service encountered an invalid AWS Lambda response or encountered an
5961
/// unexpected exception with the AWS Lambda service.
6062
case lambda
6163

@@ -71,7 +73,7 @@ public enum AWSCognitoAuthError: Error {
7173
/// Requested resource is not available with the current account setup.
7274
case invalidAccountTypeException
7375

74-
/// Request was not completed because of any network related issue
76+
/// Request was not completed because of a network related issue
7577
case network
7678

7779
/// SMS role related issue
@@ -107,6 +109,83 @@ public enum AWSCognitoAuthError: Error {
107109
/// The relying party ID doesn't match
108110
case webAuthnRelyingPartyMismatch
109111

110-
/// The WebAuthm configuration is missing or incomplete
112+
/// The WebAuthn configuration is missing or incomplete
111113
case webAuthnConfigurationMissing
112114
}
115+
116+
extension AWSCognitoAuthError: LocalizedError {
117+
public var errorDescription: String? {
118+
var message: String = ""
119+
switch self {
120+
case .userNotFound:
121+
message = "User not found in the system."
122+
case .userNotConfirmed:
123+
message = "User not confirmed in the system."
124+
case .usernameExists:
125+
message = "Username already exists in the system."
126+
case .aliasExists:
127+
message = "Alias already exists in the system."
128+
case .codeDelivery:
129+
message = "Error in delivering the confirmation code."
130+
case .codeMismatch:
131+
message = "Confirmation code entered is not correct."
132+
case .codeExpired:
133+
message = "Confirmation code has expired."
134+
case .invalidParameter:
135+
message = "One or more parameters are incorrect."
136+
case .invalidPassword:
137+
message = "Password given is invalid."
138+
case .limitExceeded:
139+
message = "Limit exceeded for the requested AWS resource."
140+
case .mfaMethodNotFound:
141+
message = "Amazon Cognito cannot find a multi-factor authentication (MFA) method."
142+
case .softwareTokenMFANotEnabled:
143+
message = "Software token (TOTP) multi-factor authentication (MFA) is not enabled for the user pool."
144+
case .passwordResetRequired:
145+
message = "Required to reset the password of the user."
146+
case .resourceNotFound:
147+
message = "Amazon Cognito service cannot find the requested resource."
148+
case .failedAttemptsLimitExceeded:
149+
message = "The user has made too many failed attempts for a given action."
150+
case .requestLimitExceeded:
151+
message = "The user has made too many requests for a given operation."
152+
case .lambda:
153+
message = "Amazon Cognito service encountered an invalid AWS Lambda response or encountered an unexpected exception with the AWS Lambda service."
154+
case .deviceNotTracked:
155+
message = "Device is not tracked."
156+
case .errorLoadingUI:
157+
message = "Error in loading the web UI."
158+
case .userCancelled:
159+
message = "User cancelled the step."
160+
case .invalidAccountTypeException:
161+
message = "Requested resource is not available with the current account setup."
162+
case .network:
163+
message = "Request was not completed because of a network related issue."
164+
case .smsRole:
165+
message = "SMS role related issue."
166+
case .emailRole:
167+
message = "Email role related issue."
168+
case .externalServiceException:
169+
message = "An external service like facebook/twitter threw an error."
170+
case .limitExceededException:
171+
message = "Limit exceeded exception. Thrown when the total number of user pools has exceeded a preset limit."
172+
case .resourceConflictException:
173+
message = "Thrown when a user tries to use a login which is already linked to another account."
174+
case .webAuthnChallengeNotFound:
175+
message = "The WebAuthn credentials don't match an existing request."
176+
case .webAuthnClientMismatch:
177+
message = "The client doesn't support WebAuhn authentication."
178+
case .webAuthnNotSupported:
179+
message = "WebAuthn is not supported on this device."
180+
case .webAuthnNotEnabled:
181+
message = "WebAuthn is not enabled."
182+
case .webAuthnOriginNotAllowed:
183+
message = "The device origin is not registered as an allowed origin."
184+
case .webAuthnRelyingPartyMismatch:
185+
message = "The relying party ID doesn't match."
186+
case .webAuthnConfigurationMissing:
187+
message = "The WebAuthn configuration is missing or incomplete."
188+
}
189+
return "\(String(describing: Self.self)).\(self): \(message)"
190+
}
191+
}

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,17 @@ struct ConfigurationHelper {
4747

4848
// parse `authFlowType`
4949
var authFlowType: AuthFlowType
50+
51+
// If Migration path is enabled, auth flow type should always be set to USER_PASSWORD_AUTH
5052
if case .boolean(let isMigrationEnabled) = cognitoUserPoolJSON.value(at: "MigrationEnabled"),
5153
isMigrationEnabled == true {
5254
authFlowType = .userPassword
5355
} else if let authJson = config.value(at: "Auth.Default"),
54-
case .string(let authFlowTypeJSON) = authJson.value(at: "authenticationFlowType"),
55-
authFlowTypeJSON == "CUSTOM_AUTH" {
56-
authFlowType = .customWithSRP
56+
case .string(let authFlowTypeConfigValue) = authJson.value(at: "authenticationFlowType"),
57+
let authFlowTypeFromConfig = AuthFlowType(rawValue: authFlowTypeConfigValue) {
58+
authFlowType = authFlowTypeFromConfig
5759
} else {
60+
// if the auth flow type is not found from config, default to SRP
5861
authFlowType = .userSRP
5962
}
6063

AmplifyPlugins/Auth/Tests/AuthHostApp/AuthIntegrationTests/PasswordlessTests/PasswordlessSignInTests.swift

+89
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,95 @@ class PasswordlessSignInTests: AWSAuthBaseTest {
167167
}
168168
}
169169

170+
/// Test successful signIn of a valid user
171+
///
172+
/// - Given: A user registered in Cognito user pool
173+
/// - When:
174+
/// - I invoke Amplify.Auth.signIn with the username and password, using userAuth flow
175+
/// - Retry confirm sign in after a wrong password attempt is not supposed to work in `userAuth` flow. Cognito doesn't support this flow.
176+
/// - Re-initiation of sign in should work correctly after a incorrect attempt
177+
/// - Then:
178+
/// - I should get a completed signIn flow.
179+
///
180+
func testSignInWithPasswordSRP_givenValidUser_expectErrorOnWrongPassword() async throws {
181+
182+
let username = "integTest\(UUID().uuidString)"
183+
let password = "Pp123@\(UUID().uuidString)"
184+
185+
try await signUp(username: username, password: password)
186+
187+
do {
188+
let pluginOptions = AWSAuthSignInOptions(
189+
authFlowType: .userAuth)
190+
var signInResult = try await Amplify.Auth.signIn(
191+
username: username,
192+
password: password,
193+
options: .init(pluginOptions: pluginOptions))
194+
guard case .continueSignInWithFirstFactorSelection(let availableFactors) = signInResult.nextStep else {
195+
XCTFail("SignIn should return a .continueSignInWithFirstFactorSelection")
196+
return
197+
}
198+
XCTAssert(availableFactors.contains(.passwordSRP))
199+
var confirmSignInResult = try await Amplify.Auth.confirmSignIn(
200+
challengeResponse: AuthFactorType.passwordSRP.challengeResponse)
201+
202+
guard case .confirmSignInWithPassword = confirmSignInResult.nextStep else {
203+
XCTFail("ConfirmSignIn should return a .confirmSignInWithPassword")
204+
return
205+
}
206+
207+
// Try confirming with wrong password and it should fail
208+
209+
do {
210+
confirmSignInResult = try await Amplify.Auth.confirmSignIn(
211+
challengeResponse: "wrong-password")
212+
} catch {
213+
guard let error = error as? AuthError else {
214+
XCTFail("Error should be of type AuthError instead got: \(error)")
215+
return
216+
}
217+
guard case .notAuthorized = error else {
218+
XCTFail("Error should be .notAuthorized instead got: \(error)")
219+
return
220+
}
221+
}
222+
223+
// Try confirming with password again and it should fail saying that re-initiation is needed
224+
225+
do {
226+
confirmSignInResult = try await Amplify.Auth.confirmSignIn(
227+
challengeResponse: password)
228+
} catch {
229+
guard let error = error as? AuthError else {
230+
XCTFail("Error should be of type AuthError instead got: \(error)")
231+
return
232+
}
233+
guard case .invalidState = error else {
234+
XCTFail("Error should be .invalidState instead got: \(error)")
235+
return
236+
}
237+
}
238+
239+
// After all the errors re-initiation of sign in should work
240+
241+
// Sign in
242+
_ = try await Amplify.Auth.signIn(
243+
username: username,
244+
password: password,
245+
options: .init(pluginOptions: pluginOptions))
246+
// Select passwordSRP
247+
_ = try await Amplify.Auth.confirmSignIn(
248+
challengeResponse: AuthFactorType.passwordSRP.challengeResponse)
249+
// Complete sign in
250+
confirmSignInResult = try await Amplify.Auth.confirmSignIn(
251+
challengeResponse: password)
252+
253+
XCTAssertTrue(confirmSignInResult.isSignedIn, "SignIn should be complete")
254+
} catch {
255+
XCTFail("SignIn with a valid username/password should not fail \(error)")
256+
}
257+
}
258+
170259
/// Test successful signIn of a valid user
171260
///
172261
/// - Given: A user registered in Cognito user pool

AmplifyPlugins/Notifications/Push/Tests/PushNotificationHostApp/LocalServer/package-lock.json

+11-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)