Skip to content

Commit

Permalink
fix(auth): Migrate keychain to the current accessibility setting (aws…
Browse files Browse the repository at this point in the history
  • Loading branch information
royjit authored Jan 25, 2023
1 parent 09a7235 commit 1a882b3
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 3 deletions.
2 changes: 1 addition & 1 deletion AWSAuthSDK/Sources/AWSMobileClient/AWSMobileClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ final public class AWSMobileClient: _AWSMobileClient {
public func initialize(_ completionHandler: @escaping (UserState?, Error?) -> Void) {
// Read awsconfiguration.json and set the credentials provider here
initializationQueue.sync {

self.keychain.migrateToCurrentAccessibility()
if (isInitialized) {
completionHandler(self.currentUserState, nil)
return
Expand Down
1 change: 1 addition & 0 deletions AWSCognitoAuth/AWSCognitoAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ - (instancetype)initWithConfiguration:(AWSCognitoAuthConfiguration *)authConfigu
_useSFAuthenticationSession = authConfiguration.isSFAuthenticationSessionEnabled;
_sfAuthenticationSessionAvailable = NO;
_keychain = [AWSCognitoAuthUICKeyChainStore keyChainStoreWithService:[NSString stringWithFormat:@"%@.%@", [NSBundle mainBundle].bundleIdentifier, @"AWSCognitoIdentityUserPool"]]; //Consistent with AWSCognitoIdentityUserPool
[_keychain migrateToCurrentAccessibility];
}
return self;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0);
+ (void)requestSharedWebCredentialForDomain:(nullable NSString *)domain account:(nullable NSString *)account completion:(nullable void (^)(NSArray UIC_CREDENTIAL_TYPE *credentials, NSError * __nullable error))completion;

+ (NSString *)generatePassword;

/// Migrate the existing items in keychain to the current accessibility
///
/// Invoke this method if you have changed the keychain accessibility but there are already existing keychain items
/// stored with a different accessibility setting.
- (void)migrateToCurrentAccessibility;
#endif

@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,26 @@ + (NSArray *)prettify:(CFTypeRef)itemClass items:(NSArray *)items

#pragma mark -

- (void)migrateToCurrentAccessibility {
NSArray *items = [self allItems];
for (NSDictionary *item in items) {
CFComparisonResult result = CFStringCompare((CFStringRef)item[@"accessibility"],
[self accessibilityObject], 0);
if (result == kCFCompareEqualTo) {
continue;
}
NSString *key = item[@"key"];
NSObject *value = item[@"value"];
if ([value isKindOfClass:[NSString class]]) {
[self setString: (NSString *)value forKey:key];
} else if ([value isKindOfClass:[NSData class]]) {
[self setData: (NSData *)value forKey:key];
}
}
}

#pragma mark -

- (void)setSynchronizable:(BOOL)synchronizable
{
_synchronizable = synchronizable;
Expand Down
2 changes: 1 addition & 1 deletion AWSCognitoIdentityProvider/AWSCognitoIdentityUserPool.m
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ - (instancetype)initWithConfiguration:(AWSServiceConfiguration *)configuration
_userPoolConfiguration = userPoolConfiguration;

_keychain = [AWSUICKeyChainStore keyChainStoreWithService:[NSString stringWithFormat:@"%@.%@", [NSBundle mainBundle].bundleIdentifier, [AWSCognitoIdentityUserPool class]]];

[_keychain migrateToCurrentAccessibility];

//If Pinpoint is setup, get the endpoint or create one.
if(userPoolConfiguration.pinpointAppId) {
Expand Down
3 changes: 2 additions & 1 deletion AWSCore/Authentication/AWSCredentialsProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,8 @@ - (void)setUpWithRegionType:(AWSRegionType)regionType

// initialize keychain - name spaced by app bundle and identity pool id
_keychain = [AWSUICKeyChainStore keyChainStoreWithService:[NSString stringWithFormat:@"%@.%@.%@", [NSBundle mainBundle].bundleIdentifier, [AWSCognitoCredentialsProvider class], identityProvider.identityPoolId]];

[_keychain migrateToCurrentAccessibility];

// If the identity provider has an identity id, use it
if (identityProvider.identityId) {
_keychain[AWSCredentialsProviderKeychainIdentityId] = identityProvider.identityId;
Expand Down
7 changes: 7 additions & 0 deletions AWSCore/UICKeyChainStore/AWSUICKeyChainStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,15 @@ __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0);
+ (void)requestSharedWebCredentialForDomain:(nullable NSString *)domain account:(nullable NSString *)account completion:(nullable void (^)(NSArray UIC_CREDENTIAL_TYPE *credentials, NSError * __nullable error))completion;

+ (NSString *)generatePassword;

#endif

/// Migrate the existing items in keychain to the current accessibility
///
/// Invoke this method if you have changed the keychain accessibility but there are already existing keychain items
/// stored with a different accessibility setting.
- (void)migrateToCurrentAccessibility;

@end

@interface AWSUICKeyChainStore (ErrorHandling)
Expand Down
20 changes: 20 additions & 0 deletions AWSCore/UICKeyChainStore/AWSUICKeyChainStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,26 @@ + (NSArray *)prettify:(CFTypeRef)itemClass items:(NSArray *)items

#pragma mark -

- (void)migrateToCurrentAccessibility {
NSArray *items = [self allItems];
for (NSDictionary *item in items) {
CFComparisonResult result = CFStringCompare((CFStringRef)item[@"accessibility"],
[self accessibilityObject], 0);
if (result == kCFCompareEqualTo) {
continue;
}
NSString *key = item[@"key"];
NSObject *value = item[@"value"];
if ([value isKindOfClass: [NSString class]]) {
[self setString: (NSString *)value forKey:key];
} else if ([value isKindOfClass: [NSData class]]) {
[self setData: (NSData *)value forKey:key];
}
}
}

#pragma mark -

- (void)setSynchronizable:(BOOL)synchronizable
{
_synchronizable = synchronizable;
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

### Misc. Updates

- **Auth**
- Added migration of keychain to the current accessibility level set for different Auth SDK. This will enabled keychain items that are stored in different accessibility level to get fixed to the current accessibility. (See [PR #4516](https://github.com/aws-amplify/aws-sdk-ios/pull/4516))


- Model updates for the following services
-AWSCloudWatchLogs
-AWSConnect
Expand Down

0 comments on commit 1a882b3

Please sign in to comment.