Skip to content

Commit

Permalink
feat(auth): add URLSession client
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisabhash committed Nov 28, 2023
1 parent 3ad7de2 commit 8bf2493
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ extension AWSCognitoAuthPlugin {
let credentialsClient = CredentialStoreOperationClient(
credentialStoreStateMachine: credentialStoreMachine)

let urlSessionClient = URLSessionClient(urlSession: makeURLSession())

let authResolver = AuthState.Resolver().eraseToAnyResolver()
let authEnvironment = makeAuthEnvironment(
authConfiguration: authConfiguration,
credentialsClient: credentialsClient
credentialsClient: credentialsClient,
urlSessionClient: urlSessionClient
)

let authStateMachine = StateMachine(resolver: authResolver, environment: authEnvironment)
Expand Down Expand Up @@ -154,7 +157,8 @@ extension AWSCognitoAuthPlugin {

private func makeAuthEnvironment(
authConfiguration: AuthConfiguration,
credentialsClient: CredentialStoreStateBehavior
credentialsClient: CredentialStoreStateBehavior,
urlSessionClient: URLSessionClientBehavior
) -> AuthEnvironment {

switch authConfiguration {
Expand All @@ -169,6 +173,7 @@ extension AWSCognitoAuthPlugin {
authenticationEnvironment: authenticationEnvironment,
authorizationEnvironment: nil,
credentialsClient: credentialsClient,
urlSessionClient: urlSessionClient,
logger: log)

case .identityPools(let identityPoolConfigurationData):
Expand All @@ -181,6 +186,7 @@ extension AWSCognitoAuthPlugin {
authenticationEnvironment: nil,
authorizationEnvironment: authorizationEnvironment,
credentialsClient: credentialsClient,
urlSessionClient: urlSessionClient,
logger: log)

case .userPoolsAndIdentityPools(let userPoolConfigurationData,
Expand All @@ -196,6 +202,7 @@ extension AWSCognitoAuthPlugin {
authenticationEnvironment: authenticationEnvironment,
authorizationEnvironment: authorizationEnvironment,
credentialsClient: credentialsClient,
urlSessionClient: urlSessionClient,
logger: log)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct AuthEnvironment: Environment, LoggerProvider {
let authenticationEnvironment: AuthenticationEnvironment?
let authorizationEnvironment: AuthorizationEnvironment?
let credentialsClient: CredentialStoreStateBehavior
let urlSessionClient: URLSessionClientBehavior?
let logger: Logger
}

Expand Down
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
}
}
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()
})
}
}

}
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)

}

0 comments on commit 8bf2493

Please sign in to comment.