Skip to content

Commit 15c514e

Browse files
harsh62lawmicha
andauthored
feat(Auth): Rename resendConfirmationCode API for User Attributes to sendVerificationCode (#3384)
* feat(Auth): Rename resendConfirmationCode API for User Attributes to sendVerificationCode * fix to use correct options * worked on review comments * Update Amplify/Categories/Auth/AuthCategoryUserBehavior.swift Co-authored-by: Michael Law <[email protected]> * updated the deprecation message --------- Co-authored-by: Michael Law <[email protected]>
1 parent def0423 commit 15c514e

File tree

12 files changed

+278
-81
lines changed

12 files changed

+278
-81
lines changed

Amplify/Categories/Auth/AuthCategory+UserBehavior.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,19 @@ extension AuthCategory: AuthCategoryUserBehavior {
2727
try await plugin.update(userAttributes: userAttributes, options: options)
2828
}
2929

30-
public func resendConfirmationCode(forUserAttributeKey userAttributeKey: AuthUserAttributeKey,
31-
options: AuthAttributeResendConfirmationCodeRequest.Options? = nil) async throws -> AuthCodeDeliveryDetails {
30+
@available(*, deprecated, renamed: "sendVerificationCode(forUserAttributeKey:options:)")
31+
public func resendConfirmationCode(
32+
forUserAttributeKey userAttributeKey: AuthUserAttributeKey,
33+
options: AuthAttributeResendConfirmationCodeRequest.Options? = nil
34+
) async throws -> AuthCodeDeliveryDetails {
3235
try await plugin.resendConfirmationCode(forUserAttributeKey: userAttributeKey, options: options)
36+
}
3337

38+
public func sendVerificationCode(
39+
forUserAttributeKey userAttributeKey: AuthUserAttributeKey,
40+
options: AuthSendUserAttributeVerificationCodeRequest.Options? = nil
41+
) async throws -> AuthCodeDeliveryDetails {
42+
try await plugin.sendVerificationCode(forUserAttributeKey: userAttributeKey, options: options)
3443
}
3544

3645
public func confirm(userAttribute: AuthUserAttributeKey,

Amplify/Categories/Auth/AuthCategoryUserBehavior.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,21 @@ public protocol AuthCategoryUserBehavior: AnyObject {
3939
/// - Parameters:
4040
/// - userAttributeKey: Attribute to be verified
4141
/// - options: Parameters specific to plugin behavior
42-
func resendConfirmationCode(forUserAttributeKey userAttributeKey: AuthUserAttributeKey,
43-
options: AuthAttributeResendConfirmationCodeRequest.Options?) async throws -> AuthCodeDeliveryDetails
42+
@available(*, deprecated, renamed: "sendVerificationCode(forUserAttributeKey:options:)")
43+
func resendConfirmationCode(
44+
forUserAttributeKey userAttributeKey: AuthUserAttributeKey,
45+
options: AuthAttributeResendConfirmationCodeRequest.Options?
46+
) async throws -> AuthCodeDeliveryDetails
47+
48+
/// Sends the verification code required to verify an attribute
49+
///
50+
/// - Parameters:
51+
/// - userAttributeKey: Attribute to be verified
52+
/// - options: Parameters specific to plugin behavior
53+
func sendVerificationCode(
54+
forUserAttributeKey userAttributeKey: AuthUserAttributeKey,
55+
options: AuthSendUserAttributeVerificationCodeRequest.Options?
56+
) async throws -> AuthCodeDeliveryDetails
4457

4558
/// Confirm an attribute using confirmation code
4659
///
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import Foundation
9+
10+
// swiftlint:disable type_name
11+
12+
/// Request for sending verification code that was generated for update attribute
13+
public struct AuthSendUserAttributeVerificationCodeRequest: AmplifyOperationRequest {
14+
15+
/// Attribute key for which the confirmation code was sent
16+
public let attributeKey: AuthUserAttributeKey
17+
18+
/// Extra request options defined in `AuthSendUserAttributeVerificationCodeRequest.Options`
19+
public var options: Options
20+
21+
public init(attributeKey: AuthUserAttributeKey,
22+
options: Options) {
23+
self.attributeKey = attributeKey
24+
self.options = options
25+
}
26+
}
27+
28+
public extension AuthSendUserAttributeVerificationCodeRequest {
29+
30+
struct Options {
31+
32+
/// Extra plugin specific options, only used in special circumstances when the existing options do not provide
33+
/// a way to utilize the underlying auth plugin functionality. See plugin documentation for expected
34+
/// key/values
35+
public let pluginOptions: Any?
36+
37+
public init(pluginOptions: Any? = nil) {
38+
self.pluginOptions = pluginOptions
39+
}
40+
}
41+
}

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/ClientBehavior/AWSCognitoAuthPlugin+UserBehavior.swift

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ public extension AWSCognitoAuthPlugin {
4141
} as! [AuthUserAttributeKey: AuthUpdateAttributeResult]
4242
}
4343

44-
func resendConfirmationCode(forUserAttributeKey userAttributeKey: AuthUserAttributeKey,
45-
options: AuthAttributeResendConfirmationCodeRequest.Options? = nil) async throws -> AuthCodeDeliveryDetails {
44+
@available(*, deprecated, renamed: "sendVerificationCode(forUserAttributeKey:options:)")
45+
func resendConfirmationCode(
46+
forUserAttributeKey userAttributeKey: AuthUserAttributeKey,
47+
options: AuthAttributeResendConfirmationCodeRequest.Options? = nil
48+
) async throws -> AuthCodeDeliveryDetails {
4649

4750
let options = options ?? AuthAttributeResendConfirmationCodeRequest.Options()
4851
let request = AuthAttributeResendConfirmationCodeRequest(
@@ -53,6 +56,22 @@ public extension AWSCognitoAuthPlugin {
5356
} as! AuthCodeDeliveryDetails
5457
}
5558

59+
func sendVerificationCode(
60+
forUserAttributeKey userAttributeKey:
61+
AuthUserAttributeKey,
62+
options: AuthSendUserAttributeVerificationCodeRequest.Options? = nil
63+
) async throws -> AuthCodeDeliveryDetails {
64+
let options = options ?? AuthSendUserAttributeVerificationCodeRequest.Options()
65+
let request = AuthSendUserAttributeVerificationCodeRequest(
66+
attributeKey: userAttributeKey, options: options)
67+
let task = AWSAuthSendUserAttributeVerificationCodeTask(
68+
request, authStateMachine: authStateMachine,
69+
userPoolFactory: authEnvironment.cognitoUserPoolFactory)
70+
return try await taskQueue.sync {
71+
return try await task.value
72+
} as! AuthCodeDeliveryDetails
73+
}
74+
5675
func confirm(userAttribute: AuthUserAttributeKey, confirmationCode: String, options: AuthConfirmUserAttributeRequest.Options? = nil) async throws {
5776
let options = options ?? AuthConfirmUserAttributeRequest.Options()
5877
let request = AuthConfirmUserAttributeRequest(

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Models/Options/AWSAttributeResendConfirmationCodeOptions.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import Foundation
99

10+
@available(*, deprecated, renamed: "AWSSendUserAttributeVerificationCodeOptions")
1011
public struct AWSAttributeResendConfirmationCodeOptions {
1112

1213
/// A map of custom key-value pairs that you can provide as input for any custom workflows that this action triggers.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import Foundation
9+
10+
public struct AWSSendUserAttributeVerificationCodeOptions {
11+
12+
/// A map of custom key-value pairs that you can provide as input for any custom workflows that this action triggers.
13+
///
14+
/// When you use the ResendConfirmationCode API action, Amazon Cognito invokes the function that is assigned to the custom message trigger.
15+
/// When Amazon Cognito invokes this function, it passes a JSON payload, which the function receives as input.
16+
/// This payload contains a clientMetadata attribute, which provides the data that you assigned to the ClientMetadata parameter in your GetUserAttributeVerificationCode request.
17+
/// In your function code in AWS Lambda, you can process the clientMetadata value to enhance your workflow for your specific needs.
18+
///
19+
/// For more information, see Customizing user pool Workflows with Lambda Triggers in the Amazon Cognito Developer Guide.
20+
public let metadata: [String: String]?
21+
22+
public init(metadata: [String: String]? = nil) {
23+
self.metadata = metadata
24+
}
25+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import Amplify
9+
10+
protocol AuthSendUserAttributeVerificationCodeTask: AmplifyAuthTask where Request == AuthSendUserAttributeVerificationCodeRequest, Success == AuthCodeDeliveryDetails, Failure == AuthError {}
11+
12+
public extension HubPayload.EventName.Auth {
13+
14+
/// eventName for HubPayloads emitted by this operation
15+
static let sendUserAttributeVerificationCodeAPI = "Auth.sendUserAttributeVerificationCodeAPI"
16+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import Foundation
9+
import Amplify
10+
import AWSPluginsCore
11+
import AWSCognitoIdentityProvider
12+
13+
class AWSAuthSendUserAttributeVerificationCodeTask: AuthSendUserAttributeVerificationCodeTask {
14+
typealias CognitoUserPoolFactory = () throws -> CognitoUserPoolBehavior
15+
16+
private let request: AuthSendUserAttributeVerificationCodeRequest
17+
private let authStateMachine: AuthStateMachine
18+
private let userPoolFactory: CognitoUserPoolFactory
19+
private let taskHelper: AWSAuthTaskHelper
20+
21+
var eventName: HubPayloadEventName {
22+
HubPayload.EventName.Auth.sendUserAttributeVerificationCodeAPI
23+
}
24+
25+
init(_ request: AuthSendUserAttributeVerificationCodeRequest,
26+
authStateMachine: AuthStateMachine,
27+
userPoolFactory: @escaping CognitoUserPoolFactory) {
28+
self.request = request
29+
self.authStateMachine = authStateMachine
30+
self.userPoolFactory = userPoolFactory
31+
self.taskHelper = AWSAuthTaskHelper(authStateMachine: authStateMachine)
32+
}
33+
34+
func execute() async throws -> AuthCodeDeliveryDetails {
35+
do {
36+
await taskHelper.didStateMachineConfigured()
37+
let accessToken = try await taskHelper.getAccessToken()
38+
let devices = try await initiateGettingVerificationCode(with: accessToken)
39+
return devices
40+
} catch let error as AuthErrorConvertible {
41+
throw error.authError
42+
} catch {
43+
throw AuthError.configuration(
44+
"Unable to execute auth task",
45+
AuthPluginErrorConstants.configurationError,
46+
error
47+
)
48+
}
49+
}
50+
51+
func initiateGettingVerificationCode(with accessToken: String) async throws -> AuthCodeDeliveryDetails {
52+
let userPoolService = try userPoolFactory()
53+
let clientMetaData = (request.options.pluginOptions as? AWSSendUserAttributeVerificationCodeOptions)?.metadata ?? [:]
54+
55+
let input = GetUserAttributeVerificationCodeInput(
56+
accessToken: accessToken,
57+
attributeName: request.attributeKey.rawValue,
58+
clientMetadata: clientMetaData)
59+
60+
let result = try await userPoolService.getUserAttributeVerificationCode(input: input)
61+
guard let deliveryDetails = result.codeDeliveryDetails?.toAuthCodeDeliveryDetails() else {
62+
let authError = AuthError.unknown("Unable to get Auth code delivery details", nil)
63+
throw authError
64+
}
65+
return deliveryDetails
66+
}
67+
}

AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/TaskTests/UserBehaviourTests/AWSCognitoAuthUserBehaviorTests.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -136,36 +136,36 @@ class AWSCognitoAuthUserBehaviorTests: BasePluginTest {
136136
_ = try await plugin.update(userAttributes: [emailAttribute, phoneAttribute])
137137
}
138138

139-
/// Test resendConfirmationCode(for:) operation can be invoked
139+
/// Test sendVerificationCode(for:) operation can be invoked
140140
///
141141
/// - Given: Given a configured auth plugin
142142
/// - When:
143-
/// - I call resendConfirmationCode(for:) operation
143+
/// - I call sendVerificationCode(for:) operation
144144
/// - Then:
145145
/// - I should get a valid task completion
146146
///
147-
func testResendConfirmationCodeAttributeRequest() async throws {
147+
func testSendVerificationCodeAttributeRequest() async throws {
148148
mockIdentityProvider = MockIdentityProvider(mockGetUserAttributeVerificationCodeOutput: { _ in
149149
GetUserAttributeVerificationCodeOutput(
150150
codeDeliveryDetails: .init(
151151
attributeName: "attributeName",
152152
deliveryMedium: .email,
153153
destination: "destination"))
154154
})
155-
let pluginOptions = AWSAttributeResendConfirmationCodeOptions(metadata: ["key": "value"])
156-
let options = AuthAttributeResendConfirmationCodeRequest.Options(pluginOptions: pluginOptions)
157-
_ = try await plugin.resendConfirmationCode(forUserAttributeKey: .email, options: options)
155+
let pluginOptions = AWSSendUserAttributeVerificationCodeOptions(metadata: ["key": "value"])
156+
let options = AuthSendUserAttributeVerificationCodeRequest.Options(pluginOptions: pluginOptions)
157+
_ = try await plugin.sendVerificationCode(forUserAttributeKey: .email, options: options)
158158
}
159159

160-
/// Test resendConfirmationCode(for:) operation can be invoked with plugin options
160+
/// Test sendVerificationCode(for:) operation can be invoked with plugin options
161161
///
162162
/// - Given: Given a configured auth plugin
163163
/// - When:
164-
/// - I call resendConfirmationCode(for:) operation
164+
/// - I call sendVerificationCode(for:) operation
165165
/// - Then:
166166
/// - I should get a valid task completion
167167
///
168-
func testResendConfirmationCodeWithPluginOptions() async throws {
168+
func testSendVerificationCodeWithPluginOptions() async throws {
169169
mockIdentityProvider = MockIdentityProvider(mockGetUserAttributeVerificationCodeOutput: { request in
170170

171171
XCTAssertNotNil(request.clientMetadata)
@@ -176,28 +176,28 @@ class AWSCognitoAuthUserBehaviorTests: BasePluginTest {
176176
deliveryMedium: .email,
177177
destination: "destination"))
178178
})
179-
let pluginOptions = AWSAttributeResendConfirmationCodeOptions(metadata: ["key": "value"])
180-
let options = AuthAttributeResendConfirmationCodeRequest.Options(pluginOptions: pluginOptions)
181-
_ = try await plugin.resendConfirmationCode(forUserAttributeKey: .email, options: options)
179+
let pluginOptions = AWSSendUserAttributeVerificationCodeOptions(metadata: ["key": "value"])
180+
let options = AuthSendUserAttributeVerificationCodeRequest.Options(pluginOptions: pluginOptions)
181+
_ = try await plugin.sendVerificationCode(forUserAttributeKey: .email, options: options)
182182
}
183183

184-
/// Test resendConfirmationCode(for:) operation can be invoked without options
184+
/// Test sendVerificationCode(for:) operation can be invoked without options
185185
///
186186
/// - Given: Given a configured auth plugin
187187
/// - When:
188-
/// - I call resendConfirmationCode(for:) operation
188+
/// - I call sendVerificationCode(for:) operation
189189
/// - Then:
190190
/// - I should get a valid task completion
191191
///
192-
func testResendConfirmationCodeAttributeRequestWithoutOptions() async throws {
192+
func testSendVerificationCodeAttributeRequestWithoutOptions() async throws {
193193
mockIdentityProvider = MockIdentityProvider(mockGetUserAttributeVerificationCodeOutput: { _ in
194194
GetUserAttributeVerificationCodeOutput(
195195
codeDeliveryDetails: .init(
196196
attributeName: "attributeName",
197197
deliveryMedium: .email,
198198
destination: "destination"))
199199
})
200-
_ = try await plugin.resendConfirmationCode(forUserAttributeKey: .email)
200+
_ = try await plugin.sendVerificationCode(forUserAttributeKey: .email)
201201
}
202202

203203
/// Test confirm(userAttribute: ) operation can be invoked

0 commit comments

Comments
 (0)