5 // Created by J Osborne on 2/14/13.
10 #include <Security/Security.h>
11 #include <Security/SecItemPriv.h>
14 NSString *kKDSecItemsUpdated = @"KDSecItemsUpdated";
16 @interface KDSecItems ()
17 @property NSMutableArray *items;
20 @implementation KDSecItems
22 -(NSInteger)numberOfRowsInTableView:(NSTableView*)t
24 return [self.items count];
27 +(NSString*)nameOfItem:(NSDictionary*)item
29 id name = item[(id)kSecAttrService];
34 NSString *path = item[(id)kSecAttrPath];
38 NSString *port = item[(id)kSecAttrPort];
39 if ([@"0" isEqualToString:port] || [@0 isEqual:port]) {
42 port = [NSString stringWithFormat:@":%@", port];
45 return [NSString stringWithFormat:@"%@://%@%@%@", item[(id)kSecAttrProtocol], item[(id)kSecAttrServer], port, path];
48 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
50 NSString *identifier = [aTableColumn identifier];
52 if ([@"account" isEqualToString:identifier]) {
53 return self.items[rowIndex][(id)kSecAttrAccount];
55 if ([@"name" isEqualToString:identifier]) {
56 return [KDSecItems nameOfItem:self.items[rowIndex]];
59 return [NSString stringWithFormat:@"*** c=%@ r%ld", [aTableColumn identifier], (long)rowIndex];
62 -(NSArray*)fetchItemsMatching:(NSDictionary *)query
64 CFTypeRef raw_items = NULL;
65 OSStatus result = SecItemCopyMatching((__bridge CFDictionaryRef)(query), &raw_items);
68 NSLog(@"Error result %d - query: %@", result, query);
71 if (CFArrayGetTypeID() == CFGetTypeID(raw_items)) {
72 return (__bridge NSArray*)raw_items;
75 NSLog(@"Unexpected result type from copyMatching: %@ (query=%@)", raw_items, query);
83 NSDictionary *query_genp = @{(id)kSecClass: (id)kSecClassGenericPassword,
84 (__bridge id)kSecAttrSynchronizable: @1,
85 (id)kSecMatchLimit: (id)kSecMatchLimitAll,
86 (id)kSecReturnAttributes: (id)kCFBooleanTrue};
87 NSDictionary *query_inet = @{(id)kSecClass: (id)kSecClassInternetPassword,
88 (__bridge id)kSecAttrSynchronizable: @1,
89 (id)kSecMatchLimit: (id)kSecMatchLimitAll,
90 (id)kSecReturnAttributes: (id)kCFBooleanTrue};
91 NSArray *nextItems = [[self fetchItemsMatching:query_genp] arrayByAddingObjectsFromArray:[self fetchItemsMatching:query_inet]];
92 self.items = [[nextItems sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
93 NSDictionary *da = a, *db = b;
94 return [da[(id)kSecAttrService] caseInsensitiveCompare:db[(id)kSecAttrService]];
97 dispatch_async(dispatch_get_main_queue(), ^{
98 [[NSNotificationCenter defaultCenter] postNotificationName:kKDSecItemsUpdated object:self];