Skip to content

Commit

Permalink
Always log if error occurred
Browse files Browse the repository at this point in the history
  • Loading branch information
kishikawakatsumi committed Jan 14, 2015
1 parent 9e4f547 commit ed99550
Showing 1 changed file with 52 additions and 24 deletions.
76 changes: 52 additions & 24 deletions Lib/UICKeyChainStore/UICKeyChainStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ + (NSString *)stringForKey:(NSString *)key service:(NSString *)service accessGro
+ (NSString *)stringForKey:(NSString *)key service:(NSString *)service accessGroup:(NSString *)accessGroup error:(NSError *__autoreleasing *)error
{
if (!key) {
NSError *e = [self argumentError:NSLocalizedString(@"the key must not to be nil", nil)];
if (error) {
*error = [self argumentError:NSLocalizedString(@"the key must not to be nil", nil)];
*error = e;
}
return nil;
}
Expand Down Expand Up @@ -198,8 +199,9 @@ + (BOOL)setString:(NSString *)value forKey:(NSString *)key service:(NSString *)s
if (data) {
return [self setData:data forKey:key service:service accessGroup:accessGroup error:error];
}
NSError *e = [self conversionError:NSLocalizedString(@"failed to convert string to data", nil)];
if (error) {
*error = [self conversionError:NSLocalizedString(@"failed to convert string to data", nil)];
*error = e;
}
return NO;
}
Expand Down Expand Up @@ -234,8 +236,9 @@ + (NSData *)dataForKey:(NSString *)key service:(NSString *)service accessGroup:(
+ (NSData *)dataForKey:(NSString *)key service:(NSString *)service accessGroup:(NSString *)accessGroup error:(NSError *__autoreleasing *)error
{
if (!key) {
NSError *e = [self argumentError:NSLocalizedString(@"the key must not to be nil", nil)];
if (error) {
*error = [self argumentError:NSLocalizedString(@"the key must not to be nil", nil)];
*error = e;
}
return nil;
}
Expand Down Expand Up @@ -277,8 +280,9 @@ + (BOOL)setData:(NSData *)data forKey:(NSString *)key service:(NSString *)servic
+ (BOOL)setData:(NSData *)data forKey:(NSString *)key service:(NSString *)service accessGroup:(NSString *)accessGroup error:(NSError *__autoreleasing *)error
{
if (!key) {
NSError *e = [self argumentError:NSLocalizedString(@"the key must not to be nil", nil)];
if (error) {
*error = [self argumentError:NSLocalizedString(@"the key must not to be nil", nil)];
*error = e;
}
return NO;
}
Expand Down Expand Up @@ -316,8 +320,9 @@ - (NSString *)stringForKey:(id)key error:(NSError *__autoreleasing *)error
if (string) {
return string;
}
NSError *e = [self.class conversionError:NSLocalizedString(@"failed to convert data to string", nil)];
if (error) {
*error = [self.class conversionError:NSLocalizedString(@"failed to convert data to string", nil)];
*error = e;
}
return nil;
}
Expand Down Expand Up @@ -351,8 +356,9 @@ - (BOOL)setString:(NSString *)string forKey:(NSString *)key label:(NSString *)la
if (data) {
return [self setData:data forKey:key label:label comment:comment error:error];
}
NSError *e = [self.class conversionError:NSLocalizedString(@"failed to convert string to data", nil)];
if (error) {
*error = [self.class conversionError:NSLocalizedString(@"failed to convert string to data", nil)];
*error = e;
}
return NO;
}
Expand Down Expand Up @@ -381,17 +387,19 @@ - (NSData *)dataForKey:(NSString *)key error:(NSError *__autoreleasing *)error
CFRelease(data);
return ret;
} else {
NSError *e = [self.class unexpectedError:NSLocalizedString(@"Unexpected error has occurred.", nil)];
if (error) {
*error = [self.class unexpectedError:NSLocalizedString(@"Unexpected error has occurred.", nil)];
*error = e;
}
return nil;
}
} else if (status == errSecItemNotFound) {
return nil;
}

NSError *e = [self.class securityError:status];
if (error) {
*error = [self.class securityError:status];
*error = e;
}
return nil;
}
Expand All @@ -416,8 +424,9 @@ - (BOOL)setData:(NSData *)data forKey:(NSString *)key label:(NSString *)label co
- (BOOL)setData:(NSData *)data forKey:(NSString *)key label:(NSString *)label comment:(NSString *)comment error:(NSError *__autoreleasing *)error
{
if (!key) {
NSError *e = [self.class argumentError:NSLocalizedString(@"the key must not to be nil", nil)];
if (error) {
*error = [self.class argumentError:NSLocalizedString(@"the key must not to be nil", nil)];
*error = e;
}
return NO;
}
Expand All @@ -435,11 +444,8 @@ - (BOOL)setData:(NSData *)data forKey:(NSString *)key label:(NSString *)label co

OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)query, NULL);
if (status == errSecSuccess || status == errSecInteractionNotAllowed) {
#if TARGET_OS_IPHONE
if (floor(NSFoundationVersionNumber) > floor(1047.25)) { // iOS 8+
[query removeObjectForKey:(__bridge id)kSecUseNoAuthenticationUI];
}
#endif
query = [self query];

NSError *unexpectedError = nil;
NSMutableDictionary *attributes = [self attributesWithKey:nil value:data error:&unexpectedError];

Expand All @@ -451,6 +457,7 @@ - (BOOL)setData:(NSData *)data forKey:(NSString *)key label:(NSString *)label co
}

