-
Notifications
You must be signed in to change notification settings - Fork 0
/
ACKeychain.m
executable file
·191 lines (162 loc) · 9.71 KB
/
ACKeychain.m
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
// ACKeychain.m
// ACKeychain
//
// Created by Anthony Castelli on 3/23/13.
// Copyright (c) 2013 Emerys. All rights reserved.
//
#import "ACKeychain.h"
NSString *const ACKeychainPassword = @"password";
NSString *const ACKeychainUsername = @"username";
NSString *const ACKeychainIdentifier = @"identifier";
NSString *const ACKeychainExpirationDate = @"expirationDate";
NSString *const ACKeychainService = @"service";
NSString *const ACKeychainInfo = @"info";
@interface ACKeychain (Private)
- (NSDictionary *)credentialsFromKeychainItem:(NSDictionary *)item;
@end
@implementation ACKeychain
+ (ACKeychain *)defaultKeychain {
static ACKeychain *_sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedInstance = [[self alloc] init];
});
return _sharedInstance;
}
- (NSDictionary *)credentialsForQuery:(NSDictionary *)query {
CFDictionaryRef result = NULL;
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef *) &result);
if (status == errSecSuccess && result != NULL) {
NSDictionary *credentials = [self credentialsFromKeychainItem:(__bridge_transfer NSDictionary *) result];
return credentials;
}
return nil;
}
- (NSDictionary *)credentialsFromKeychainItem:(NSDictionary *)item {
NSString *username = [[NSString alloc] initWithData:[item valueForKey:(__bridge id)kSecAttrAccount]
encoding:NSUTF8StringEncoding];
NSString *identifier = [[NSString alloc] initWithData:[item valueForKey:(__bridge id)kSecAttrGeneric]
encoding:NSUTF8StringEncoding];
NSString *service = [[NSString alloc] initWithData:[item valueForKey:(__bridge id)kSecAttrService]
encoding:NSUTF8StringEncoding];
NSData *passwordData = [item valueForKey:(__bridge id)kSecValueData];
NSString *password = nil;
if (passwordData) {
password = [[NSString alloc] initWithData:passwordData
encoding:NSUTF8StringEncoding];
}
NSData *miscData = [item valueForKey:(__bridge id)kSecAttrComment];
NSDictionary *misc = nil;
if (miscData) {
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:miscData];
misc = [unarchiver decodeObjectForKey:ACKeychainInfo];
[unarchiver finishDecoding];
}
NSMutableDictionary *credentials = [NSMutableDictionary dictionaryWithObjectsAndKeys:
username, ACKeychainUsername,
identifier, ACKeychainIdentifier,
service, ACKeychainService, nil];
[credentials setValue:password forKey:ACKeychainPassword];
[credentials setValue:misc forKey:ACKeychainInfo];
[credentials setValue:[misc valueForKey:ACKeychainExpirationDate] forKey:ACKeychainExpirationDate];
return credentials;
}
- (BOOL)storeUsername:(NSString *)username password:(NSString *)password identifier:(NSString *)identifier forService:(NSString *)service {
return [self storeUsername:username password:password identifier:identifier expirationDate:nil forService:service];
}
- (BOOL)storeUsername:(NSString *)username password:(NSString *)password identifier:(NSString *)identifier info:(NSDictionary *)info forService:(NSString *)service {
if ([self deleteCredentialsForUsername:username service:service] &&
[self deleteCredentialsForIdentifier:identifier service:service]) {
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
(__bridge id)kSecClassGenericPassword, (__bridge id)kSecClass,
[username dataUsingEncoding:NSUTF8StringEncoding], (__bridge id)kSecAttrAccount,
[identifier dataUsingEncoding:NSUTF8StringEncoding], (__bridge id)kSecAttrGeneric,
[service dataUsingEncoding:NSUTF8StringEncoding], (__bridge id)kSecAttrService, nil];
[dictionary setValue:[password dataUsingEncoding:NSUTF8StringEncoding] forKey:(__bridge id)kSecValueData];
NSMutableData *miscData = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:miscData];
[archiver encodeObject:info forKey:ACKeychainInfo];
[archiver finishEncoding];
[dictionary setValue:miscData forKey:(__bridge id)kSecAttrComment];
OSStatus status = SecItemAdd((__bridge CFDictionaryRef)dictionary, NULL);
return (status == errSecSuccess);
}
return NO;
}
- (BOOL)storeUsername:(NSString *)username password:(NSString *)password identifier:(NSString *)identifier expirationDate:(NSDate *)expirationDate forService:(NSString *)service {
NSDictionary *info = nil;
if (expirationDate) {
info = [NSDictionary dictionaryWithObject:expirationDate
forKey:ACKeychainExpirationDate];
}
return [self storeUsername:username password:password identifier:identifier info:info forService:service];
}
- (NSDictionary *)credentialsForIdentifier:(NSString *)identifier service:(NSString *)service {
NSDictionary *query = [NSDictionary dictionaryWithObjectsAndKeys:
(__bridge id)kSecClassGenericPassword, (__bridge id)kSecClass,
[service dataUsingEncoding:NSUTF8StringEncoding], (__bridge id)kSecAttrService,
[identifier dataUsingEncoding:NSUTF8StringEncoding], (__bridge id)kSecAttrGeneric,
(__bridge id)kCFBooleanTrue, (__bridge id)kSecReturnAttributes,
(__bridge id)kCFBooleanTrue, (__bridge id)kSecReturnData,
nil];
return [self credentialsForQuery:query];
}
- (NSDictionary *)credentialsForUsername:(NSString *)username service:(NSString *)service {
NSDictionary *query = [NSDictionary dictionaryWithObjectsAndKeys:
(__bridge id)kSecClassGenericPassword, (__bridge id)kSecClass,
[service dataUsingEncoding:NSUTF8StringEncoding], (__bridge id)kSecAttrService,
[username dataUsingEncoding:NSUTF8StringEncoding], (__bridge id)kSecAttrAccount,
(__bridge id)kCFBooleanTrue, (__bridge id)kSecReturnAttributes,
(__bridge id)kCFBooleanTrue, (__bridge id)kSecReturnData,
nil];
return [self credentialsForQuery:query];
}
- (NSArray *)allCredentialsForService:(NSString *)service limit:(NSUInteger)limit {
NSDictionary *query = [NSDictionary dictionaryWithObjectsAndKeys:
(__bridge id)kSecClassGenericPassword, (__bridge id)kSecClass,
[service dataUsingEncoding:NSUTF8StringEncoding], (__bridge id)kSecAttrService,
(__bridge id)kCFBooleanTrue, (__bridge id)kSecReturnAttributes,
[NSNumber numberWithInt:limit], (__bridge id)kSecMatchLimit,
(__bridge id)kCFBooleanTrue, (__bridge id)kSecReturnData,
nil];
CFArrayRef list = NULL;
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef *) &list);
if (status == errSecSuccess && list != NULL) {
NSMutableArray *result = [NSMutableArray array];
for (NSDictionary *item in (__bridge_transfer NSArray *)list) {
NSDictionary *credentials = [self credentialsFromKeychainItem:item];
[result addObject:credentials];
}
return result;
}
return nil;
}
- (BOOL)deleteCredentialsForIdentifier:(NSString *)identifier service:(NSString *)service {
NSDictionary *query = [NSDictionary dictionaryWithObjectsAndKeys:
(__bridge id)kSecClassGenericPassword, (__bridge id)kSecClass,
[service dataUsingEncoding:NSUTF8StringEncoding], (__bridge id)kSecAttrService,
[identifier dataUsingEncoding:NSUTF8StringEncoding], (__bridge id)kSecAttrGeneric,
nil];
OSStatus status = SecItemDelete((__bridge CFDictionaryRef)query);
return (status == errSecSuccess || status == errSecItemNotFound);
}
- (BOOL)deleteCredentialsForUsername:(NSString *)username service:(NSString *)service {
NSDictionary *query = [NSDictionary dictionaryWithObjectsAndKeys:
(__bridge id)kSecClassGenericPassword, (__bridge id)kSecClass,
[service dataUsingEncoding:NSUTF8StringEncoding], (__bridge id)kSecAttrService,
[username dataUsingEncoding:NSUTF8StringEncoding], (__bridge id)kSecAttrAccount,
nil];
OSStatus status = SecItemDelete((__bridge CFDictionaryRef)query);
return (status == errSecSuccess || status == errSecItemNotFound);
}
- (BOOL)deleteAllCredentialsForService:(NSString *)service {
NSDictionary *query = [NSDictionary dictionaryWithObjectsAndKeys:
(__bridge id)kSecClassGenericPassword, (__bridge id)kSecClass,
[service dataUsingEncoding:NSUTF8StringEncoding], (__bridge id)kSecAttrService,
(__bridge id)kCFBooleanTrue, (__bridge id)kSecReturnAttributes,
nil];
OSStatus status = SecItemDelete((__bridge CFDictionaryRef)query);
return (status == errSecSuccess || status == errSecItemNotFound);
}
@end