Skip to content

Commit 00a6498

Browse files
yaroluchkoharsh62
authored andcommitted
Indentation, clean up, and batch migration to avoid inconsistent state
1 parent 9d04bd9 commit 00a6498

File tree

7 files changed

+87
-35
lines changed

7 files changed

+87
-35
lines changed

Amplify/Categories/Auth/Models/AccessGroup.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ public struct AccessGroup {
1616
}
1717

1818
public static func none(migrateKeychainItemsOfUserSession: Bool) -> AccessGroup {
19-
return .init(name: nil, migrateKeychainItems: migrateKeychainItemsOfUserSession)
19+
return .init(migrateKeychainItems: migrateKeychainItemsOfUserSession)
2020
}
2121

2222
public static var none: AccessGroup {
2323
return .none(migrateKeychainItemsOfUserSession: false)
2424
}
2525

26-
private init(name: String?, migrateKeychainItems: Bool) {
26+
private init(name: String? = nil, migrateKeychainItems: Bool) {
2727
self.name = name
2828
self.migrateKeychainItems = migrateKeychainItems
2929
}

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/AWSCognitoAuthPlugin+Configure.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,11 @@ extension AWSCognitoAuthPlugin {
186186
}
187187

188188
private func makeCredentialStore() -> AmplifyAuthCredentialStoreBehavior {
189-
return AWSCognitoAuthCredentialStore(authConfiguration: authConfiguration, accessGroup: secureStoragePreferences?.accessGroup?.name,
190-
migrateKeychainItemsOfUserSession: secureStoragePreferences?.accessGroup?.migrateKeychainItems ?? false)
189+
return AWSCognitoAuthCredentialStore(
190+
authConfiguration: authConfiguration,
191+
accessGroup: secureStoragePreferences?.accessGroup?.name,
192+
migrateKeychainItemsOfUserSession: secureStoragePreferences?.accessGroup?.migrateKeychainItems ?? false
193+
)
191194
}
192195

193196
private func makeLegacyKeychainStore(service: String) -> KeychainStoreBehavior {

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/AWSCognitoAuthPlugin.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public final class AWSCognitoAuthPlugin: AWSCognitoAuthPluginBehavior {
4646
return "awsCognitoAuthPlugin"
4747
}
4848

49-
/// Instantiates an instance of the AWSCognitoAuthPlugin with optionally custom network
50-
/// preferences and custom secure storage preferences
49+
/// Instantiates an instance of the AWSCognitoAuthPlugin with optional custom network
50+
/// preferences and optional custom secure storage preferences
5151
/// - Parameters:
5252
/// - networkPreferences: network preferences
5353
/// - secureStoragePreferences: secure storage preferences

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/CredentialStorage/AWSCognitoAuthCredentialStore.swift

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ struct AWSCognitoAuthCredentialStore {
3535
private let userDefaults = UserDefaults.standard
3636
private let accessGroup: String?
3737

38-
init(authConfiguration: AuthConfiguration, accessGroup: String? = nil, migrateKeychainItemsOfUserSession: Bool = false) {
38+
init(
39+
authConfiguration: AuthConfiguration,
40+
accessGroup: String? = nil,
41+
migrateKeychainItemsOfUserSession: Bool = false
42+
) {
3943
self.authConfiguration = authConfiguration
4044
self.accessGroup = accessGroup
4145
if let accessGroup {
@@ -241,33 +245,14 @@ extension AWSCognitoAuthCredentialStore: AmplifyAuthCredentialStoreBehavior {
241245
return
242246
}
243247

244-
let oldItems: [(key: String, value: Data)]
245-
do {
246-
oldItems = try oldKeychain._getAll()
247-
} catch {
248-
log.error("[AWSCognitoAuthCredentialStore] Error getting all items from keychain under old access group, aborting migration")
249-
return
250-
}
251-
252-
if oldItems.isEmpty {
253-
log.verbose("[AWSCognitoAuthCredentialStore] No items in keychain under old access group, clearing keychain items under new access group")
254-
return
255-
}
256-
257-
for item in oldItems {
258-
do {
259-
try keychain._set(item.value, key: item.key)
260-
} catch {
261-
log.error("[AWSCognitoAuthCredentialStore] Error migrating one of the items, aborting migration: \(error)")
262-
try? clearAllCredentials()
263-
return
264-
}
265-
}
248+
let oldService = oldAccessGroup != nil ? sharedService : service
249+
let newService = accessGroup != nil ? sharedService : service
266250

267251
do {
268-
try oldKeychain._removeAll()
252+
try KeychainStoreMigrator(oldService: oldService, newService: newService, oldAccessGroup: oldAccessGroup, newAccessGroup: accessGroup).migrate()
269253
} catch {
270-
log.error("[AWSCognitoAuthCredentialStore] Error deleting all items from keychain under old access group after migration")
254+
log.error("[AWSCognitoAuthCredentialStore] Migration has failed")
255+
return
271256
}
272257

273258
log.verbose("[AWSCognitoAuthCredentialStore] Migration of keychain items from old access group to new access group successful")

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Task/AWSAuthFetchSessionTask.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ class AWSAuthFetchSessionTask: AuthFetchSessionTask, DefaultLogger {
2020
HubPayload.EventName.Auth.fetchSessionAPI
2121
}
2222

23-
init(_ request: AuthFetchSessionRequest, authStateMachine: AuthStateMachine, configuration: AuthConfiguration, forceReconfigure: Bool = false) {
23+
init(
24+
_ request: AuthFetchSessionRequest,
25+
authStateMachine: AuthStateMachine,
26+
configuration: AuthConfiguration,
27+
forceReconfigure: Bool = false
28+
) {
2429
self.request = request
2530
self.authStateMachine = authStateMachine
2631
self.fetchAuthSessionHelper = FetchAuthSessionOperationHelper()

AmplifyPlugins/Core/AWSPluginsCore/Keychain/KeychainStore.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,16 @@ public struct KeychainStore: KeychainStoreBehavior {
7575
}
7676

7777
public init(service: String) {
78-
attributes = KeychainStoreAttributes(service: service)
79-
log.verbose("[KeychainStore] Initialized keychain with service=\(service), attributes=\(attributes), accessGroup=")
78+
self.init(service: service, accessGroup: nil)
8079
}
8180

8281
public init(service: String, accessGroup: String? = nil) {
8382
attributes = KeychainStoreAttributes(service: service, accessGroup: accessGroup)
84-
log.verbose("[KeychainStore] Initialized keychain with service=\(service), attributes=\(attributes), accessGroup=\(attributes.accessGroup ?? "")")
83+
log.verbose(
84+
"[KeychainStore] Initialized keychain with service=\(service), " +
85+
"attributes=\(attributes), " +
86+
"accessGroup=\(attributes.accessGroup ?? "No access group specified")"
87+
)
8588
}
8689

8790
@_spi(KeychainStore)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import Foundation
9+
import Amplify
10+
11+
public struct KeychainStoreMigrator {
12+
let oldAttributes: KeychainStoreAttributes
13+
let newAttributes: KeychainStoreAttributes
14+
15+
public init(oldService: String, newService: String, oldAccessGroup: String?, newAccessGroup: String?) {
16+
self.oldAttributes = KeychainStoreAttributes(service: oldService, accessGroup: oldAccessGroup)
17+
self.newAttributes = KeychainStoreAttributes(service: newService, accessGroup: newAccessGroup)
18+
}
19+
20+
public func migrate() throws {
21+
log.verbose("[KeychainStoreMigrator] Starting to migrate items")
22+
23+
var updateQuery = oldAttributes.defaultGetQuery()
24+
25+
var updateAttributes = [String: Any]()
26+
updateAttributes[KeychainStore.Constants.AttributeService] = newAttributes.service
27+
updateAttributes[KeychainStore.Constants.AttributeAccessGroup] = newAttributes.accessGroup
28+
29+
// Remove any current items to avoid duplicate item error
30+
try? KeychainStore(service: newAttributes.service, accessGroup: newAttributes.accessGroup)._removeAll()
31+
32+
let updateStatus = SecItemUpdate(updateQuery as CFDictionary, updateAttributes as CFDictionary)
33+
switch updateStatus {
34+
case errSecSuccess:
35+
break
36+
case errSecItemNotFound:
37+
log.verbose("[KeychainStoreMigrator] No items to migrate, keychain under new access group is cleared")
38+
case errSecDuplicateItem:
39+
log.verbose("[KeychainStoreMigrator] Duplicate items found, could not migrate")
40+
return
41+
default:
42+
log.error("[KeychainStoreMigrator] Error of status=\(updateStatus) occurred when attempting to migrate items in keychain")
43+
throw KeychainStoreError.securityError(updateStatus)
44+
}
45+
46+
log.verbose("[KeychainStoreMigrator] Successfully migrated items to new service and access group")
47+
}
48+
}
49+
50+
extension KeychainStoreMigrator: DefaultLogger {
51+
public static var log: Logger {
52+
Amplify.Logging.logger(forNamespace: String(describing: self))
53+
}
54+
55+
public nonisolated var log: Logger { Self.log }
56+
}

0 commit comments

Comments
 (0)