-
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
3ad7de2
commit 8bf2493
Showing
5 changed files
with
156 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
32 changes: 32 additions & 0 deletions
32
...s/Auth/Sources/AWSCognitoAuthPlugin/Support/URLSessionClient/PreInitiateAuthPayload.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 PreInitiateAuthPayload: 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 | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
...Plugins/Auth/Sources/AWSCognitoAuthPlugin/Support/URLSessionClient/URLSessionClient.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,87 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
import Amplify | ||
|
||
// A delegate client for `URLSessionClientBehavior` | ||
public struct URLSessionClient: URLSessionClientBehavior { | ||
|
||
let urlSession: URLSession | ||
|
||
public init(urlSession: URLSession) { | ||
self.urlSession = urlSession | ||
} | ||
|
||
public func cancelAndReset() async { | ||
self.urlSession.invalidateAndCancel() | ||
await self.urlSession.reset() | ||
} | ||
|
||
public func data( | ||
for request: URLRequest, | ||
delegate: (URLSessionTaskDelegate)?) | ||
async throws -> (Data, URLResponse) { | ||
if #available(iOS 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() | ||
}) | ||
} | ||
} | ||
|
||
public func data( | ||
from url: URL, | ||
delegate: (URLSessionTaskDelegate)?) | ||
async throws -> (Data, URLResponse) { | ||
if #available(iOS 15.0, *) { | ||
return try await self.urlSession.data( | ||
from: url, | ||
delegate: delegate | ||
) | ||
} else { | ||
// Fallback on earlier versions | ||
return try await withCheckedThrowingContinuation({ continuation in | ||
let dataTask = urlSession.dataTask(with: url) { 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() | ||
}) | ||
} | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
...Auth/Sources/AWSCognitoAuthPlugin/Support/URLSessionClient/URLSessionClientBehavior.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,27 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
|
||
// Protocol for replicating `URLSession` behavior | ||
// Note: Additional behavior can be added later if necessary | ||
public protocol URLSessionClientBehavior { | ||
|
||
/// For testing only. Resets the state of the object in preparation for testing. | ||
func cancelAndReset() async | ||
|
||
func data( | ||
for request: URLRequest, | ||
delegate: (URLSessionTaskDelegate)? | ||
) async throws -> (Data, URLResponse) | ||
|
||
func data( | ||
from url: URL, | ||
delegate: (URLSessionTaskDelegate)? | ||
) async throws -> (Data, URLResponse) | ||
|
||
} |