if (unexpectedError) {
NSLog(@"error: [%@] %@", @(unexpectedError.code), NSLocalizedString(@"Unexpected error has occurred.", nil));
if (error) {
*error = unexpectedError;
}
Expand All @@ -465,8 +472,9 @@ - (BOOL)setData:(NSData *)data forKey:(NSString *)key label:(NSString *)label co
status = SecItemUpdate((__bridge CFDictionaryRef)query, (__bridge CFDictionaryRef)attributes);
}
if (status != errSecSuccess) {
NSError *e = [self.class securityError:status];
if (error) {
*error = [self.class securityError:status];
*error = e;
}
return NO;
}
Expand All @@ -483,22 +491,25 @@ - (BOOL)setData:(NSData *)data forKey:(NSString *)key label:(NSString *)label co
}

if (unexpectedError) {
NSLog(@"error: [%@] %@", @(unexpectedError.code), NSLocalizedString(@"Unexpected error has occurred.", nil));
if (error) {
*error = unexpectedError;
}
return NO;
} else {
status = SecItemAdd((__bridge CFDictionaryRef)attributes, NULL);
if (status != errSecSuccess) {
NSError *e = [self.class securityError:status];
if (error) {
*error = [self.class securityError:status];
*error = e;
}
return NO;
}
}
} else {
NSError *e = [self.class securityError:status];
if (error) {
*error = [self.class securityError:status];
*error = e;
}
return NO;
}
Expand Down Expand Up @@ -536,8 +547,9 @@ + (BOOL)removeItemForKey:(NSString *)key service:(NSString *)service accessGroup
+ (BOOL)removeItemForKey:(NSString *)key service:(NSString *)service accessGroup:(NSString *)accessGroup error:(NSError *__autoreleasing *)error
{
if (!key) {
NSError *e = [self.class argumentError:NSLocalizedString(@"the key must not to be nil", nil)];
if (error) {
*error = [self.class argumentError:NSLocalizedString(@"the key must not to be nil", nil)];
*error = e;
}
return NO;
}
Expand Down Expand Up @@ -596,8 +608,9 @@ - (BOOL)removeItemForKey:(NSString *)key error:(NSError *__autoreleasing *)error

OSStatus status = SecItemDelete((__bridge CFDictionaryRef)query);
if (status != errSecSuccess && status != errSecItemNotFound) {
NSError *e = [self.class securityError:status];
if (error) {
*error = [self.class securityError:status];
*error = e;
}
return NO;
}
Expand All @@ -621,8 +634,9 @@ - (BOOL)removeAllItemsWithError:(NSError *__autoreleasing *)error

OSStatus status = SecItemDelete((__bridge CFDictionaryRef)query);
if (status != errSecSuccess && status != errSecItemNotFound) {
NSError *e = [self.class securityError:status];
if (error) {
*error = [self.class securityError:status];
*error = e;
}
return NO;
}
Expand Down Expand Up @@ -808,10 +822,21 @@ + (NSArray *)prettify:(CFTypeRef)itemClass items:(NSArray *)items

#pragma mark -

- (void)setSynchronizable:(BOOL)synchronizable
{
_synchronizable = synchronizable;
if (_authenticationPolicy) {
NSLog(@"%@", @"Cannot specify both an authenticationPolicy and a synchronizable");
}
}

- (void)setAccessibility:(UICKeyChainStoreAccessibility)accessibility authenticationPolicy:(UICKeyChainStoreAuthenticationPolicy)authenticationPolicy
{
_accessibility = accessibility;
_authenticationPolicy = authenticationPolicy;
if (_synchronizable) {
NSLog(@"%@", @"Cannot specify both an authenticationPolicy and a synchronizable");
}
}

#pragma mark -
Expand Down Expand Up @@ -919,17 +944,20 @@ - (NSMutableDictionary *)attributesWithKey:(NSString *)key value:(NSData *)value
CFErrorRef securityError = NULL;
SecAccessControlRef accessControl = SecAccessControlCreateWithFlags(kCFAllocatorDefault, accessibilityObject, (SecAccessControlCreateFlags)_authenticationPolicy, &securityError);
if (securityError) {
NSError *e = (__bridge NSError *)securityError;
NSLog(@"error: [%@] %@", @(e.code), e.localizedDescription);
if (error) {
*error = (__bridge NSError *)securityError;
*error = e;
return nil;
}
}
if (!accessControl) {
NSString *message = NSLocalizedString(@"Unexpected error has occurred.", nil);
NSError *e = [self.class unexpectedError:message];
if (error) {
NSString *message = @"Unexpected error has occurred.";
*error = [self.class unexpectedError:message];
return nil;
*error = e;
}
return nil;
}
attributes[(__bridge __strong id)kSecAttrAccessControl] = (__bridge id)accessControl;
} else {
Expand Down

0 comments on commit ed99550

Please sign in to comment.