Permalink
Browse files

added method to delete all keys for a service

  • Loading branch information...
Jason Rush
Jason Rush committed May 10, 2011
1 parent e0eaedf commit d02089402bcbb0bb8fd3e6995aeee4c0fb585910
Showing with 42 additions and 8 deletions.
  1. +1 −0 Security/SFHFKeychainUtils.h
  2. +41 −8 Security/SFHFKeychainUtils.m
@@ -37,5 +37,6 @@
+ (NSString *) getPasswordForUsername: (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;
++ (BOOL) deleteAllItemForServiceName: (NSString *) serviceName error: (NSError **) error;
@end
@@ -399,19 +399,19 @@ + (BOOL) storeUsername: (NSString *) username andPassword: (NSString *) password
+ (BOOL) deleteItemForUsername: (NSString *) username andServiceName: (NSString *) serviceName error: (NSError **) error
{
if (!username || !serviceName)
- {
- if (error != nil)
{
+ if (error != nil)
+ {
*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: -2000 userInfo: nil];
}
return NO;
}
if (error != nil)
- {
+ {
*error = nil;
}
-
+
NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, kSecAttrService, kSecReturnAttributes, nil] autorelease];
NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, username, serviceName, kCFBooleanTrue, nil] autorelease];
@@ -420,13 +420,46 @@ + (BOOL) deleteItemForUsername: (NSString *) username andServiceName: (NSString
OSStatus status = SecItemDelete((CFDictionaryRef) query);
if (error != nil && status != noErr)
- {
+ {
*error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: status userInfo: nil];
+
+ return NO;
+ }
- return NO;
+ return YES;
+}
+
++ (BOOL) deleteAllItemForServiceName: (NSString *) serviceName error: (NSError **) error
+{
+ if (!serviceName)
+ {
+ if (error != nil)
+ {
+ *error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: -2000 userInfo: nil];
+ }
+ return NO;
}
-
- return YES;
+
+ if (error != nil)
+ {
+ *error = nil;
+ }
+
+ NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrService, kSecReturnAttributes, nil] autorelease];
+ NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, serviceName, kCFBooleanTrue, nil] autorelease];
+
+ NSDictionary *query = [[[NSDictionary alloc] initWithObjects: objects forKeys: keys] autorelease];
+
+ OSStatus status = SecItemDelete((CFDictionaryRef) query);
+
+ if (error != nil && status != noErr)
+ {
+ *error = [NSError errorWithDomain: SFHFKeychainUtilsErrorDomain code: status userInfo: nil];
+
+ return NO;
+ }
+
+ return YES;
}
#endif

0 comments on commit d020894

Please sign in to comment.