Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(auth): resolve swiftformat errors and warnings #3846

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct ASFAppInfo: ASFAppInfoBehavior {
}

var targetSDK: String {
var targetSDK: String = ""
var targetSDK = ""
#if os(iOS) || os(watchOS) || os(tvOS)
targetSDK = "\(__IPHONE_OS_VERSION_MIN_REQUIRED)"
#elseif os(macOS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// SPDX-License-Identifier: Apache-2.0
//

import Foundation
import Amplify
import Foundation

@MainActor
struct ASFDeviceInfo: ASFDeviceBehavior {
Expand All @@ -28,9 +28,13 @@ struct ASFDeviceInfo: ASFDeviceBehavior {
var type: String {
var systemInfo = utsname()
uname(&systemInfo)
return String(bytes: Data(bytes: &systemInfo.machine,
count: Int(_SYS_NAMELEN)),
encoding: .utf8) ?? DeviceInfo.current.hostName
return String(
bytes: Data(
bytes: &systemInfo.machine,
count: Int(_SYS_NAMELEN)
),
encoding: .utf8
) ?? DeviceInfo.current.hostName
}

var platform: String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import Foundation

protocol AdvancedSecurityBehavior {

func userContextData(for username: String,
deviceInfo: ASFDeviceBehavior,
appInfo: ASFAppInfoBehavior,
configuration: UserPoolConfigurationData) throws -> String
func userContextData(
for username: String,
deviceInfo: ASFDeviceBehavior,
appInfo: ASFAppInfoBehavior,
configuration: UserPoolConfigurationData
) throws -> String
}

protocol ASFDeviceBehavior: Sendable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ extension CognitoUserPoolASF {

static func asfDeviceID(
for username: String,
credentialStoreClient: CredentialStoreStateBehavior) async throws -> String {
credentialStoreClient: CredentialStoreStateBehavior
) async throws -> String {
let deviceMetaDataType = CredentialStoreDataType.deviceMetadata(username: username)
let data = try? await credentialStoreClient.fetchData(type: deviceMetaDataType)
if case .deviceMetadata(let metadata, _) = data,
Expand All @@ -29,10 +30,12 @@ extension CognitoUserPoolASF {
return uuid
}

static func encodedContext(username: String,
asfDeviceId: String,
asfClient: AdvancedSecurityBehavior,
userPoolConfiguration: UserPoolConfigurationData) async -> String? {
static func encodedContext(
username: String,
asfDeviceId: String,
asfClient: AdvancedSecurityBehavior,
userPoolConfiguration: UserPoolConfigurationData
) async -> String? {
let deviceInfo: ASFDeviceBehavior = await ASFDeviceInfo(id: asfDeviceId)
let appInfo: ASFAppInfoBehavior = ASFAppInfo()

Expand All @@ -41,7 +44,8 @@ extension CognitoUserPoolASF {
for: username,
deviceInfo: deviceInfo,
appInfo: appInfo,
configuration: userPoolConfiguration)
configuration: userPoolConfiguration
)
} catch {
// Ignore the error and add nil as context data
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// SPDX-License-Identifier: Apache-2.0
//

import Foundation
import CryptoKit
import Foundation

struct CognitoUserPoolASF: AdvancedSecurityBehavior {

Expand All @@ -27,23 +27,31 @@ struct CognitoUserPoolASF: AdvancedSecurityBehavior {
static let phoneTypeKey = "PhoneType"
static let asfVersion = "IOS20171114"

func userContextData(for username: String = "unknown",
deviceInfo: ASFDeviceBehavior,
appInfo: ASFAppInfoBehavior,
configuration: UserPoolConfigurationData) throws -> String {
func userContextData(
for username: String = "unknown",
deviceInfo: ASFDeviceBehavior,
appInfo: ASFAppInfoBehavior,
configuration: UserPoolConfigurationData
) throws -> String {

let contextData = prepareUserContextData(deviceInfo: deviceInfo, appInfo: appInfo)
let payload = try prepareJsonPayload(username: username,
contextData: contextData,
userPoolId: configuration.poolId)
let signature = try calculateSecretHash(contextJson: payload,
clientId: configuration.clientId)
let payload = try prepareJsonPayload(
username: username,
contextData: contextData,
userPoolId: configuration.poolId
)
let signature = try calculateSecretHash(
contextJson: payload,
clientId: configuration.clientId
)
let result = try prepareJsonResult(payload: payload, signature: signature)
return result
}

func prepareUserContextData(deviceInfo: ASFDeviceBehavior,
appInfo: ASFAppInfoBehavior) -> [String: String] {
func prepareUserContextData(
deviceInfo: ASFDeviceBehavior,
appInfo: ASFAppInfoBehavior
) -> [String: String] {
var build = "release"
#if DEBUG
build = "debug"
Expand Down Expand Up @@ -73,10 +81,12 @@ struct CognitoUserPoolASF: AdvancedSecurityBehavior {
return contextData
}

func prepareJsonPayload(username: String,
contextData: [String: String],
userPoolId: String) throws -> String {
let timestamp = String(format: "%lli", floor(Date().timeIntervalSince1970 * 1000))
func prepareJsonPayload(
username: String,
contextData: [String: String],
userPoolId: String
) throws -> String {
let timestamp = String(format: "%lli", floor(Date().timeIntervalSince1970 * 1_000))
let payload = [
"contextData": contextData,
"username": username,
Expand All @@ -92,8 +102,8 @@ struct CognitoUserPoolASF: AdvancedSecurityBehavior {

func timeZoneOffet(seconds: Int = TimeZone.current.secondsFromGMT()) -> String {

let hours = seconds/3600
let minutes = abs(seconds/60) % 60
let hours = seconds / 3_600
let minutes = abs(seconds / 60) % 60
return String(format: "%+.2d:%.2d", hours, minutes)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

import Foundation
@_spi(InternalAmplifyConfiguration) import Amplify
import AWSClientRuntime
import AWSCognitoIdentity
import AWSCognitoIdentityProvider
import AWSPluginsCore
import ClientRuntime
import AWSClientRuntime
@_spi(PluginHTTPClientEngine) import InternalAmplifyCredentials
@_spi(InternalHttpEngineProxy) import AWSPluginsCore

Expand All @@ -33,13 +33,16 @@ extension AWSCognitoAuthPlugin {
} else {
throw PluginError.pluginConfigurationError(
AuthPluginErrorConstants.decodeConfigurationError.errorDescription,
AuthPluginErrorConstants.decodeConfigurationError.recoverySuggestion)
AuthPluginErrorConstants.decodeConfigurationError.recoverySuggestion
)
}

let credentialStoreResolver = CredentialStoreState.Resolver().eraseToAnyResolver()
let credentialEnvironment = credentialStoreEnvironment(authConfiguration: authConfiguration)
let credentialStoreMachine = StateMachine(resolver: credentialStoreResolver,
environment: credentialEnvironment)
let credentialStoreMachine = StateMachine(
resolver: credentialStoreResolver,
environment: credentialEnvironment
)
let credentialsClient = CredentialStoreOperationClient(
credentialStoreStateMachine: credentialStoreMachine)

Expand All @@ -54,35 +57,40 @@ extension AWSCognitoAuthPlugin {
let hubEventHandler = AuthHubEventHandler()
let analyticsHandler = try UserPoolAnalytics(
authConfiguration.getUserPoolConfiguration(),
credentialStoreEnvironment: credentialEnvironment.credentialStoreEnvironment)

configure(authConfiguration: authConfiguration,
authEnvironment: authEnvironment,
authStateMachine: authStateMachine,
credentialStoreStateMachine: credentialStoreMachine,
hubEventHandler: hubEventHandler,
analyticsHandler: analyticsHandler)
credentialStoreEnvironment: credentialEnvironment.credentialStoreEnvironment
)

configure(
authConfiguration: authConfiguration,
authEnvironment: authEnvironment,
authStateMachine: authStateMachine,
credentialStoreStateMachine: credentialStoreMachine,
hubEventHandler: hubEventHandler,
analyticsHandler: analyticsHandler
)
}

func configure(authConfiguration: AuthConfiguration,
authEnvironment: AuthEnvironment,
authStateMachine: AuthStateMachine,
credentialStoreStateMachine: CredentialStoreStateMachine,
hubEventHandler: AuthHubEventBehavior,
analyticsHandler: UserPoolAnalyticsBehavior,
queue: OperationQueue = OperationQueue()) {
func configure(
authConfiguration: AuthConfiguration,
authEnvironment: AuthEnvironment,
authStateMachine: AuthStateMachine,
credentialStoreStateMachine: CredentialStoreStateMachine,
hubEventHandler: AuthHubEventBehavior,
analyticsHandler: UserPoolAnalyticsBehavior,
queue: OperationQueue = OperationQueue()
) {

self.authConfiguration = authConfiguration
self.queue = queue
self.queue.maxConcurrentOperationCount = 1
self.authEnvironment = authEnvironment
self.authStateMachine = authStateMachine
self.credentialStoreStateMachine = credentialStoreStateMachine
self.internalConfigure()
self.listenToStateMachineChanges()
internalConfigure()
listenToStateMachineChanges()
self.hubEventHandler = hubEventHandler
self.analyticsHandler = analyticsHandler
self.taskQueue = TaskQueue()
taskQueue = TaskQueue()
}

// MARK: - Configure Helpers
Expand All @@ -94,7 +102,7 @@ extension AWSCognitoAuthPlugin {
serviceSpecific: .init(endpointResolver: userPoolConfig.endpoint?.resolver)
)

if var httpClientEngineProxy = httpClientEngineProxy {
if var httpClientEngineProxy {
httpClientEngineProxy.target = baseClientEngine(for: configuration)
configuration.httpClientEngine = UserAgentSettingClientEngine(
target: httpClientEngineProxy
Expand Down Expand Up @@ -201,7 +209,8 @@ extension AWSCognitoAuthPlugin {
authenticationEnvironment: authenticationEnvironment,
authorizationEnvironment: nil,
credentialsClient: credentialsClient,
logger: log)
logger: log
)

case .identityPools(let identityPoolConfigurationData):
let authorizationEnvironment = authorizationEnvironment(
Expand All @@ -213,10 +222,13 @@ extension AWSCognitoAuthPlugin {
authenticationEnvironment: nil,
authorizationEnvironment: authorizationEnvironment,
credentialsClient: credentialsClient,
logger: log)
logger: log
)

case .userPoolsAndIdentityPools(let userPoolConfigurationData,
let identityPoolConfigurationData):
case .userPoolsAndIdentityPools(
let userPoolConfigurationData,
let identityPoolConfigurationData
):
let authenticationEnvironment = authenticationEnvironment(
userPoolConfigData: userPoolConfigurationData)
let authorizationEnvironment = authorizationEnvironment(
Expand All @@ -228,39 +240,49 @@ extension AWSCognitoAuthPlugin {
authenticationEnvironment: authenticationEnvironment,
authorizationEnvironment: authorizationEnvironment,
credentialsClient: credentialsClient,
logger: log)
logger: log
)
}
}

private func authenticationEnvironment(userPoolConfigData: UserPoolConfigurationData) -> AuthenticationEnvironment {

let srpAuthEnvironment = BasicSRPAuthEnvironment(userPoolConfiguration: userPoolConfigData,
cognitoUserPoolFactory: makeUserPool)
let srpAuthEnvironment = BasicSRPAuthEnvironment(
userPoolConfiguration: userPoolConfigData,
cognitoUserPoolFactory: makeUserPool
)
let srpSignInEnvironment = BasicSRPSignInEnvironment(srpAuthEnvironment: srpAuthEnvironment)
let userPoolEnvironment = BasicUserPoolEnvironment(
userPoolConfiguration: userPoolConfigData,
cognitoUserPoolFactory: makeUserPool,
cognitoUserPoolASFFactory: makeCognitoASF,
cognitoUserPoolAnalyticsHandlerFactory: makeUserPoolAnalytics)
cognitoUserPoolAnalyticsHandlerFactory: makeUserPoolAnalytics
)
let hostedUIEnvironment = hostedUIEnvironment(userPoolConfigData)
return BasicAuthenticationEnvironment(srpSignInEnvironment: srpSignInEnvironment,
userPoolEnvironment: userPoolEnvironment,
hostedUIEnvironment: hostedUIEnvironment)
return BasicAuthenticationEnvironment(
srpSignInEnvironment: srpSignInEnvironment,
userPoolEnvironment: userPoolEnvironment,
hostedUIEnvironment: hostedUIEnvironment
)
}

private func hostedUIEnvironment(_ configuration: UserPoolConfigurationData) -> HostedUIEnvironment? {
guard let hostedUIConfig = configuration.hostedUIConfig else {
return nil
}
return BasicHostedUIEnvironment(configuration: hostedUIConfig,
hostedUISessionFactory: makeHostedUISession,
urlSessionFactory: makeURLSession,
randomStringFactory: makeRandomString)
return BasicHostedUIEnvironment(
configuration: hostedUIConfig,
hostedUISessionFactory: makeHostedUISession,
urlSessionFactory: makeURLSession,
randomStringFactory: makeRandomString
)
}

private func authorizationEnvironment(identityPoolConfigData: IdentityPoolConfigurationData) -> AuthorizationEnvironment {
BasicAuthorizationEnvironment(identityPoolConfiguration: identityPoolConfigData,
cognitoIdentityFactory: makeIdentityClient)
BasicAuthorizationEnvironment(
identityPoolConfiguration: identityPoolConfigData,
cognitoIdentityFactory: makeIdentityClient
)
}

private func credentialStoreEnvironment(authConfiguration: AuthConfiguration) -> CredentialEnvironment {
Expand All @@ -278,8 +300,9 @@ extension AWSCognitoAuthPlugin {
let operation = AuthConfigureOperation(
request: request,
authStateMachine: authStateMachine,
credentialStoreStateMachine: credentialStoreStateMachine)
self.queue.addOperation(operation)
credentialStoreStateMachine: credentialStoreStateMachine
)
queue.addOperation(operation)
}
}

Expand Down
Loading
Loading