Skip to content

[Auth] Fixed macOS extension keychain access by adding recommended kS… #9102

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

Merged
merged 2 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions FirebaseAuth/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Unreleased
- [changed] Added a `X-Firebase-GMPID` header to network requests. (#9046)
- [fixed] Added multi-tenancy support to generic OAuth providers. (#7990)
- [fixed] macOS Extension access to Shared Keychain by adding kSecUseDataProtectionKeychain recommended key (#6876)

# 8.9.0
- [changed] Improved error logging. (#8704)
Expand Down
59 changes: 33 additions & 26 deletions FirebaseAuth/Sources/SystemService/FIRAuthStoredUserManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,9 @@ - (FIRUser *)getStoredUserForAccessGroup:(NSString *)accessGroup
shareAuthStateAcrossDevices:(BOOL)shareAuthStateAcrossDevices
projectIdentifier:(NSString *)projectIdentifier
error:(NSError *_Nullable *_Nullable)outError {
NSMutableDictionary *query = [[NSMutableDictionary alloc] init];
query[(__bridge id)kSecClass] = (__bridge id)kSecClassGenericPassword;

query[(__bridge id)kSecAttrAccessGroup] = accessGroup;
query[(__bridge id)kSecAttrService] = projectIdentifier;
query[(__bridge id)kSecAttrAccount] = kSharedKeychainAccountValue;
if (shareAuthStateAcrossDevices) {
query[(__bridge id)kSecAttrSynchronizable] = (__bridge id)kCFBooleanTrue;
}

NSMutableDictionary *query = [self keychainQueryForAccessGroup:accessGroup
shareAuthStateAcrossDevices:shareAuthStateAcrossDevices
projectIdentifier:projectIdentifier];
NSData *data = [self.keychainServices getItemWithQuery:query error:outError];
// If there's an outError parameter and it's populated, or there's no data, return.
if ((outError && *outError) || !data) {
Expand Down Expand Up @@ -113,22 +106,15 @@ - (BOOL)setStoredUser:(FIRUser *)user
shareAuthStateAcrossDevices:(BOOL)shareAuthStateAcrossDevices
projectIdentifier:(NSString *)projectIdentifier
error:(NSError *_Nullable *_Nullable)outError {
NSMutableDictionary *query = [[NSMutableDictionary alloc] init];
query[(__bridge id)kSecClass] = (__bridge id)kSecClassGenericPassword;
NSMutableDictionary *query = [self keychainQueryForAccessGroup:accessGroup
shareAuthStateAcrossDevices:shareAuthStateAcrossDevices
projectIdentifier:projectIdentifier];
if (shareAuthStateAcrossDevices) {
query[(__bridge id)kSecAttrAccessible] = (__bridge id)kSecAttrAccessibleAfterFirstUnlock;
} else {
query[(__bridge id)kSecAttrAccessible] =
(__bridge id)kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly;
}

query[(__bridge id)kSecAttrAccessGroup] = accessGroup;
query[(__bridge id)kSecAttrService] = projectIdentifier;
query[(__bridge id)kSecAttrAccount] = kSharedKeychainAccountValue;
if (shareAuthStateAcrossDevices) {
query[(__bridge id)kSecAttrSynchronizable] = (__bridge id)kCFBooleanTrue;
}

#if TARGET_OS_WATCH
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initRequiringSecureCoding:false];
#else
Expand All @@ -153,23 +139,44 @@ - (BOOL)removeStoredUserForAccessGroup:(NSString *)accessGroup
shareAuthStateAcrossDevices:(BOOL)shareAuthStateAcrossDevices
projectIdentifier:(NSString *)projectIdentifier
error:(NSError *_Nullable *_Nullable)outError {
NSMutableDictionary *query = [[NSMutableDictionary alloc] init];
query[(__bridge id)kSecClass] = (__bridge id)kSecClassGenericPassword;
NSMutableDictionary *query = [self keychainQueryForAccessGroup:accessGroup
shareAuthStateAcrossDevices:shareAuthStateAcrossDevices
projectIdentifier:projectIdentifier];
if (shareAuthStateAcrossDevices) {
query[(__bridge id)kSecAttrAccessible] = (__bridge id)kSecAttrAccessibleAfterFirstUnlock;
} else {
query[(__bridge id)kSecAttrAccessible] =
(__bridge id)kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly;
}
if (shareAuthStateAcrossDevices) {
query[(__bridge id)kSecAttrSynchronizable] = (__bridge id)kCFBooleanTrue;
}
return [self.keychainServices removeItemWithQuery:query error:outError];
}

#pragma mark - Internal Methods

- (NSMutableDictionary *)keychainQueryForAccessGroup:(NSString *)accessGroup
shareAuthStateAcrossDevices:(BOOL)shareAuthStateAcrossDevices
projectIdentifier:(NSString *)projectIdentifier {
NSMutableDictionary *query = [[NSMutableDictionary alloc] init];
query[(__bridge id)kSecClass] = (__bridge id)kSecClassGenericPassword;
query[(__bridge id)kSecAttrAccessGroup] = accessGroup;
query[(__bridge id)kSecAttrService] = projectIdentifier;
query[(__bridge id)kSecAttrAccount] = kSharedKeychainAccountValue;

return [self.keychainServices removeItemWithQuery:query error:outError];
if (@available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 6.0, *)) {
/*
"The data protection key affects operations only in macOS.
Other platforms automatically behave as if the key is set to true,
and ignore the key in the query dictionary. You can safely use the key on all platforms."
[kSecUseDataProtectionKeychain](https://developer.apple.com/documentation/security/ksecusedataprotectionkeychain)
*/
query[(__bridge id)kSecUseDataProtectionKeychain] = (__bridge id)kCFBooleanTrue;
}

if (shareAuthStateAcrossDevices) {
query[(__bridge id)kSecAttrSynchronizable] = (__bridge id)kCFBooleanTrue;
}

return query;
}

@end