-
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.
- Loading branch information
1 parent
247f390
commit 316d580
Showing
15 changed files
with
138 additions
and
161 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
.../Sources/AWSCognitoAuthPlugin/Support/Passwordless/AWSPreInitiateAuthSignUpBehavior.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 PreInitiateAuthSignUpBehavior | ||
class AWSPreInitiateAuthSignUpBehavior : PreInitiateAuthSignUpBehavior { | ||
|
||
let urlSession: URLSession | ||
let userAgent: String | ||
|
||
init(urlSession: URLSession) { | ||
self.urlSession = urlSession | ||
self.userAgent = "\(AmplifyAWSServiceConfiguration.userAgentLib) \(AmplifyAWSServiceConfiguration.userAgentOS)" | ||
} | ||
|
||
func preInitiateAuthSignUp(preInitiateAuthSignUpEndpoint: URL, | ||
preInitiateAuthSignUpPayload: PreInitiateAuthSignUpPayload) async throws -> Result<Void, AuthError> { | ||
|
||
var request = URLRequest(url: preInitiateAuthSignUpEndpoint) | ||
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
...uth/Sources/AWSCognitoAuthPlugin/Support/Passwordless/PreInitiateAuthSignUpBehavior.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 PreInitiateAuthSignUpBehavior { | ||
|
||
func preInitiateAuthSignUp( | ||
preInitiateAuthSignUpEndpoint: URL, | ||
preInitiateAuthSignUpPayload: PreInitiateAuthSignUpPayload) | ||
async throws -> Result<Void, AuthError> | ||
|
||
} |
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
87 changes: 0 additions & 87 deletions
87
...Plugins/Auth/Sources/AWSCognitoAuthPlugin/Support/URLSessionClient/URLSessionClient.swift
This file was deleted.
Oops, something went wrong.
27 changes: 0 additions & 27 deletions
27
...Auth/Sources/AWSCognitoAuthPlugin/Support/URLSessionClient/URLSessionClientBehavior.swift
This file was deleted.
Oops, something went wrong.
24 changes: 24 additions & 0 deletions
24
...ns/Auth/Tests/AWSCognitoAuthPluginUnitTests/Mocks/MockPreInitiateAuthSignUpBehavior.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 MockPreInitiateAuthSignUpBehavior: PreInitiateAuthSignUpBehavior { | ||
|
||
public var preInitiateAuthSignUpCallCount = 0 | ||
|
||
func preInitiateAuthSignUp( | ||
preInitiateAuthSignUpEndpoint: URL, | ||
preInitiateAuthSignUpPayload: PreInitiateAuthSignUpPayload) | ||
async throws -> Result<Void, AuthError> { | ||
preInitiateAuthSignUpCallCount += 1 | ||
return .successfulVoid | ||
} | ||
} |
31 changes: 0 additions & 31 deletions
31
AmplifyPlugins/Auth/Tests/AWSCognitoAuthPluginUnitTests/Mocks/MockURLSessionClient.swift
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.