amplify-swift/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Task/UserTasks/AWSAuthConfirmUserAttribute...

58 lines
1.9 KiB
Swift

//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
import Foundation
import Amplify
import AWSPluginsCore
import ClientRuntime
import AWSCognitoIdentityProvider
class AWSAuthConfirmUserAttributeTask: AuthConfirmUserAttributeTask {
typealias CognitoUserPoolFactory = () throws -> CognitoUserPoolBehavior
private let request: AuthConfirmUserAttributeRequest
private let authStateMachine: AuthStateMachine
private let userPoolFactory: CognitoUserPoolFactory
private let taskHelper: AWSAuthTaskHelper
var eventName: HubPayloadEventName {
HubPayload.EventName.Auth.confirmUserAttributesAPI
}
init(_ request: AuthConfirmUserAttributeRequest, authStateMachine: AuthStateMachine, userPoolFactory: @escaping CognitoUserPoolFactory) {
self.request = request
self.authStateMachine = authStateMachine
self.userPoolFactory = userPoolFactory
self.taskHelper = AWSAuthTaskHelper(authStateMachine: authStateMachine)
}
func execute() async throws {
do {
await taskHelper.didStateMachineConfigured()
let accessToken = try await taskHelper.getAccessToken()
try await confirmUserAttribute(with: accessToken)
} catch let error as VerifyUserAttributeOutputError {
throw error.authError
} catch let error as AuthError {
throw error
} catch let error {
throw AuthError.unknown("Unable to execute auth task", error)
}
}
func confirmUserAttribute(with accessToken: String) async throws {
let userPoolService = try userPoolFactory()
let input = VerifyUserAttributeInput(
accessToken: accessToken,
attributeName: request.attributeKey.rawValue,
code: request.confirmationCode)
_ = try await userPoolService.verifyUserAttribute(input: input)
}
}