-
Notifications
You must be signed in to change notification settings - Fork 200
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
feat(auth): Keychain Sharing (No App Reload Required) #3811
Open
yaroluchko
wants to merge
7
commits into
main
Choose a base branch
from
yaluchko/auth-keychain-sharing-no-reload
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
09f0583
feat(Auth) Keychain Sharing (App Reload Required)
yaroluchko 8dd74ed
Reconfigure when fetching auth session if sharing keychain
yaroluchko 3dfa43b
Update API dumps for new version
aws-amplify-ops f5c01b1
Indentation, clean up, and batch migration to avoid inconsistent state
yaroluchko a999a54
Update API dumps for new version
aws-amplify-ops 26fb82c
Addressing review comments: documentation, no more credentials valid …
yaroluchko 01e2268
Style fixes
yaroluchko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
|
||
/// A structure representing an access group for managing keychain items. | ||
public struct AccessGroup { | ||
/// The name of the access group. | ||
public let name: String? | ||
|
||
/// A flag indicating whether to migrate keychain items. | ||
public let migrateKeychainItems: Bool | ||
|
||
/** | ||
Initializes an `AccessGroup` with the specified name and migration option. | ||
|
||
- Parameter name: The name of the access group. | ||
- Parameter migrateKeychainItemsOfUserSession: A flag indicating whether to migrate keychain items. Defaults to `false`. | ||
*/ | ||
public init(name: String, migrateKeychainItemsOfUserSession: Bool = false) { | ||
self.init(name: name, migrateKeychainItems: migrateKeychainItemsOfUserSession) | ||
thisisabhash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
Creates an `AccessGroup` instance with no specified name. | ||
|
||
- Parameter migrateKeychainItemsOfUserSession: A flag indicating whether to migrate keychain items. | ||
- Returns: An `AccessGroup` instance with the migration option set. | ||
*/ | ||
public static func none(migrateKeychainItemsOfUserSession: Bool) -> AccessGroup { | ||
return .init(migrateKeychainItems: migrateKeychainItemsOfUserSession) | ||
} | ||
|
||
/** | ||
A static property representing an `AccessGroup` with no name and no migration. | ||
|
||
- Returns: An `AccessGroup` instance with no name and the migration option set to `false`. | ||
*/ | ||
public static var none: AccessGroup { | ||
return .none(migrateKeychainItemsOfUserSession: false) | ||
} | ||
|
||
private init(name: String? = nil, migrateKeychainItems: Bool) { | ||
self.name = name | ||
self.migrateKeychainItems = migrateKeychainItems | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ struct AWSCognitoAuthCredentialStore { | |
|
||
// Credential store constants | ||
private let service = "com.amplify.awsCognitoAuthPlugin" | ||
private let sharedService = "com.amplify.awsCognitoAuthPluginShared" | ||
private let sessionKey = "session" | ||
private let deviceMetadataKey = "deviceMetadata" | ||
private let deviceASFKey = "deviceASF" | ||
|
@@ -25,14 +26,40 @@ struct AWSCognitoAuthCredentialStore { | |
private var isKeychainConfiguredKey: String { | ||
"\(userDefaultsNameSpace).isKeychainConfigured" | ||
} | ||
/// This UserDefaults Key is use to retrieve the stored access group to determine | ||
/// which access group the migration should happen from | ||
/// If none is found, the unshared service is used for migration and all items | ||
/// under that service are queried | ||
private var accessGroupKey: String { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a small comment explaining the usage of the |
||
"\(userDefaultsNameSpace).accessGroup" | ||
} | ||
|
||
private let authConfiguration: AuthConfiguration | ||
private let keychain: KeychainStoreBehavior | ||
private let userDefaults = UserDefaults.standard | ||
private let accessGroup: String? | ||
|
||
init(authConfiguration: AuthConfiguration, accessGroup: String? = nil) { | ||
init( | ||
authConfiguration: AuthConfiguration, | ||
accessGroup: String? = nil, | ||
migrateKeychainItemsOfUserSession: Bool = false | ||
) { | ||
self.authConfiguration = authConfiguration | ||
self.keychain = KeychainStore(service: service, accessGroup: accessGroup) | ||
self.accessGroup = accessGroup | ||
if let accessGroup { | ||
self.keychain = KeychainStore(service: sharedService, accessGroup: accessGroup) | ||
} else { | ||
self.keychain = KeychainStore(service: service) | ||
} | ||
|
||
let oldAccessGroup = retrieveStoredAccessGroup() | ||
if migrateKeychainItemsOfUserSession { | ||
try? migrateKeychainItemsToAccessGroup() | ||
} else if oldAccessGroup == nil && oldAccessGroup != accessGroup { | ||
try? KeychainStore(service: service)._removeAll() | ||
} | ||
|
||
saveStoredAccessGroup() | ||
|
||
if !userDefaults.bool(forKey: isKeychainConfiguredKey) { | ||
try? clearAllCredentials() | ||
|
@@ -182,6 +209,39 @@ extension AWSCognitoAuthCredentialStore: AmplifyAuthCredentialStoreBehavior { | |
private func clearAllCredentials() throws { | ||
try keychain._removeAll() | ||
} | ||
|
||
private func retrieveStoredAccessGroup() -> String? { | ||
return userDefaults.string(forKey: accessGroupKey) | ||
} | ||
|
||
private func saveStoredAccessGroup() { | ||
if let accessGroup { | ||
userDefaults.set(accessGroup, forKey: accessGroupKey) | ||
} else { | ||
userDefaults.removeObject(forKey: accessGroupKey) | ||
} | ||
} | ||
|
||
private func migrateKeychainItemsToAccessGroup() throws { | ||
let oldAccessGroup = retrieveStoredAccessGroup() | ||
|
||
if oldAccessGroup == accessGroup { | ||
log.info("[AWSCognitoAuthCredentialStore] Stored access group is the same as current access group, aborting migration") | ||
return | ||
} | ||
|
||
let oldService = oldAccessGroup != nil ? sharedService : service | ||
let newService = accessGroup != nil ? sharedService : service | ||
|
||
do { | ||
try KeychainStoreMigrator(oldService: oldService, newService: newService, oldAccessGroup: oldAccessGroup, newAccessGroup: accessGroup).migrate() | ||
} catch { | ||
log.error("[AWSCognitoAuthCredentialStore] Migration has failed") | ||
return | ||
} | ||
|
||
log.verbose("[AWSCognitoAuthCredentialStore] Migration of keychain items from old access group to new access group successful") | ||
} | ||
|
||
} | ||
|
||
|
@@ -205,3 +265,5 @@ private extension AWSCognitoAuthCredentialStore { | |
} | ||
|
||
} | ||
|
||
extension AWSCognitoAuthCredentialStore: DefaultLogger { } |
23 changes: 23 additions & 0 deletions
23
...Plugins/Auth/Sources/AWSCognitoAuthPlugin/Models/AWSCognitoSecureStoragePreferences.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
import Amplify | ||
|
||
/// A struct to store preferences for how the plugin uses storage | ||
public struct AWSCognitoSecureStoragePreferences { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: doc comments. |
||
|
||
/// The access group that the keychain will use for auth items | ||
public let accessGroup: AccessGroup? | ||
|
||
/// Creates an intstance of AWSCognitoSecureStoragePreferences | ||
/// - Parameters: | ||
/// - accessGroup: access group to be used | ||
public init(accessGroup: AccessGroup? = nil) { | ||
self.accessGroup = accessGroup | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add doc comments explaining what is the intent of the variable, this comment applies to anything that is being made public here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Example:
(Its just an example, please make modifications as you feel are correct)