Guest User

Untitled

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