amplify-swift/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Actions/CredentialStore/ClearCredentialStore.swift

77 lines
2.8 KiB
Swift

//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
import Foundation
import AWSPluginsCore
struct ClearCredentialStore: Action {
let identifier = "ClearCredentialStore"
let dataStoreType: CredentialStoreDataType
func execute(withDispatcher dispatcher: EventDispatcher, environment: Environment) async {
logVerbose("\(#fileID) Starting execution", environment: environment)
guard let credentialEnvironment = environment as? CredentialEnvironment else {
let event = CredentialStoreEvent(
eventType: .throwError(KeychainStoreError.configuration(
message: AuthPluginErrorConstants.configurationError)))
logVerbose("\(#fileID) Sending event \(event.type)", environment: environment)
await dispatcher.send(event)
return
}
let credentialStoreEnvironment = credentialEnvironment.credentialStoreEnvironment
let amplifyCredentialStore = credentialStoreEnvironment.amplifyCredentialStoreFactory()
do {
let dataType: CredentialStoreDataType
switch dataStoreType {
case .amplifyCredentials:
try amplifyCredentialStore.deleteCredential()
dataType = .amplifyCredentials
case .deviceMetadata(let username):
try amplifyCredentialStore.removeDevice(for: username)
dataType = .deviceMetadata(username: username)
case .asfDeviceId(username: let username):
try amplifyCredentialStore.removeASFDevice(for: username)
dataType = .asfDeviceId(username: username)
}
let event = CredentialStoreEvent(eventType: .credentialCleared(dataType))
logVerbose("\(#fileID) Sending event \(event.type)", environment: environment)
await dispatcher.send(event)
} catch let error as KeychainStoreError {
let event = CredentialStoreEvent(eventType: .throwError(error))
logVerbose("\(#fileID) Sending event \(event.type)", environment: environment)
await dispatcher.send(event)
} catch {
let event = CredentialStoreEvent(
eventType: .throwError(KeychainStoreError.unknown("An unknown error occurred", error)))
logVerbose("\(#fileID) Sending event \(event.type)", environment: environment)
await dispatcher.send(event)
}
}
}
extension ClearCredentialStore: CustomDebugDictionaryConvertible {
var debugDictionary: [String: Any] {
[
"identifier": identifier
]
}
}
extension ClearCredentialStore: CustomDebugStringConvertible {
var debugDescription: String {
debugDictionary.debugDescription
}
}