Skip to content

Commit

Permalink
fix: refactor keychain errors (#3354)
Browse files Browse the repository at this point in the history
* fix: refactor keychain errors

* fix: debug dictionary not properly formatted.

* making keychain store error as Auth convertible

* Update AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/StateMachine/CodeGen/States/DebugInfo/RefreshSessionState+Debug.swift

Co-authored-by: Ian Saultz <[email protected]>

* worked on review comment

* worked on comments to narrow the keychain status list

---------

Co-authored-by: Ian Saultz <[email protected]>
  • Loading branch information
harsh62 and atierian authored Nov 10, 2023
1 parent 54c763c commit a659848
Show file tree
Hide file tree
Showing 19 changed files with 174 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extension AuthState: CustomDebugStringConvertible {

var debugDictionary: [String: Any] {

var additionalMetadataDictionary: [String: Any] = [:]
let additionalMetadataDictionary: [String: Any]

switch self {
case .notConfigured:
Expand All @@ -36,6 +36,5 @@ extension AuthState: CustomDebugStringConvertible {

var debugDescription: String {
return (debugDictionary as AnyObject).description

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
extension AuthenticationState: CustomDebugDictionaryConvertible {

var debugDictionary: [String: Any] {
var additionalMetadataDictionary: [String: Any] = [:]
let additionalMetadataDictionary: [String: Any]
switch self {
case .notConfigured:
additionalMetadataDictionary = [:]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation

extension AuthorizationState: CustomDebugDictionaryConvertible {
var debugDictionary: [String: Any] {
var additionalMetadataDictionary: [String: Any] = [:]
let additionalMetadataDictionary: [String: Any]

switch self {
case .notConfigured,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,37 @@

import Foundation

extension CredentialStoreState {
extension CredentialStoreState: CustomDebugStringConvertible {
var debugDictionary: [String: Any] {
let stateTypeDictionary: [String: Any] = ["CredentialStoreState": type]
var additionalMetadataDictionary: [String: Any] = [:]
let additionalMetadataDictionary: [String: Any]

switch self {
case .notConfigured:
additionalMetadataDictionary = [:]
case .migratingLegacyStore:
additionalMetadataDictionary = [:]
case .loadingStoredCredentials:
additionalMetadataDictionary = [:]
case .clearingCredentials:
case .notConfigured,
.migratingLegacyStore,
.loadingStoredCredentials,
.storingCredentials,
.clearingCredentials,
.idle:
additionalMetadataDictionary = [:]
case .clearedCredential(let dataType):
additionalMetadataDictionary = ["StoreDataType": dataType]
case .storingCredentials:
additionalMetadataDictionary = [:]
case .success:
additionalMetadataDictionary = [:]
case .error:
additionalMetadataDictionary = [:]
case .idle:
additionalMetadataDictionary = [:]
additionalMetadataDictionary = [
"clearedDataType": dataType
]
case .success(let data):
additionalMetadataDictionary = [
"savedData": data
]
case .error(let error):
additionalMetadataDictionary = [
"errorType": error
]
}
return stateTypeDictionary.merging(additionalMetadataDictionary, uniquingKeysWith: { $1 })
return [type: additionalMetadataDictionary]
}


var debugDescription: String {
return (debugDictionary as AnyObject).description
}

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

import Foundation

extension CustomSignInState {
extension CustomSignInState: CustomDebugDictionaryConvertible {

var debugDictionary: [String: Any] {

var additionalMetadataDictionary: [String: Any] = [:]
let additionalMetadataDictionary: [String: Any]
switch self {
case .notStarted:
additionalMetadataDictionary = [:]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
extension DeleteUserState: CustomDebugDictionaryConvertible {

var debugDictionary: [String: Any] {
var additionalMetadataDictionary: [String: Any] = [:]
let additionalMetadataDictionary: [String: Any]
switch self {
case .notStarted:
additionalMetadataDictionary = [:]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

import Foundation

extension DeviceSRPState {
extension DeviceSRPState: CustomDebugDictionaryConvertible {

var debugDictionary: [String: Any] {

var additionalMetadataDictionary: [String: Any] = [:]
let additionalMetadataDictionary: [String: Any]
switch self {
case .notStarted:
additionalMetadataDictionary = [:]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

import Foundation

extension FetchAuthSessionState {
extension FetchAuthSessionState: CustomDebugDictionaryConvertible {

var debugDictionary: [String: Any] {
var additionalMetadataDictionary: [String: Any] = [:]
let additionalMetadataDictionary: [String: Any]
switch self {
case .notStarted:
additionalMetadataDictionary = [:]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
extension HostedUISignInState: CustomDebugDictionaryConvertible {

var debugDictionary: [String: Any] {
var additionalMetadataDictionary: [String: Any] = [:]
let additionalMetadataDictionary: [String: Any]
switch self {
case .notStarted:
additionalMetadataDictionary = [:]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

import Foundation

extension MigrateSignInState {
extension MigrateSignInState: CustomDebugDictionaryConvertible {

var debugDictionary: [String: Any] {

var additionalMetadataDictionary: [String: Any] = [:]
let additionalMetadataDictionary: [String: Any]

switch self {

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

import Foundation

extension RefreshSessionState {
extension RefreshSessionState: CustomDebugDictionaryConvertible {

var debugDictionary: [String: Any] {
var additionalMetadataDictionary: [String: Any] = [:]
let additionalMetadataDictionary: [String: Any]
switch self {
case .fetchingAuthSessionWithUserPool(let state, _):
additionalMetadataDictionary = ["fetchingSession": state.debugDictionary]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

import Foundation

extension SRPSignInState {
extension SRPSignInState: CustomDebugDictionaryConvertible {

var debugDictionary: [String: Any] {

var additionalMetadataDictionary: [String: Any] = [:]
let additionalMetadataDictionary: [String: Any]
switch self {
case .notStarted:
additionalMetadataDictionary = [:]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@

import Foundation

extension SignInChallengeState {
extension SignInChallengeState: CustomDebugDictionaryConvertible {

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

case .waitingForAnswer(let respondAuthChallenge, _),
.verifying(let respondAuthChallenge, _, _):
additionalMetadataDictionary = respondAuthChallenge.debugDictionary
case .error(let respondAuthChallenge, _, let error):
additionalMetadataDictionary = respondAuthChallenge.debugDictionary
additionalMetadataDictionary["error"] = error
additionalMetadataDictionary = respondAuthChallenge.debugDictionary.merging(
[
"error": error
],
uniquingKeysWith: {$1})
default: additionalMetadataDictionary = [:]
}
return [type: additionalMetadataDictionary]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

import Foundation

extension SignInState {
extension SignInState: CustomDebugDictionaryConvertible {

var debugDictionary: [String: Any] {

var additionalMetadataDictionary: [String: Any] = [:]
let additionalMetadataDictionary: [String: Any]

switch self {

Expand All @@ -22,9 +22,13 @@ extension SignInState {
case .signingInWithHostedUI(let substate):
additionalMetadataDictionary = substate.debugDictionary
case .resolvingChallenge(let challengeState, let challengeType, let signInMethod):
additionalMetadataDictionary = challengeState.debugDictionary
additionalMetadataDictionary["challengeType"] = challengeType
additionalMetadataDictionary["signInMethod"] = signInMethod

additionalMetadataDictionary = challengeState.debugDictionary.merging(
[
"challengeType": challengeType,
"signInMethod": signInMethod
],
uniquingKeysWith: {$1})

case .notStarted:
additionalMetadataDictionary = [:]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@

import Foundation

extension SignInTOTPSetupState {
extension SignInTOTPSetupState: CustomDebugDictionaryConvertible {

var debugDictionary: [String: Any] {
var additionalMetadataDictionary: [String: Any] = [:]
let additionalMetadataDictionary: [String: Any]
switch self {
case .waitingForAnswer(let signInTOTPSetupData):
additionalMetadataDictionary = signInTOTPSetupData.debugDictionary
case .verifying(let signInSetupData, let confirmSignInEventData):
additionalMetadataDictionary = confirmSignInEventData.debugDictionary
additionalMetadataDictionary = additionalMetadataDictionary.merging(
additionalMetadataDictionary = confirmSignInEventData.debugDictionary.merging(
signInSetupData.debugDictionary,
uniquingKeysWith: {$1})
case .error(let error):
additionalMetadataDictionary["error"] = error
case .error(let data, let error):
additionalMetadataDictionary = [
"totpSetupData": data ?? "Nil",
"error": error
]
default: additionalMetadataDictionary = [:]
}
return [type: additionalMetadataDictionary]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

import Foundation

extension SignOutState {
extension SignOutState: CustomDebugDictionaryConvertible {

var debugDictionary: [String: Any] {

var additionalMetadataDictionary: [String: Any] = [:]
let additionalMetadataDictionary: [String: Any]

switch self {
case .error(let error):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import Foundation
import AWSPluginsCore
import Amplify

extension KeychainStoreError: AuthErrorConvertible {

var authError: AuthError {
switch self {
case .configuration(let message):
return .configuration(message, self.recoverySuggestion)
case .unknown(let errorDescription, let error):
return .unknown(errorDescription, error)
case .conversionError(let errorDescription, let error):
return .configuration(errorDescription, self.recoverySuggestion, error)
case .codingError(let errorDescription, let error):
return .configuration(errorDescription, self.recoverySuggestion, error)
case .itemNotFound:
return .service(self.errorDescription, self.recoverySuggestion)
case .securityError:
return .service(self.errorDescription, self.recoverySuggestion)
}
}
}
54 changes: 54 additions & 0 deletions AmplifyPlugins/Core/AWSPluginsCore/Keychain/KeychainStatus.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import Foundation

enum KeychainStatus {
case success
case userCanceled
case duplicateItem
case itemNotFound
case missingEntitlement
case unexpectedError(OSStatus)
}

extension KeychainStatus: CustomStringConvertible {

init(status: OSStatus) {
switch status {
case 0:
self = .success
case -128:
self = .userCanceled
case -25299:
self = .duplicateItem
case -25300:
self = .itemNotFound
case -34018:
self = .missingEntitlement
default:
self = .unexpectedError(status)
}
}

var description: String {
switch self {
case .success:
return "No error."
case .userCanceled:
return "User canceled the operation."
case .duplicateItem:
return "The specified item already exists in the keychain."
case .itemNotFound:
return "The specified item could not be found in the keychain."
case .missingEntitlement:
return "Internal error when a required entitlement isn't present, client has neither application-identifier nor keychain-access-groups entitlements."
case .unexpectedError(let status):
return "Unexpected error has occurred with status: \(status)."
}
}
}
Loading

0 comments on commit a659848

Please sign in to comment.