-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): add URLSession client (#3387)
* feat(auth): add URLSession client * Add mocks * Fix the unit test build * Address review comments * Address review comments * Address review comments
- Loading branch information
1 parent
214d0a9
commit f9282f8
Showing
12 changed files
with
178 additions
and
2 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
79 changes: 79 additions & 0 deletions
79
...ns/Auth/Sources/AWSCognitoAuthPlugin/Support/Passwordless/AWSAuthPasswordlessClient.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,79 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
import Amplify | ||
import AWSPluginsCore | ||
|
||
/// Concrete implementation of AuthPasswordlessBehavior | ||
class AWSAuthPasswordlessClient : AuthPasswordlessBehavior { | ||
|
||
let urlSession: URLSession | ||
let userAgent: String | ||
|
||
init(urlSession: URLSession) { | ||
self.urlSession = urlSession | ||
self.userAgent = "\(AmplifyAWSServiceConfiguration.userAgentLib) \(AmplifyAWSServiceConfiguration.userAgentOS)" | ||
} | ||
|
||
func preInitiateAuthSignUp(endpoint: URL, | ||
payload: PreInitiateAuthSignUpPayload) async throws -> Result<Void, AuthError> { | ||
|
||
var request = URLRequest(url: endpoint) | ||
request.httpMethod = "POST" | ||
request.setValue(userAgent, forHTTPHeaderField: "User-Agent") | ||
|
||
do { | ||
let (_, response) = try await self.data(for: request) | ||
|
||
guard let response = response as? HTTPURLResponse else { | ||
throw AuthError.unknown("Response received is not a HTTPURLResponse") | ||
} | ||
|
||
if response.statusCode == 200 { | ||
return .successfulVoid | ||
} else { | ||
throw AuthError.unknown("Response received with status code: \(response.statusCode)") | ||
} | ||
} catch { | ||
throw AuthError.service(error.localizedDescription, | ||
"API Gateway returned an error. Please check the error message for more details.", | ||
error) | ||
} | ||
} | ||
|
||
private func data( | ||
for request: URLRequest, | ||
delegate: (URLSessionTaskDelegate)? = nil) | ||
async throws -> (Data, URLResponse) { | ||
if #available(iOS 15.0, macOS 12.0, tvOS 15.0, *) { | ||
return try await self.urlSession.data( | ||
for: request, | ||
delegate: delegate | ||
) | ||
} else { | ||
// Fallback on earlier versions | ||
return try await withCheckedThrowingContinuation({ continuation in | ||
let dataTask = urlSession.dataTask(with: request) { data, response, error in | ||
if let data = data, let response = response { | ||
continuation.resume(returning: (data, response)) | ||
} else { | ||
continuation.resume(throwing: error ?? AuthError.unknown( | ||
""" | ||
An unknown error occurred with | ||
data: \(String(describing: data)) | ||
response: \(String(describing: response)) | ||
error: \(String(describing: error)) | ||
""") | ||
) | ||
} | ||
} | ||
dataTask.resume() | ||
}) | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...ins/Auth/Sources/AWSCognitoAuthPlugin/Support/Passwordless/AuthPasswordlessBehavior.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,19 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
import Amplify | ||
|
||
// Protocol for initiating sign up in a passwordless flow | ||
protocol AuthPasswordlessBehavior { | ||
|
||
func preInitiateAuthSignUp( | ||
endpoint: URL, | ||
payload: PreInitiateAuthSignUpPayload) | ||
async throws -> Result<Void, AuthError> | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...Auth/Sources/AWSCognitoAuthPlugin/Support/Passwordless/PreInitiateAuthSignUpPayload.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,32 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
|
||
// Payload data structure to send to the API Gateway endpoint | ||
// for password sign up and sign in flow to initiate creating a new user | ||
struct PreInitiateAuthSignUpPayload: Codable { | ||
let username: String | ||
let deliveryMedium: String | ||
let userAttributes: [String:String]? | ||
|
||
let userPoolId: String | ||
let region: String | ||
|
||
init( | ||
username: String, | ||
deliveryMedium: String, | ||
userAttributes: [String : String]?, | ||
userPoolId: String, region: String | ||
) { | ||
self.username = username | ||
self.deliveryMedium = deliveryMedium | ||
self.userAttributes = userAttributes | ||
self.userPoolId = userPoolId | ||
self.region = region | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...Plugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/Mocks/MockAuthPasswordlessBehavior.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,24 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
import Amplify | ||
@testable import AWSPluginsCore | ||
@testable import AWSCognitoAuthPlugin | ||
|
||
class MockAuthPasswordlessBehavior: AuthPasswordlessBehavior { | ||
|
||
public var preInitiateAuthSignUpCallCount = 0 | ||
|
||
func preInitiateAuthSignUp( | ||
endpoint: URL, | ||
payload: PreInitiateAuthSignUpPayload) | ||
async throws -> Result<Void, AuthError> { | ||
preInitiateAuthSignUpCallCount += 1 | ||
return .successfulVoid | ||
} | ||
} |
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
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