-
Notifications
You must be signed in to change notification settings - Fork 198
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
base: main
Are you sure you want to change the base?
Conversation
API Breakage Report💔 Public API Breaking Change detected:Module: Amplify Module: AWSPluginsCore |
9473eb0
to
c5cb449
Compare
* Remove migrateKeychainItemsOfUserSession bool from SecureStoragePreferences
API Breakage Report💔 Public API Breaking Change detected:Module: AWSPluginsCore |
e98f334
to
8dd74ed
Compare
API Breakage Report💔 Public API Breaking Change detected:Module: AWSPluginsCore |
1 similar comment
API Breakage Report💔 Public API Breaking Change detected:Module: AWSPluginsCore |
AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/AWSCognitoAuthPlugin+Configure.swift
Outdated
Show resolved
Hide resolved
AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/AWSCognitoAuthPlugin.swift
Outdated
Show resolved
Hide resolved
AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Task/AWSAuthFetchSessionTask.swift
Outdated
Show resolved
Hide resolved
...gins/Auth/Sources/AWSCognitoAuthPlugin/CredentialStorage/AWSCognitoAuthCredentialStore.swift
Outdated
Show resolved
Hide resolved
...sts/AuthHostApp/AuthIntegrationTests/CredentialStore/CredentialStoreConfigurationTests.swift
Show resolved
Hide resolved
...sts/AuthHostApp/AuthIntegrationTests/CredentialStore/CredentialStoreConfigurationTests.swift
Show resolved
Hide resolved
AmplifyPlugins/Core/AWSPluginsCore/Keychain/KeychainStore.swift
Outdated
Show resolved
Hide resolved
AmplifyPlugins/Core/AWSPluginsCore/Keychain/KeychainStore.swift
Outdated
Show resolved
Hide resolved
API Breakage Report💔 Public API Breaking Change detected:Module: AWSPluginsCore |
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.
LGTM
import Foundation | ||
|
||
public struct AccessGroup { | ||
public let name: String? |
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)
/// 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)
}
/**
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)
}
}
@@ -25,14 +26,33 @@ struct AWSCognitoAuthCredentialStore { | |||
private var isKeychainConfiguredKey: String { | |||
"\(userDefaultsNameSpace).isKeychainConfigured" | |||
} | |||
private var accessGroupKey: String { |
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 you add a small comment explaining the usage of the accessGroupKey
like for isKeychainConfiguredKey
?
return userDefaults.string(forKey: accessGroupKey) | ||
} | ||
|
||
private func saveStoredAccessGroup() throws { |
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.
The method is not throwing, so can be renamed without throws.
private func saveStoredAccessGroup() throws { | |
private func saveStoredAccessGroup() { |
@@ -182,6 +202,62 @@ extension AWSCognitoAuthCredentialStore: AmplifyAuthCredentialStoreBehavior { | |||
private func clearAllCredentials() throws { | |||
try keychain._removeAll() | |||
} | |||
|
|||
private func retrieveStoredAccessGroup() throws -> String? { |
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.
The method is not throwing, so can be renamed without throws.
private func retrieveStoredAccessGroup() throws -> String? { | |
private func retrieveStoredAccessGroup() -> String? { |
let oldKeychain: KeychainStoreBehavior | ||
|
||
if oldAccessGroup == accessGroup { | ||
log.verbose("[AWSCognitoAuthCredentialStore] Stored access group is the same as current access group, aborting migration") |
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.
Rather than verbose, I think this log statement should be of type info
.
import Foundation | ||
import Amplify | ||
|
||
public struct AWSCognitoSecureStoragePreferences { |
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.
nit: doc comments.
@_spi(KeychainStore) | ||
/// Retrieves all key-value pairs in the keychain | ||
/// This System Programming Interface (SPI) may have breaking changes in future updates. | ||
func _getAll() throws -> [(key: String, value: Data)] |
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.
Why does this need to return a tuple rather than a dictionary? Shouldn't it be something like:
func _getAll() throws -> [(key: String, value: Data)] | |
func _getAll() throws -> [[String: Data]] |
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.
With KeychainStoreMigrator
in place, the _getAll() function is no longer used anywhere. I forgot to remove it. If we go back to using _getAll() by the end of the review I will update this to be a dictionary instead.
query[Constants.MatchLimit] = Constants.MatchLimitAll | ||
query[Constants.ReturnData] = kCFBooleanTrue | ||
query[Constants.ReturnAttributes] = kCFBooleanTrue | ||
query[Constants.ReturnRef] = kCFBooleanTrue |
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.
What does ReturnRef
exactly do and why do we need it?
// Remove any current items to avoid duplicate item error | ||
try? KeychainStore(service: newAttributes.service, accessGroup: newAttributes.accessGroup)._removeAll() |
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.
The usage of _removeAll
is really necessary here? It seems very dangerous, if for some reason this logic runs in a scenario where we don't want it to.
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.
I placed it here as any current items in the keychain of the access group we are migrating to will cause the batch update to fail. I could, instead, only call _removeAll when necessary (i.e only when a duplicate item error occurs). It just makes the code a little more nested. Let me know what you think.
extension KeychainStoreMigrator: DefaultLogger { | ||
public static var log: Logger { | ||
Amplify.Logging.logger(forNamespace: String(describing: self)) | ||
} | ||
|
||
public nonisolated var log: Logger { Self.log } | ||
} |
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.
Just double check, I don't think you need to define these methods again, just the following should be enough.
extension KeychainStoreMigrator: DefaultLogger { | |
public static var log: Logger { | |
Amplify.Logging.logger(forNamespace: String(describing: self)) | |
} | |
public nonisolated var log: Logger { Self.log } | |
} | |
extension KeychainStoreMigrator: DefaultLogger { } |
let keychainAccessGroupWatch = "W3DRXD72QU.com.amazon.aws.amplify.swift.AuthWatchAppShared" | ||
let keychainAccessGroupWatch2 = "W3DRXD72QU.com.amazon.aws.amplify.swift.AuthWatchAppShared2" |
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.
Does the team id need to be part of the group?
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.
Yes
guard awsCredential.areValid() else { | ||
log.verbose("[AWSCognitoAuthCredentialStore] Credentials found are not valid (expired) in old access group keychain, aborting migration") | ||
return | ||
} |
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.
I think this check might no be accurate, as the areValid
method doesn't validate the refresh token.. The refresh token might still be valid.. So I think we should skip this check, and if its not valid, the fetchAuthSession
will fail, which is acceptable I think.
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.
That makes sense. I'm okay with removing this part.
…check, only delete items if absolutely necessary
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3811 +/- ##
==========================================
- Coverage 68.54% 68.47% -0.08%
==========================================
Files 1080 1083 +3
Lines 37608 37695 +87
==========================================
+ Hits 25778 25810 +32
- Misses 11830 11885 +55
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Issue #
#2508
#3277
Description
This allows Amplify Swift developers to set the access group they would like the auth session to be shared on. This PR, as opposed to this PR, does not require the other app with which the auth session is being shared with to be reloaded. This helps immensely when writing a product with app extensions.
Changes made
AWSCognitoCredentialAuthCredentialStore
now uses access group to create keychain instance with said access group.If an access group is specified:
fetchAuthSession
reconfigures the plugin when called, so that app reload is not requiredMigration:
migrateKeychainItemsOfUserSession
totrue
within theaccessGroup
structUserDefaults
) to the new access groupUsage
Migrating to a Shared Keychain
To use a shared keychain:
To move to the shared keychain using this new keychain access group, specify the
accessGroup
parameter when instantiating theAWSCognitoAuthPlugin
. If a user is currently signed-in, they will be logged out when first using the access group:If you would prefer the user session to be migrated (which will allow the user to continue to be signed-in), then specify the
migrateKeychainItemsOfUserSession
boolean in theAccessGroup
struct to be true like so:Sign in a user with any sign-in method within one app that uses this access group. After reloading another app that uses this access group, the user will be signed in. Likewise, signing out of one app will sign out the other app after reloading it.
Migrating to another Shared Keychain
To move to a different access group, update the name parameter of the
AccessGroup
to be the new access group. SetmigrateKeychainItemsOfUserSession
totrue
to migrate an existing user session under the previously used access group.Migrating from a Shared Keychain
If you’d like to stop sharing state between this app and other apps, you can set the access group to be
AccessGroup.none
orAccessGroup.none(migrateKeychainItemsOfUserSession: true)
if you’d like the session to be migrated.General Checklist
Given When Then
inline code documentation and are named accordinglytestThing_condition_expectation()
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.