-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Auth): Adding TOTP support in Amplify Auth category (#3040)
* feat(Auth): Adding TOTP support in Amplify Auth category * worked on review comments * adding more detailed comments for the new API's
- Loading branch information
Showing
7 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
public enum MFAType: String { | ||
|
||
/// Short Messaging Service linked with a phone number | ||
case sms | ||
|
||
/// Time-based One Time Password linked with an authenticator app | ||
case totp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
|
||
public struct TOTPSetupDetails { | ||
|
||
/// Secret code returned by the service to help setting up TOTP | ||
public let sharedSecret: String | ||
|
||
/// username that will be used to construct the URI | ||
public let username: String | ||
|
||
public init(sharedSecret: String, username: String) { | ||
self.sharedSecret = sharedSecret | ||
self.username = username | ||
} | ||
/// Returns a TOTP setup URI that can help the customers avoid barcode scanning and use native password manager to handle TOTP association | ||
/// Example: On iOS and MacOS, URI will redirect to associated Password Manager for the platform | ||
/// | ||
/// throws AuthError.validation if a `URL` cannot be formed with the supplied parameters | ||
/// (for example, if the parameter string contains characters that are illegal in a URL, or is an empty string). | ||
public func getSetupURI( | ||
appName: String, | ||
accountName: String? = nil) throws -> URL { | ||
guard let URL = URL( | ||
string: "otpauth://totp/\(appName):\(accountName ?? username)?secret=\(sharedSecret)&issuer=\(appName)") else { | ||
|
||
throw AuthError.validation( | ||
"appName or accountName", | ||
"Invalid Parameters. Cannot form URL from the supplied appName or accountName", | ||
"Please make sure that the supplied parameters don't contain any characters that are illegal in a URL or is an empty String", | ||
nil) | ||
} | ||
return URL | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
|
||
/// Request to set up TOTP | ||
public struct SetUpTOTPRequest: AmplifyOperationRequest { | ||
|
||
/// Extra request options defined in `SetUpTOTPRequest.Options` | ||
public var options: Options | ||
|
||
public init(options: Options) { | ||
self.options = options | ||
} | ||
} | ||
|
||
public extension SetUpTOTPRequest { | ||
|
||
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 | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
Amplify/Categories/Auth/Request/VerifyTOTPSetupRequest.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
|
||
/// Request to verify TOTP setup | ||
public struct VerifyTOTPSetupRequest: AmplifyOperationRequest { | ||
|
||
/// Code from the associated Authenticator app that will be used for verification | ||
public var code: String | ||
|
||
/// Extra request options defined in `VerifyTOTPSetupRequest.Options` | ||
public var options: Options | ||
|
||
public init( | ||
code: String, | ||
options: Options) { | ||
self.code = code | ||
self.options = options | ||
} | ||
} | ||
|
||
public extension VerifyTOTPSetupRequest { | ||
|
||
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 | ||
} | ||
} | ||
} |