Skip to content

Commit

Permalink
update rules
Browse files Browse the repository at this point in the history
  • Loading branch information
phantumcode committed Sep 13, 2024
1 parent 05f004f commit b780f1c
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ struct ValidateCredentialsAndConfiguration: Action {
func execute(withDispatcher dispatcher: EventDispatcher, environment: Environment) async {

logVerbose("\(#fileID) Starting execution", environment: environment)
var event: StateMachineEvent = switch authConfiguration {
var event: StateMachineEvent
switch authConfiguration {
case .identityPools:
AuthEvent(eventType: .configureAuthorization(
event = AuthEvent(eventType: .configureAuthorization(
authConfiguration,
cachedCredentials
))
default:
AuthEvent(eventType: .configureAuthentication(
event = AuthEvent(eventType: .configureAuthentication(
authConfiguration,
cachedCredentials
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ struct InitializeFederationToIdentityPool: Action {
func execute(withDispatcher dispatcher: EventDispatcher, environment: Environment) async {
logVerbose("\(#fileID) Starting execution", environment: environment)
let authProviderLoginsMap = AuthProviderLoginsMap(federatedToken: federatedToken)
let event = if let developerProvidedIdentityId {
FetchAuthSessionEvent.init(
let event: FetchAuthSessionEvent

if let developerProvidedIdentityId {
event = FetchAuthSessionEvent.init(
eventType: .fetchAWSCredentials(
developerProvidedIdentityId,
authProviderLoginsMap
))
} else {
FetchAuthSessionEvent.init(
event = FetchAuthSessionEvent.init(
eventType: .fetchAuthenticatedIdentityID(authProviderLoginsMap))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ struct FetchHostedUISignInToken: Action {
logVerbose("\(#fileID) Starting execution", environment: environment)

guard let environment = environment as? AuthEnvironment,
let hostedUIEnvironment = environment.hostedUIEnvironment
else {
let hostedUIEnvironment = environment.hostedUIEnvironment else {
let message = AuthPluginErrorConstants.configurationError
let error = AuthenticationError.configuration(message: message)
let event = AuthenticationEvent(eventType: .error(error))
Expand Down Expand Up @@ -74,8 +73,7 @@ struct FetchHostedUISignInToken: Action {
guard let json = try JSONSerialization.jsonObject(
with: data,
options: []
) as? [String: Any]
else {
) as? [String: Any] else {
throw HostedUIError.tokenParsing
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class ShowHostedUISignIn: NSObject, Action {
logVerbose("\(#fileID) Starting execution", environment: environment)

guard let environment = environment as? AuthEnvironment,
let hostedUIEnvironment = environment.hostedUIEnvironment
else {
let hostedUIEnvironment = environment.hostedUIEnvironment else {
let message = AuthPluginErrorConstants.configurationError
let error = AuthenticationError.configuration(message: message)
let event = AuthenticationEvent(eventType: .error(error))
Expand All @@ -36,8 +35,7 @@ class ShowHostedUISignIn: NSObject, Action {
let hostedUIConfig = hostedUIEnvironment.configuration

guard let callbackURL = URL(string: hostedUIConfig.oauth.signInRedirectURI),
let callbackURLScheme = callbackURL.scheme
else {
let callbackURLScheme = callbackURL.scheme else {
let event = SignInEvent(eventType: .throwAuthError(.hostedUI(.signInURI)))
logVerbose("\(#fileID) Sending event \(event)", environment: environment)
await dispatcher.send(event)
Expand All @@ -58,8 +56,7 @@ class ShowHostedUISignIn: NSObject, Action {

guard let code = queryItems.first(where: { $0.name == "code" })?.value,
let state = queryItems.first(where: { $0.name == "state" })?.value,
signingInData.state == state
else {
signingInData.state == state else {

let event = HostedUIEvent(eventType: .throwError(.hostedUI(.codeValidation)))
logVerbose("\(#fileID) Sending event \(event)", environment: environment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ struct InitiateSignOut: Action {
func execute(withDispatcher dispatcher: EventDispatcher, environment: Environment) async {
logVerbose("\(#fileID) Starting execution", environment: environment)

let event = if case .hostedUI(let options) = signedInData.signInMethod,
let event: SignOutEvent
if case .hostedUI(let options) = signedInData.signInMethod,
options.preferPrivateSession == false {
SignOutEvent(eventType: .invokeHostedUISignOut(
event = SignOutEvent(eventType: .invokeHostedUISignOut(
signOutEventData,
signedInData
))
} else if signOutEventData.globalSignOut {
SignOutEvent(eventType: .signOutGlobally(signedInData))
event = SignOutEvent(eventType: .signOutGlobally(signedInData))
} else {
SignOutEvent(eventType: .revokeToken(signedInData))
event = SignOutEvent(eventType: .revokeToken(signedInData))
}
logVerbose("\(#fileID) Sending event \(event.type)", environment: environment)
await dispatcher.send(event)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ extension RespondToAuthChallenge {

var codeDeliveryDetails: AuthCodeDeliveryDetails {
guard let parameters,
let medium = parameters["CODE_DELIVERY_DELIVERY_MEDIUM"]
else {
let medium = parameters["CODE_DELIVERY_DELIVERY_MEDIUM"] else {
return AuthCodeDeliveryDetails(
destination: .unknown(nil),
attributeKey: nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ import Foundation
extension SignInChallengeState: CustomDebugDictionaryConvertible {

var debugDictionary: [String: Any] {
let additionalMetadataDictionary: [String: Any] = switch self {
let additionalMetadataDictionary: [String: Any]
switch self {

case .waitingForAnswer(let respondAuthChallenge, _),
.verifying(let respondAuthChallenge, _, _):
respondAuthChallenge.debugDictionary
additionalMetadataDictionary = respondAuthChallenge.debugDictionary
case .error(let respondAuthChallenge, _, let error):
respondAuthChallenge.debugDictionary.merging(
additionalMetadataDictionary = respondAuthChallenge.debugDictionary.merging(
[
"error": error
],
uniquingKeysWith: {$1}
)
default: [:]
default: additionalMetadataDictionary = [:]
}
return [type: additionalMetadataDictionary]
}
Expand Down
7 changes: 4 additions & 3 deletions AmplifyPlugins/Auth/Sources/AmplifySRP/HKDF.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@ public enum HMACKeyDerivationFunction {
outputLength: Int
) -> Data {
let key = SymmetricKey(data: keyingMaterial)
var hkdf: SymmetricKey = if let info {
HKDF<SHA256>.deriveKey(
var hkdf: SymmetricKey
if let info {
hkdf = HKDF<SHA256>.deriveKey(
inputKeyMaterial: key,
salt: salt,
info: Data(info.utf8),
outputByteCount: outputLength
)
} else {
HKDF<SHA256>.deriveKey(
hkdf = HKDF<SHA256>.deriveKey(
inputKeyMaterial: key,
salt: salt,
outputByteCount: outputLength
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ class MigrateLegacyCredentialStoreTests: XCTestCase {
signedInData: let signedInData,
identityID: let identityID,
credentials: let awsCredentials
) = credentials
else {
) = credentials else {
XCTFail("The credentials saved should be of type AmplifyCredentials")
return
}
Expand Down Expand Up @@ -143,8 +142,7 @@ class MigrateLegacyCredentialStoreTests: XCTestCase {
await action.execute(
withDispatcher: MockDispatcher { event in
guard let event = event as? CredentialStoreEvent,
case let .throwError(error) = event.eventType
else {
case let .throwError(error) = event.eventType else {
XCTFail("Expected failure due to no CredentialEnvironment")
expectation.fulfill()
return
Expand All @@ -168,8 +166,7 @@ class MigrateLegacyCredentialStoreTests: XCTestCase {
await action.execute(
withDispatcher: MockDispatcher { event in
guard let event = event as? CredentialStoreEvent,
case .loadCredentialStore(let type) = event.eventType
else {
case .loadCredentialStore(let type) = event.eventType else {
XCTFail("Expected .loadCredentialStore")
expectation.fulfill()
return
Expand All @@ -184,8 +181,7 @@ class MigrateLegacyCredentialStoreTests: XCTestCase {
MockAmplifyCredentialStoreBehavior(
saveCredentialHandler: { codableCredentials in
guard let amplifyCredentials = codableCredentials as? AmplifyCredentials,
case .identityPoolOnly(_, let credentials) = amplifyCredentials
else {
case .identityPoolOnly(_, let credentials) = amplifyCredentials else {
XCTFail("Expected .identityPoolOnly")
return
}
Expand Down Expand Up @@ -213,8 +209,7 @@ class MigrateLegacyCredentialStoreTests: XCTestCase {
await action.execute(
withDispatcher: MockDispatcher { event in
guard let event = event as? CredentialStoreEvent,
case .loadCredentialStore(let type) = event.eventType
else {
case .loadCredentialStore(let type) = event.eventType else {
XCTFail("Expected .loadCredentialStore")
expectation.fulfill()
return
Expand All @@ -229,8 +224,7 @@ class MigrateLegacyCredentialStoreTests: XCTestCase {
MockAmplifyCredentialStoreBehavior(
saveCredentialHandler: { codableCredentials in
guard let amplifyCredentials = codableCredentials as? AmplifyCredentials,
case .identityPoolWithFederation(let token, _, _) = amplifyCredentials
else {
case .identityPoolWithFederation(let token, _, _) = amplifyCredentials else {
XCTFail("Expected .identityPoolWithFederation")
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ class AWSAuthSignOutTaskTests: BasePluginTest {
}
)
guard let result = await plugin.signOut() as? AWSCognitoSignOutResult,
case .complete = result
else {
case .complete = result else {
XCTFail("Did not return complete signOut")
return
}
Expand All @@ -52,8 +51,7 @@ class AWSAuthSignOutTaskTests: BasePluginTest {
revokeTokenError: let revokeTokenError,
globalSignOutError: let globalSignOutError,
hostedUIError: let hostedUIError
) = result
else {
) = result else {
XCTFail("Did not return partial signOut")
return
}
Expand All @@ -76,8 +74,7 @@ class AWSAuthSignOutTaskTests: BasePluginTest {
revokeTokenError: let revokeTokenError,
globalSignOutError: let globalSignOutError,
hostedUIError: let hostedUIError
) = result
else {
) = result else {
XCTFail("Did not return partial signOut")
return
}
Expand All @@ -97,8 +94,7 @@ class AWSAuthSignOutTaskTests: BasePluginTest {

guard let result = await authPlugin.signOut() as? AWSCognitoSignOutResult,
case .failed(let authError) = result,
case .invalidState = authError
else {
case .invalidState = authError else {

XCTFail("Sign out during federation should not succeed")
return
Expand Down Expand Up @@ -138,8 +134,7 @@ class AWSAuthSignOutTaskTests: BasePluginTest {
let authPlugin = configureCustomPluginWith(initialState: initialState)

guard let result = await authPlugin.signOut() as? AWSCognitoSignOutResult,
case .complete = result
else {
case .complete = result else {

XCTFail("Sign out during guest should succeed")
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ class AWSAuthConfirmSignUpAPITests: BasePluginTest {
let errorDescription,
let recoverySuggestion,
let notAuthorizedError
) = authError
else {
) = authError else {
XCTFail("Auth error should be of type notAuthorized")
return
}
Expand Down Expand Up @@ -274,8 +273,7 @@ class AWSAuthConfirmSignUpAPITests: BasePluginTest {
let errorMessage,
let recovery,
let serviceError
) = authError
else {
) = authError else {
XCTFail("Auth error should be of type service error")
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,7 @@ class AWSAuthSignUpAPITests: BasePluginTest {
let errorDescription,
let recoverySuggestion,
let notAuthorizedError
) = authError
else {
) = authError else {
XCTFail("Auth error should be of type notAuthorized")
return
}
Expand Down Expand Up @@ -312,8 +311,7 @@ class AWSAuthSignUpAPITests: BasePluginTest {
let errorMessage,
let recovery,
let serviceError
) = authError
else {
) = authError else {
XCTFail("Auth error should be of type service error")
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import AWSPluginsCore
import Foundation
@testable import AWSCognitoAuthPlugin

extension DeviceMetadata {
public extension DeviceMetadata {

init(from decoder: Decoder) throws {
self = .noData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ class CredentialStoreConfigurationTests: AWSAuthBaseTest {

// Then
guard let credentials = try? newCredentialStore.retrieveCredential(),
case .identityPoolOnly(let retrievedIdentityID, let retrievedCredentials) = credentials
else {
case .identityPoolOnly(let retrievedIdentityID, let retrievedCredentials) = credentials else {
XCTFail("Unable to retrieve Credentials")
return
}
Expand Down Expand Up @@ -104,8 +103,7 @@ class CredentialStoreConfigurationTests: AWSAuthBaseTest {
let retrievedTokens,
let retrievedIdentityID,
let retrievedCredentials
) = credentials
else {
) = credentials else {
XCTFail("Unable to retrieve Credentials")
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ final class AuthStressTests: AuthStressBaseTest {
for index in 1 ... concurrencyLimit {
Task {
// Randomly yield the task so that below execution of force refresh happens
let session: AuthSession = if index == concurrencyLimit / 2 {
try await Amplify.Auth.fetchAuthSession(options: .forceRefresh())
let session: AuthSession
if index == concurrencyLimit / 2 {
session = try await Amplify.Auth.fetchAuthSession(options: .forceRefresh())
} else {
try await Amplify.Auth.fetchAuthSession()
session = try await Amplify.Auth.fetchAuthSession()
}
guard let cognitoSession = session as? AWSAuthCognitoSession,
let _ = try? cognitoSession.identityIdResult.get()
else {
let _ = try? cognitoSession.identityIdResult.get() else {
XCTFail("Could not fetch Identity ID")
return
}
Expand Down

0 comments on commit b780f1c

Please sign in to comment.