public
Created

  • Download Gist
SFHFKeychainUtils Analyzer Fix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
--- a/Classes/SFHFKeychainUtils.h
+++ b/Classes/SFHFKeychainUtils.h
@@ -35,7 +35,7 @@
}
+ (NSString *) getPasswordForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error;
-+ (void) storeUsername: (NSString *) username andPassword: (NSString *) password forServiceName: (NSString *) serviceName updateExisting: (BOOL) updateExisting error: (NSError **) error;
-+ (void) deleteItemForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error;
++ (BOOL) storeUsername: (NSString *) username andPassword: (NSString *) password forServiceName: (NSString *) serviceName updateExisting: (BOOL) updateExisting error: (NSError **) error;
++ (BOOL) deleteItemForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error;
@end
\ No newline at end of file
 
--- a/Classes/SFHFKeychainUtils.m
+++ b/Classes/SFHFKeychainUtils.m
@@ -286,19 +286,23 @@
return [password autorelease];
}
-+ (void) storeUsername: (NSString *) username andPassword: (NSString *) password forServiceName: (NSString *) serviceName updateExisting: (BOOL) updateExisting error: (NSError **) error {
- if (!username || !password || !serviceName) {
- if (error != nil) {
++ (BOOL) storeUsername: (NSString *) username andPassword: (NSString *) password forServiceName: (NSString *) serviceName updateExisting: (BOOL) updateExisting error: (NSError **) error
+{
+ if (!username || !password || !serviceName)
+ {
+ if (error != nil)
+ {
*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: -2000 userInfo: nil];
}
- return;
+ return NO;
}
// See if we already have a password entered for these credentials.
NSError *getError = nil;
NSString *existingPassword = [SFHFKeychainUtils getPasswordForUsername: username andServiceName: serviceName error:&getError];
- if ([getError code] == -1999) {
+ if ([getError code] == -1999)
+ {
// There is an existing entry without a password properly stored (possibly as a result of the previous incorrect version of this code.
// Delete the existing item before moving on entering a correct one.
@@ -306,31 +310,38 @@
[self deleteItemForUsername: username andServiceName: serviceName error: &getError];
- if ([getError code] != noErr) {
- if (error != nil) {
+ if ([getError code] != noErr)
+ {
+ if (error != nil)
+ {
*error = getError;
}
- return;
+ return NO;
}
}
- else if ([getError code] != noErr) {
- if (error != nil) {
+ else if ([getError code] != noErr)
+ {
+ if (error != nil)
+ {
*error = getError;
}
- return;
+ return NO;
}
- if (error != nil) {
+ if (error != nil)
+ {
*error = nil;
}
OSStatus status = noErr;
- if (existingPassword) {
+ if (existingPassword)
+ {
// We have an existing, properly entered item with a password.
// Update the existing item.
- if (![existingPassword isEqualToString:password] && updateExisting) {
+ if (![existingPassword isEqualToString:password] && updateExisting)
+ {
//Only update if we're allowed to update existing. If not, simply do nothing.
NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass,
@@ -350,7 +361,8 @@
status = SecItemUpdate((CFDictionaryRef) query, (CFDictionaryRef) [NSDictionary dictionaryWithObject: [password dataUsingEncoding: NSUTF8StringEncoding] forKey: (NSString *) kSecValueData]);
}
}
- else {
+ else
+ {
// No existing entry (or an existing, improperly entered, and therefore now
// deleted, entry). Create a new entry.
@@ -373,21 +385,30 @@
status = SecItemAdd((CFDictionaryRef) query, NULL);
}
- if (error != nil && status != noErr) {
+ if (error != nil && status != noErr)
+ {
// Something went wrong with adding the new item. Return the Keychain error code.
*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: status userInfo: nil];
+
+ return NO;
}
+
+ return YES;
}
-+ (void) deleteItemForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error {
- if (!username || !serviceName) {
- if (error != nil) {
++ (BOOL) deleteItemForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error
+{
+ if (!username || !serviceName)
+ {
+ if (error != nil)
+ {
*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: -2000 userInfo: nil];
}
- return;
+ return NO;
}
- if (error != nil) {
+ if (error != nil)
+ {
*error = nil;
}
@@ -398,9 +419,14 @@
OSStatus status = SecItemDelete((CFDictionaryRef) query);
- if (error != nil && status != noErr) {
+ if (error != nil && status != noErr)
+ {
*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: status userInfo: nil];
+
+ return NO;
}
+
+ return YES;
}
#endif

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.