From a7281b8c9444a9bfcfcb993709650ee4379b4287 Mon Sep 17 00:00:00 2001 From: Abhash Kumar Singh Date: Mon, 27 Nov 2023 13:55:04 -0800 Subject: [PATCH] feat(auth): add passwordless request options for magiclink --- .../Auth/AuthCategory+ClientBehavior.swift | 4 +- .../Auth/AuthCategoryBehavior.swift | 4 +- ...thConfirmSignInWithMagicLinkRequest.swift} | 15 ++++-- .../AuthConfirmSignInWithOTPRequest.swift | 2 +- .../AuthSignInWithMagicLinkRequest.swift | 46 +++++++++++++++++++ .../AWSCognitoAuthPlugin+ClientBehavior.swift | 4 +- .../Mocks/MockAuthCategoryPlugin.swift | 4 +- 7 files changed, 65 insertions(+), 14 deletions(-) rename Amplify/Categories/Auth/Request/{AuthPasswordlessMagicLinkRequest.swift => AuthConfirmSignInWithMagicLinkRequest.swift} (54%) create mode 100644 Amplify/Categories/Auth/Request/AuthSignInWithMagicLinkRequest.swift diff --git a/Amplify/Categories/Auth/AuthCategory+ClientBehavior.swift b/Amplify/Categories/Auth/AuthCategory+ClientBehavior.swift index 6a1dcb3448..7d1a2d09d3 100644 --- a/Amplify/Categories/Auth/AuthCategory+ClientBehavior.swift +++ b/Amplify/Categories/Auth/AuthCategory+ClientBehavior.swift @@ -92,7 +92,7 @@ extension AuthCategory: AuthCategoryBehavior { username: String, flow: AuthPasswordlessFlow, redirectURL: String, - options: AuthPasswordlessMagicLinkRequest.Options? = nil + options: AuthSignInWithMagicLinkRequest.Options? = nil ) async throws -> AuthSignInResult { try await plugin.signInWithMagicLink( username: username, @@ -103,7 +103,7 @@ extension AuthCategory: AuthCategoryBehavior { public func confirmSignInWithMagicLink( challengeResponse: String, - options: AuthPasswordlessMagicLinkRequest.Options? = nil + options: AuthConfirmSignInWithMagicLinkRequest.Options? = nil ) async throws -> AuthSignInResult { try await plugin.confirmSignInWithMagicLink( challengeResponse: challengeResponse, diff --git a/Amplify/Categories/Auth/AuthCategoryBehavior.swift b/Amplify/Categories/Auth/AuthCategoryBehavior.swift index fc1463617f..b175774bf6 100644 --- a/Amplify/Categories/Auth/AuthCategoryBehavior.swift +++ b/Amplify/Categories/Auth/AuthCategoryBehavior.swift @@ -160,7 +160,7 @@ public protocol AuthCategoryBehavior: AuthCategoryUserBehavior, AuthCategoryDevi username: String, flow: AuthPasswordlessFlow, redirectURL: String, - options: AuthPasswordlessMagicLinkRequest.Options? + options: AuthSignInWithMagicLinkRequest.Options? ) async throws -> AuthSignInResult /// Confirms Sign in with Magiclink flow @@ -172,7 +172,7 @@ public protocol AuthCategoryBehavior: AuthCategoryUserBehavior, AuthCategoryDevi /// - options: Parameters specific to plugin behavior func confirmSignInWithMagicLink( challengeResponse: String, - options: AuthPasswordlessMagicLinkRequest.Options? + options: AuthConfirmSignInWithMagicLinkRequest.Options? ) async throws -> AuthSignInResult /// Initiates Sign in with OTP diff --git a/Amplify/Categories/Auth/Request/AuthPasswordlessMagicLinkRequest.swift b/Amplify/Categories/Auth/Request/AuthConfirmSignInWithMagicLinkRequest.swift similarity index 54% rename from Amplify/Categories/Auth/Request/AuthPasswordlessMagicLinkRequest.swift rename to Amplify/Categories/Auth/Request/AuthConfirmSignInWithMagicLinkRequest.swift index 1bcb4adf9f..b004bf3694 100644 --- a/Amplify/Categories/Auth/Request/AuthPasswordlessMagicLinkRequest.swift +++ b/Amplify/Categories/Auth/Request/AuthConfirmSignInWithMagicLinkRequest.swift @@ -7,18 +7,23 @@ import Foundation -/// Request to sign in a user with Passwordless Magic Link flow -public struct AuthPasswordlessMagicLinkRequest: AmplifyOperationRequest { +/// Request to confirm sign in a user with magic link flow +public struct AuthConfirmSignInWithMagicLinkRequest: AmplifyOperationRequest { - /// Extra request options defined in `AuthPasswordlessMagicLinkRequest.Options` + /// The value of `challengeResponse` is the code that is received + /// by the user in magic link + public let challengeResponse: String + + /// Extra request options defined in `AuthConfirmSignInWithMagicLinkRequest.Options` public var options: Options - public init(options: Options) { + public init(challengeResponse: String, options: Options) { + self.challengeResponse = challengeResponse self.options = options } } -public extension AuthPasswordlessMagicLinkRequest { +public extension AuthConfirmSignInWithMagicLinkRequest { struct Options { diff --git a/Amplify/Categories/Auth/Request/AuthConfirmSignInWithOTPRequest.swift b/Amplify/Categories/Auth/Request/AuthConfirmSignInWithOTPRequest.swift index c7b42bbfa1..3034a88848 100644 --- a/Amplify/Categories/Auth/Request/AuthConfirmSignInWithOTPRequest.swift +++ b/Amplify/Categories/Auth/Request/AuthConfirmSignInWithOTPRequest.swift @@ -13,7 +13,7 @@ public struct AuthConfirmSignInWithOTPRequest: AmplifyOperationRequest { /// The value of `challengeResponse`is the OTP that is received on the destination provided during sign in request public let challengeResponse: String - /// Extra request options defined in `AuthPasswordlessOTPRequest.Options` + /// Extra request options defined in `AuthConfirmSignInWithOTPRequest.Options` public var options: Options public init(challengeResponse: String, options: Options) { diff --git a/Amplify/Categories/Auth/Request/AuthSignInWithMagicLinkRequest.swift b/Amplify/Categories/Auth/Request/AuthSignInWithMagicLinkRequest.swift new file mode 100644 index 0000000000..39244f28fc --- /dev/null +++ b/Amplify/Categories/Auth/Request/AuthSignInWithMagicLinkRequest.swift @@ -0,0 +1,46 @@ +// +// Copyright Amazon.com Inc. or its affiliates. +// All Rights Reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// + +import Foundation + +/// Request to sign in a user with Passwordless Magic Link flow +public struct AuthSignInWithMagicLinkRequest: AmplifyOperationRequest { + + /// User name for which the magic link was requested + public let username: String + + /// The flow that the request should begin with. + public let flow: AuthPasswordlessFlow + + /// The redirect url that the magic link will be configured with + public let redirectURL: String + + /// Extra request options defined in `AuthSignInWithMagicLinkRequest.Options` + public var options: Options + + public init(username: String, flow: AuthPasswordlessFlow, redirectURL: String, options: Options) { + self.username = username + self.flow = flow + self.redirectURL = redirectURL + self.options = options + } +} + +public extension AuthSignInWithMagicLinkRequest { + + struct Options { + + /// Extra plugin specific options, only used in special circumstances when the existing options do not provide + /// a way to utilize the underlying auth plugin functionality. See plugin documentation for expected + /// key/values + public let pluginOptions: Any? + + public init(pluginOptions: Any? = nil) { + self.pluginOptions = pluginOptions + } + } +} diff --git a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/ClientBehavior/AWSCognitoAuthPlugin+ClientBehavior.swift b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/ClientBehavior/AWSCognitoAuthPlugin+ClientBehavior.swift index e669e8f7ad..a7b0e5648e 100644 --- a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/ClientBehavior/AWSCognitoAuthPlugin+ClientBehavior.swift +++ b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/ClientBehavior/AWSCognitoAuthPlugin+ClientBehavior.swift @@ -198,14 +198,14 @@ extension AWSCognitoAuthPlugin: AuthCategoryBehavior { username: String, flow: AuthPasswordlessFlow, redirectURL: String, - options: AuthPasswordlessMagicLinkRequest.Options? + options: AuthSignInWithMagicLinkRequest.Options? ) async throws -> AuthSignInResult { throw AuthError.unknown("Not Implemented") } public func confirmSignInWithMagicLink( challengeResponse: String, - options: AuthPasswordlessMagicLinkRequest.Options? + options: AuthConfirmSignInWithMagicLinkRequest.Options? ) async throws -> AuthSignInResult { throw AuthError.unknown("Not Implemented") } diff --git a/AmplifyTestCommon/Mocks/MockAuthCategoryPlugin.swift b/AmplifyTestCommon/Mocks/MockAuthCategoryPlugin.swift index 6093799470..8c0c07d3f1 100644 --- a/AmplifyTestCommon/Mocks/MockAuthCategoryPlugin.swift +++ b/AmplifyTestCommon/Mocks/MockAuthCategoryPlugin.swift @@ -114,14 +114,14 @@ class MockAuthCategoryPlugin: MessageReporter, AuthCategoryPlugin { username: String, flow: AuthPasswordlessFlow, redirectURL: String, - options: AuthPasswordlessMagicLinkRequest.Options? + options: AuthSignInWithMagicLinkRequest.Options? ) async throws -> AuthSignInResult { fatalError() } func confirmSignInWithMagicLink( challengeResponse: String, - options: AuthPasswordlessMagicLinkRequest.Options? + options: AuthConfirmSignInWithMagicLinkRequest.Options? ) async throws -> AuthSignInResult { fatalError() }