Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisabhash committed Nov 29, 2023
1 parent 247f390 commit 316d580
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ extension AWSCognitoAuthPlugin {
let credentialsClient = CredentialStoreOperationClient(
credentialStoreStateMachine: credentialStoreMachine)

let urlSessionClient = URLSessionClient(urlSession: makeURLSession())
let preInitiateAuthSignUpClient = AWSPreInitiateAuthSignUpBehavior(urlSession: makeURLSession())

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

let authStateMachine = StateMachine(resolver: authResolver, environment: authEnvironment)
Expand Down Expand Up @@ -158,7 +158,7 @@ extension AWSCognitoAuthPlugin {
private func makeAuthEnvironment(
authConfiguration: AuthConfiguration,
credentialsClient: CredentialStoreStateBehavior,
urlSessionClient: URLSessionClientBehavior
preInitiateAuthSignUpClient: PreInitiateAuthSignUpBehavior
) -> AuthEnvironment {

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

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

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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 {
struct PreInitiateAuthSignUpPayload: Codable {
let username: String
let deliveryMedium: String
let userAttributes: [String:String]?
Expand Down

This file was deleted.

This file was deleted.

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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ enum Defaults {
authenticationEnvironment: authenticationEnvironment,
authorizationEnvironment: authZEnvironment ?? authorizationEnvironment,
credentialsClient: makeCredentialStoreOperationBehavior(),
urlSessionClient: URLSessionClient(urlSession: makeURLSession()),
preInitiateAuthSignUpClient: AWSPreInitiateAuthSignUpBehavior(urlSession: makeURLSession()),
logger: Amplify.Logging.logger(forCategory: "awsCognitoAuthPluginTest")
)
Amplify.Logging.logLevel = .verbose
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ class AuthenticationProviderConfirmSigninTests: BasePluginTest {
authenticationEnvironment: nil,
authorizationEnvironment: authorizationEnvironment,
credentialsClient: Defaults.makeCredentialStoreOperationBehavior(),
urlSessionClient: nil,
preInitiateAuthSignUpClient: nil,
logger: Amplify.Logging.logger(forCategory: "awsCognitoAuthPluginTest")
)
let stateMachine = Defaults.authStateMachineWith(environment: environment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ class ConfirmSignInTOTPTaskTests: BasePluginTest {
authenticationEnvironment: nil,
authorizationEnvironment: authorizationEnvironment,
credentialsClient: Defaults.makeCredentialStoreOperationBehavior(),
urlSessionClient: nil,
preInitiateAuthSignUpClient: nil,
logger: Amplify.Logging.logger(forCategory: "awsCognitoAuthPluginTest")
)
let stateMachine = Defaults.authStateMachineWith(environment: environment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ class ConfirmSignInWithMFASelectionTaskTests: BasePluginTest {
authenticationEnvironment: nil,
authorizationEnvironment: authorizationEnvironment,
credentialsClient: Defaults.makeCredentialStoreOperationBehavior(),
urlSessionClient: nil,
preInitiateAuthSignUpClient: nil,
logger: Amplify.Logging.logger(forCategory: "awsCognitoAuthPluginTest")
)
let stateMachine = Defaults.authStateMachineWith(environment: environment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ class ConfirmSignInWithSetUpMFATaskTests: BasePluginTest {
authenticationEnvironment: nil,
authorizationEnvironment: authorizationEnvironment,
credentialsClient: Defaults.makeCredentialStoreOperationBehavior(),
urlSessionClient: nil,
preInitiateAuthSignUpClient: nil,
logger: Amplify.Logging.logger(forCategory: "awsCognitoAuthPluginTest")
)
let stateMachine = Defaults.authStateMachineWith(environment: environment,
Expand Down
Loading

0 comments on commit 316d580

Please sign in to comment.