From 78c4ba2a4350396591550c1c649d46297233332e Mon Sep 17 00:00:00 2001 From: Julio Andres Date: Thu, 23 Jun 2016 19:01:57 -0700 Subject: [PATCH 1/7] Made the objectForKey return an NSError when available --- FXKeychain/FXKeychain.h | 2 +- FXKeychain/FXKeychain.m | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/FXKeychain/FXKeychain.h b/FXKeychain/FXKeychain.h index f7cce69..1f7adce 100644 --- a/FXKeychain/FXKeychain.h +++ b/FXKeychain/FXKeychain.h @@ -78,7 +78,7 @@ typedef NS_ENUM(NSInteger, FXKeychainAccess) - (BOOL)setObject:(nullable id)object forKey:(nonnull id)key; - (BOOL)setObject:(nullable id)object forKeyedSubscript:(nonnull id)key; - (BOOL)removeObjectForKey:(nonnull id)key; -- (nullable id)objectForKey:(nonnull id)key; +- (nullable id)objectForKey:(nonnull id)key error:(NSError * _Nullable * _Nullable) error; - (nullable id)objectForKeyedSubscript:(nonnull id)key; @end diff --git a/FXKeychain/FXKeychain.m b/FXKeychain/FXKeychain.m index 60b76b4..e1652a8 100644 --- a/FXKeychain/FXKeychain.m +++ b/FXKeychain/FXKeychain.m @@ -286,13 +286,12 @@ - (BOOL)removeObjectForKey:(id)key return [self setObject:nil forKey:key]; } -- (id)objectForKey:(id)key +- (id)objectForKey:(id)key error:(NSError**) error { NSData *data = [self dataForKey:key]; if (data) { id object = nil; - NSError *error = nil; NSPropertyListFormat format = NSPropertyListBinaryFormat_v1_0; //check if data is a binary plist @@ -302,7 +301,7 @@ - (id)objectForKey:(id)key object = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:&format - error:&error]; + error:error]; if ([object respondsToSelector:@selector(objectForKey:)] && [(NSDictionary *)object objectForKey:@"$archiver"]) { @@ -326,7 +325,7 @@ - (id)objectForKey:(id)key } if (!object) { - NSLog(@"FXKeychain failed to decode data for key '%@', error: %@", key, error); + NSLog(@"FXKeychain failed to decode data for key '%@', error: %@", key, *error); } return object; } @@ -339,7 +338,7 @@ - (id)objectForKey:(id)key - (id)objectForKeyedSubscript:(id)key { - return [self objectForKey:key]; + return [self objectForKey:key error:nil]; } @end From b625e973f81060598755fe3603ad8aec1bcad466 Mon Sep 17 00:00:00 2001 From: Julio Andres Date: Thu, 23 Jun 2016 19:37:06 -0700 Subject: [PATCH 2/7] Added more error out vars on set and remove --- FXKeychain/FXKeychain.h | 4 ++-- FXKeychain/FXKeychain.m | 15 +++++++-------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/FXKeychain/FXKeychain.h b/FXKeychain/FXKeychain.h index 1f7adce..91aff19 100644 --- a/FXKeychain/FXKeychain.h +++ b/FXKeychain/FXKeychain.h @@ -75,9 +75,9 @@ typedef NS_ENUM(NSInteger, FXKeychainAccess) - (nonnull id)initWithService:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup; -- (BOOL)setObject:(nullable id)object forKey:(nonnull id)key; +- (BOOL)setObject:(nullable id)object forKey:(nonnull id)key error:(NSError * _Nullable * _Nullable) error; - (BOOL)setObject:(nullable id)object forKeyedSubscript:(nonnull id)key; -- (BOOL)removeObjectForKey:(nonnull id)key; +- (BOOL)removeObjectForKey:(nonnull id)key error:(NSError * _Nullable * _Nullable) error; - (nullable id)objectForKey:(nonnull id)key error:(NSError * _Nullable * _Nullable) error; - (nullable id)objectForKeyedSubscript:(nonnull id)key; diff --git a/FXKeychain/FXKeychain.m b/FXKeychain/FXKeychain.m index e1652a8..8f28bfd 100644 --- a/FXKeychain/FXKeychain.m +++ b/FXKeychain/FXKeychain.m @@ -163,7 +163,7 @@ - (NSData *)dataForKey:(id)key return CFBridgingRelease(data); } -- (BOOL)setObject:(id)object forKey:(id)key +- (BOOL)setObject:(id)object forKey:(id)key error:(NSError**) error { //generate query NSMutableDictionary *query = [NSMutableDictionary dictionary]; @@ -179,7 +179,6 @@ - (BOOL)setObject:(id)object forKey:(id)key //encode object NSData *data = nil; - NSError *error = nil; if ([(id)object isKindOfClass:[NSString class]]) { //check that string data does not represent a binary plist @@ -187,7 +186,7 @@ - (BOOL)setObject:(id)object forKey:(id)key if (![object hasPrefix:@"bplist"] || ![NSPropertyListSerialization propertyListWithData:[object dataUsingEncoding:NSUTF8StringEncoding] options:NSPropertyListImmutable format:&format - error:NULL]) + error:error]) { //safe to encode as a string data = [object dataUsingEncoding:NSUTF8StringEncoding]; @@ -200,7 +199,7 @@ - (BOOL)setObject:(id)object forKey:(id)key data = [NSPropertyListSerialization dataWithPropertyList:[object FXKeychain_propertyListRepresentation] format:NSPropertyListBinaryFormat_v1_0 options:0 - error:&error]; + error:error]; #if FXKEYCHAIN_USE_NSCODING //property list encoding failed. try NSCoding @@ -214,7 +213,7 @@ - (BOOL)setObject:(id)object forKey:(id)key } //fail if object is invalid - NSAssert(!object || (object && data), @"FXKeychain failed to encode object for key '%@', error: %@", key, error); + NSAssert(!object || (object && data), @"FXKeychain failed to encode object for key '%@', error: %@", key, *error); if (data) { @@ -278,12 +277,12 @@ - (BOOL)setObject:(id)object forKey:(id)key - (BOOL)setObject:(id)object forKeyedSubscript:(id)key { - return [self setObject:object forKey:key]; + return [self setObject:object forKey:key error:nil]; } -- (BOOL)removeObjectForKey:(id)key +- (BOOL)removeObjectForKey:(id)key error:(NSError **)error { - return [self setObject:nil forKey:key]; + return [self setObject:nil forKey:key error:error]; } - (id)objectForKey:(id)key error:(NSError**) error From d951e5f2a948059a1018999105704dd8a9dd15ac Mon Sep 17 00:00:00 2001 From: Julio Andres Date: Thu, 23 Jun 2016 23:30:56 -0700 Subject: [PATCH 3/7] Added "overloaded" methods to maintain backwards compatibility for methods without NSErrors --- FXKeychain/FXKeychain.h | 3 +++ FXKeychain/FXKeychain.m | 19 +++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/FXKeychain/FXKeychain.h b/FXKeychain/FXKeychain.h index 91aff19..6d27b9b 100644 --- a/FXKeychain/FXKeychain.h +++ b/FXKeychain/FXKeychain.h @@ -75,9 +75,12 @@ typedef NS_ENUM(NSInteger, FXKeychainAccess) - (nonnull id)initWithService:(nullable NSString *)service accessGroup:(nullable NSString *)accessGroup; +- (BOOL)setObject:(nullable id)object forKey:(nonnull id)key; - (BOOL)setObject:(nullable id)object forKey:(nonnull id)key error:(NSError * _Nullable * _Nullable) error; - (BOOL)setObject:(nullable id)object forKeyedSubscript:(nonnull id)key; +- (BOOL)removeObjectForKey:(nonnull id)key; - (BOOL)removeObjectForKey:(nonnull id)key error:(NSError * _Nullable * _Nullable) error; +- (nullable id)objectForKey:(nonnull id)key; - (nullable id)objectForKey:(nonnull id)key error:(NSError * _Nullable * _Nullable) error; - (nullable id)objectForKeyedSubscript:(nonnull id)key; diff --git a/FXKeychain/FXKeychain.m b/FXKeychain/FXKeychain.m index 8f28bfd..db6c365 100644 --- a/FXKeychain/FXKeychain.m +++ b/FXKeychain/FXKeychain.m @@ -163,6 +163,11 @@ - (NSData *)dataForKey:(id)key return CFBridgingRelease(data); } +- (BOOL)setObject:(id)object forKey:(id)key +{ + return [self setObject:object forKey:key error:nil]; +} + - (BOOL)setObject:(id)object forKey:(id)key error:(NSError**) error { //generate query @@ -277,7 +282,12 @@ - (BOOL)setObject:(id)object forKey:(id)key error:(NSError**) error - (BOOL)setObject:(id)object forKeyedSubscript:(id)key { - return [self setObject:object forKey:key error:nil]; + return [self setObject:object forKey:key]; +} + +- (BOOL)removeObjectForKey:(id)key +{ + return [self removeObjectForKey:key error:nil]; } - (BOOL)removeObjectForKey:(id)key error:(NSError **)error @@ -285,6 +295,11 @@ - (BOOL)removeObjectForKey:(id)key error:(NSError **)error return [self setObject:nil forKey:key error:error]; } +- (id)objectForKey:(id)key +{ + return [self objectForKey:key error:nil]; +} + - (id)objectForKey:(id)key error:(NSError**) error { NSData *data = [self dataForKey:key]; @@ -337,7 +352,7 @@ - (id)objectForKey:(id)key error:(NSError**) error - (id)objectForKeyedSubscript:(id)key { - return [self objectForKey:key error:nil]; + return [self objectForKey:key]; } @end From 56f87b88c7c0d52d5da395ab57c3e972ec7547d0 Mon Sep 17 00:00:00 2001 From: Nikola Lajic Date: Fri, 31 Mar 2017 15:39:05 +0200 Subject: [PATCH 4/7] Errors are created when keychain operations fail --- FXKeychain/FXKeychain.h | 1 + FXKeychain/FXKeychain.m | 16 ++++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/FXKeychain/FXKeychain.h b/FXKeychain/FXKeychain.h index 6d27b9b..f4cc8c7 100644 --- a/FXKeychain/FXKeychain.h +++ b/FXKeychain/FXKeychain.h @@ -59,6 +59,7 @@ typedef NS_ENUM(NSInteger, FXKeychainAccess) FXKeychainAccessibleAlwaysThisDeviceOnly }; +extern NSString * _Nonnull const kFXKeychainErrorDomain; @interface FXKeychain : NSObject diff --git a/FXKeychain/FXKeychain.m b/FXKeychain/FXKeychain.m index db6c365..d8765b6 100644 --- a/FXKeychain/FXKeychain.m +++ b/FXKeychain/FXKeychain.m @@ -39,6 +39,7 @@ #error This class requires automatic reference counting #endif +NSString * const kFXKeychainErrorDomain = @"FXKeychainErrorDomain"; @implementation NSObject (FXKeychainPropertyListCoding) @@ -137,7 +138,7 @@ - (id)initWithService:(NSString *)service return self; } -- (NSData *)dataForKey:(id)key +- (NSData *)dataForKey:(id)key error:(NSError **)error { //generate query NSMutableDictionary *query = [NSMutableDictionary dictionary]; @@ -159,6 +160,7 @@ - (NSData *)dataForKey:(id)key if (status != errSecSuccess && status != errSecItemNotFound) { NSLog(@"FXKeychain failed to retrieve data for key '%@', error: %ld", key, (long)status); + *error = [NSError errorWithDomain:kFXKeychainErrorDomain code:status userInfo:nil]; } return CFBridgingRelease(data); } @@ -168,7 +170,7 @@ - (BOOL)setObject:(id)object forKey:(id)key return [self setObject:object forKey:key error:nil]; } -- (BOOL)setObject:(id)object forKey:(id)key error:(NSError**) error +- (BOOL)setObject:(id)object forKey:(id)key error:(NSError **)error { //generate query NSMutableDictionary *query = [NSMutableDictionary dictionary]; @@ -237,7 +239,7 @@ - (BOOL)setObject:(id)object forKey:(id)key error:(NSError**) error //write data OSStatus status = errSecSuccess; - if ([self dataForKey:key]) + if ([self dataForKey:key error:error]) { //there's already existing data for this key, update it status = SecItemUpdate((__bridge CFDictionaryRef)query, (__bridge CFDictionaryRef)update); @@ -251,6 +253,7 @@ - (BOOL)setObject:(id)object forKey:(id)key error:(NSError**) error if (status != errSecSuccess) { NSLog(@"FXKeychain failed to store data for key '%@', error: %ld", key, (long)status); + *error = [NSError errorWithDomain:kFXKeychainErrorDomain code:status userInfo:nil]; return NO; } } @@ -274,6 +277,7 @@ - (BOOL)setObject:(id)object forKey:(id)key error:(NSError**) error if (status != errSecSuccess) { NSLog(@"FXKeychain failed to delete data for key '%@', error: %ld", key, (long)status); + *error = [NSError errorWithDomain:kFXKeychainErrorDomain code:status userInfo:nil]; return NO; } } @@ -300,9 +304,9 @@ - (id)objectForKey:(id)key return [self objectForKey:key error:nil]; } -- (id)objectForKey:(id)key error:(NSError**) error +- (id)objectForKey:(id)key error:(NSError **) error { - NSData *data = [self dataForKey:key]; + NSData *data = [self dataForKey:key error:error]; if (data) { id object = nil; @@ -339,7 +343,7 @@ - (id)objectForKey:(id)key error:(NSError**) error } if (!object) { - NSLog(@"FXKeychain failed to decode data for key '%@', error: %@", key, *error); + NSLog(@"FXKeychain failed to decode data for key '%@', error: %@", key, *error); } return object; } From 0b7151d8d4ab1d748ff52aad7ea4e046c02893b7 Mon Sep 17 00:00:00 2001 From: MrNickBarker Date: Mon, 22 May 2017 11:39:54 +0200 Subject: [PATCH 5/7] Check for pointer before trying to write error --- FXKeychain/FXKeychain.m | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/FXKeychain/FXKeychain.m b/FXKeychain/FXKeychain.m index d8765b6..0a3f405 100644 --- a/FXKeychain/FXKeychain.m +++ b/FXKeychain/FXKeychain.m @@ -160,7 +160,10 @@ - (NSData *)dataForKey:(id)key error:(NSError **)error if (status != errSecSuccess && status != errSecItemNotFound) { NSLog(@"FXKeychain failed to retrieve data for key '%@', error: %ld", key, (long)status); - *error = [NSError errorWithDomain:kFXKeychainErrorDomain code:status userInfo:nil]; + if (error != NULL) + { + *error = [NSError errorWithDomain:kFXKeychainErrorDomain code:status userInfo:nil]; + } } return CFBridgingRelease(data); } @@ -253,7 +256,10 @@ - (BOOL)setObject:(id)object forKey:(id)key error:(NSError **)error if (status != errSecSuccess) { NSLog(@"FXKeychain failed to store data for key '%@', error: %ld", key, (long)status); - *error = [NSError errorWithDomain:kFXKeychainErrorDomain code:status userInfo:nil]; + if (error != NULL) + { + *error = [NSError errorWithDomain:kFXKeychainErrorDomain code:status userInfo:nil]; + } return NO; } } @@ -277,7 +283,10 @@ - (BOOL)setObject:(id)object forKey:(id)key error:(NSError **)error if (status != errSecSuccess) { NSLog(@"FXKeychain failed to delete data for key '%@', error: %ld", key, (long)status); - *error = [NSError errorWithDomain:kFXKeychainErrorDomain code:status userInfo:nil]; + if (error != NULL) + { + *error = [NSError errorWithDomain:kFXKeychainErrorDomain code:status userInfo:nil]; + } return NO; } } From 8d3009da71ac3920bca372e0bc32c6de1bbc2f42 Mon Sep 17 00:00:00 2001 From: Gonzalo Larralde Date: Mon, 2 Jul 2018 13:52:59 -0300 Subject: [PATCH 6/7] Avoid name collision with original methods in Swift. --- FXKeychain/FXKeychain.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/FXKeychain/FXKeychain.h b/FXKeychain/FXKeychain.h index f4cc8c7..93848c1 100644 --- a/FXKeychain/FXKeychain.h +++ b/FXKeychain/FXKeychain.h @@ -77,12 +77,12 @@ extern NSString * _Nonnull const kFXKeychainErrorDomain; accessGroup:(nullable NSString *)accessGroup; - (BOOL)setObject:(nullable id)object forKey:(nonnull id)key; -- (BOOL)setObject:(nullable id)object forKey:(nonnull id)key error:(NSError * _Nullable * _Nullable) error; +- (BOOL)setObject:(nullable id)object forKey:(nonnull id)key error:(NSError * _Nullable * _Nullable) error NS_SWIFT_NAME(verboseSet(object:key:)); - (BOOL)setObject:(nullable id)object forKeyedSubscript:(nonnull id)key; - (BOOL)removeObjectForKey:(nonnull id)key; -- (BOOL)removeObjectForKey:(nonnull id)key error:(NSError * _Nullable * _Nullable) error; +- (BOOL)removeObjectForKey:(nonnull id)key error:(NSError * _Nullable * _Nullable) error NS_SWIFT_NAME(verboseRemove(key:)); - (nullable id)objectForKey:(nonnull id)key; -- (nullable id)objectForKey:(nonnull id)key error:(NSError * _Nullable * _Nullable) error; +- (nullable id)objectForKey:(nonnull id)key error:(NSError * _Nullable * _Nullable) error NS_SWIFT_NAME(verboseObject(key:)); - (nullable id)objectForKeyedSubscript:(nonnull id)key; @end From 4a8e9c40509de9bcdd9804b5a92bebdb04576047 Mon Sep 17 00:00:00 2001 From: Gonzalo Larralde Date: Mon, 2 Jul 2018 13:53:21 -0300 Subject: [PATCH 7/7] Bump version. --- FXKeychain.podspec.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/FXKeychain.podspec.json b/FXKeychain.podspec.json index f588479..e44e19f 100644 --- a/FXKeychain.podspec.json +++ b/FXKeychain.podspec.json @@ -1,14 +1,14 @@ { "name": "FXKeychain", - "version": "1.5.3", + "version": "1.5.4", "license": "zlib", "summary": "FXKeychain is a lightweight wrapper around the Apple keychain APIs that provides a simple dictionary-like interface.", "homepage": "https://github.com/nicklockwood/FXKeychain", "authors": "Nick Lockwood", "social_media_url": "https://twitter.com/nicklockwood", "source": { - "git": "https://github.com/nicklockwood/FXKeychain.git", - "tag": "1.5.3" + "git": "https://github.com/julioacarrettoni/FXKeychain.git", + "tag": "1.5.4" }, "source_files": "FXKeychain/FXKeychain.{h,m}", "requires_arc": true, @@ -17,4 +17,4 @@ "osx": "10.6" }, "frameworks": "Security" -} \ No newline at end of file +}