forked from kishikawakatsumi/UICKeyChainStore
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUICKeyChainStore.m
executable file
·377 lines (315 loc) · 13.1 KB
/
UICKeyChainStore.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//
// UICKeyChainStore.m
// UICKeyChainStore
//
// Created by Kishikawa Katsumi on 11/11/20.
// Copyright (c) 2011 Kishikawa Katsumi. All rights reserved.
//
#import "UICKeyChainStore.h"
static NSString *defaultService;
@interface UICKeyChainStore () {
NSMutableDictionary *itemsToUpdate;
}
@end
@implementation UICKeyChainStore
@synthesize service;
@synthesize accessGroup;
+ (void)initialize {
defaultService = [[NSBundle mainBundle] bundleIdentifier];
}
+ (NSString *)stringForKey:(NSString *)key {
return [self stringForKey:key service:defaultService accessGroup:nil];
}
+ (NSString *)stringForKey:(NSString *)key service:(NSString *)service {
return [self stringForKey:key service:service accessGroup:nil];
}
+ (NSString *)stringForKey:(NSString *)key service:(NSString *)service accessGroup:(NSString *)accessGroup {
NSData *data = [self dataForKey:key service:service accessGroup:accessGroup];
if (data) {
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
return nil;
}
+ (void)setString:(NSString *)value forKey:(NSString *)key {
[self setString:value forKey:key service:defaultService accessGroup:nil];
}
+ (void)setString:(NSString *)value forKey:(NSString *)key service:(NSString *)service {
[self setString:value forKey:key service:service accessGroup:nil];
}
+ (void)setString:(NSString *)value forKey:(NSString *)key service:(NSString *)service accessGroup:(NSString *)accessGroup {
NSData *data = [value dataUsingEncoding:NSUTF8StringEncoding];
[self setData:data forKey:key service:service accessGroup:accessGroup];
}
+ (NSData *)dataForKey:(NSString *)key {
return [self dataForKey:key service:defaultService accessGroup:nil];
}
+ (NSData *)dataForKey:(NSString *)key service:(NSString *)service {
return [self dataForKey:key service:service accessGroup:nil];
}
+ (NSData *)dataForKey:(NSString *)key service:(NSString *)service accessGroup:(NSString *)accessGroup {
if (!key) {
NSAssert(NO, @"key must not be nil.");
return nil;
}
if (!service) {
service = defaultService;
}
NSMutableDictionary* query = [NSMutableDictionary dictionary];
[query setObject:(__bridge id)(kSecClassGenericPassword) forKey:(__bridge id<NSCopying>)(kSecClass)];
[query setObject:(id)kCFBooleanTrue forKey:(__bridge id<NSCopying>)(kSecReturnData)];
[query setObject:(__bridge id)(kSecMatchLimitOne) forKey:(__bridge id<NSCopying>)(kSecMatchLimit)];
[query setObject:service forKey:(__bridge id<NSCopying>)(kSecAttrService)];
[query setObject:key forKey:(__bridge id<NSCopying>)(kSecAttrGeneric)];
[query setObject:key forKey:(__bridge id<NSCopying>)(kSecAttrAccount)];
#if !TARGET_IPHONE_SIMULATOR
if (accessGroup) {
[query setObject:accessGroup forKey:kSecAttrAccessGroup];
}
#endif
NSData *data = nil;
CFTypeRef *localData = NULL;
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef *)&localData);
if (status != errSecSuccess) {
return nil;
}
data = objc_retainedObject(localData);
return data;
}
+ (void)setData:(NSData *)data forKey:(NSString *)key {
[self setData:data forKey:key service:defaultService accessGroup:nil];
}
+ (void)setData:(NSData *)data forKey:(NSString *)key service:(NSString *)service {
[self setData:data forKey:key service:service accessGroup:nil];
}
+ (void)setData:(NSData *)data forKey:(NSString *)key service:(NSString *)service accessGroup:(NSString *)accessGroup {
if (!key) {
NSAssert(NO, @"key must not be nil.");
return;
}
if (!service) {
service = defaultService;
}
NSMutableDictionary *query = [NSMutableDictionary dictionary];
[query setObject:(__bridge id)(kSecClassGenericPassword) forKey:(__bridge id<NSCopying>)(kSecClass)];
[query setObject:service forKey:(__bridge id<NSCopying>)(kSecAttrService)];
[query setObject:key forKey:(__bridge id<NSCopying>)(kSecAttrGeneric)];
[query setObject:key forKey:(__bridge id<NSCopying>)(kSecAttrAccount)];
#if !TARGET_IPHONE_SIMULATOR
if (accessGroup) {
[query setObject:accessGroup forKey:kSecAttrAccessGroup];
}
#endif
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)query, NULL);
if (status == errSecSuccess) {
if (data) {
NSMutableDictionary *attributesToUpdate = [NSMutableDictionary dictionary];
[attributesToUpdate setObject:data forKey:(__bridge id<NSCopying>)(kSecValueData)];
status = SecItemUpdate((__bridge CFDictionaryRef)query, (__bridge CFDictionaryRef)attributesToUpdate);
if (status != errSecSuccess) {
NSLog(@"%s|SecItemUpdate: error(%ld)", __func__, status);
}
} else {
[self removeItemForKey:key service:service accessGroup:accessGroup];
}
} else if (status == errSecItemNotFound) {
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:(__bridge id)(kSecClassGenericPassword) forKey:(__bridge id<NSCopying>)(kSecClass)];
[attributes setObject:service forKey:(__bridge id<NSCopying>)(kSecAttrService)];
[attributes setObject:key forKey:(__bridge id<NSCopying>)(kSecAttrGeneric)];
[attributes setObject:key forKey:(__bridge id<NSCopying>)(kSecAttrAccount)];
[attributes setObject:data forKey:(__bridge id<NSCopying>)(kSecValueData)];
#if !TARGET_IPHONE_SIMULATOR
if (accessGroup) {
[attributes setObject:accessGroup forKey:kSecAttrAccessGroup];
}
#endif
status = SecItemAdd((__bridge CFDictionaryRef)attributes, NULL);
if (status != errSecSuccess) {
NSLog(@"%s|SecItemAdd: error(%ld)", __func__, status);
}
} else {
NSLog(@"%s|SecItemCopyMatching: error(%ld)", __func__, status);
}
}
+ (void)removeItemForKey:(NSString *)key {
[UICKeyChainStore removeItemForKey:key service:defaultService accessGroup:nil];
}
+ (void)removeItemForKey:(NSString *)key service:(NSString *)service {
[UICKeyChainStore removeItemForKey:key service:service accessGroup:nil];
}
+ (void)removeItemForKey:(NSString *)key service:(NSString *)service accessGroup:(NSString *)accessGroup {
if (!key) {
NSAssert(NO, @"key must not be nil.");
return;
}
if (!service) {
service = defaultService;
}
NSMutableDictionary *itemToDelete = [NSMutableDictionary dictionary];
[itemToDelete setObject:(__bridge id)(kSecClassGenericPassword) forKey:(__bridge id<NSCopying>)(kSecClass)];
[itemToDelete setObject:service forKey:(__bridge id<NSCopying>)(kSecAttrService)];
[itemToDelete setObject:key forKey:(__bridge id<NSCopying>)(kSecAttrGeneric)];
[itemToDelete setObject:key forKey:(__bridge id<NSCopying>)(kSecAttrAccount)];
#if !TARGET_IPHONE_SIMULATOR
if (accessGroup) {
[itemToDelete setObject:accessGroup forKey:kSecAttrAccessGroup];
}
#endif
OSStatus status = SecItemDelete((__bridge CFDictionaryRef)itemToDelete);
if (status != errSecSuccess && status != errSecItemNotFound) {
NSLog(@"%s|SecItemDelete: error(%ld)", __func__, status);
}
}
+ (NSArray *)itemsForService:(NSString *)service accessGroup:(NSString *)accessGroup {
if (!service) {
service = defaultService;
}
NSMutableDictionary *query = [NSMutableDictionary dictionary];
[query setObject:(__bridge id)(kSecClassGenericPassword) forKey:(__bridge id<NSCopying>)(kSecClass)];
[query setObject:(id)kCFBooleanTrue forKey:(__bridge id<NSCopying>)(kSecReturnAttributes)];
[query setObject:(id)kCFBooleanTrue forKey:(__bridge id<NSCopying>)(kSecReturnData)];
[query setObject:CFBridgingRelease(kSecMatchLimitAll) forKey:(__bridge id<NSCopying>)(kSecMatchLimit)];
[query setObject:service forKey:(__bridge id<NSCopying>)(kSecAttrService)];
#if !TARGET_IPHONE_SIMULATOR
if (accessGroup) {
[query setObject:accessGroup forKey:kSecAttrAccessGroup];
}
#endif
CFArrayRef result = nil;
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef *)&result);
if (status == errSecSuccess || status == errSecItemNotFound) {
return (__bridge NSArray *)result ;
} else {
NSLog(@"%s|SecItemCopyMatching: error(%ld)", __func__, status);
return nil;
}
}
+ (void)removeAllItems {
[self removeAllItemsForService:defaultService accessGroup:nil];
}
+ (void)removeAllItemsForService:(NSString *)service {
[self removeAllItemsForService:service accessGroup:nil];
}
+ (void)removeAllItemsForService:(NSString *)service accessGroup:(NSString *)accessGroup {
NSArray *items = [UICKeyChainStore itemsForService:service accessGroup:accessGroup];
for (NSDictionary *item in items) {
NSMutableDictionary *itemToDelete = [NSMutableDictionary dictionaryWithDictionary:item];
[itemToDelete setObject:(__bridge id)(kSecClassGenericPassword) forKey:(__bridge id<NSCopying>)(kSecClass)];
OSStatus status = SecItemDelete((__bridge CFDictionaryRef)itemToDelete);
if (status != errSecSuccess) {
NSLog(@"%s|SecItemDelete: error(%ld)", __func__, status);
NSLog(@"%@", itemToDelete);
}
}
}
#pragma mark -
+ (UICKeyChainStore *)keyChainStore {
return [[self alloc] initWithService:defaultService];
}
+ (UICKeyChainStore *)keyChainStoreWithService:(NSString *)service {
return [[self alloc] initWithService:service];
}
+ (UICKeyChainStore *)keyChainStoreWithService:(NSString *)service accessGroup:(NSString *)accessGroup {
return [[self alloc] initWithService:service accessGroup:accessGroup];
}
- (id)init {
return [self initWithService:defaultService accessGroup:nil];
}
- (id)initWithService:(NSString *)s {
return [self initWithService:s accessGroup:nil];
}
- (id)initWithService:(NSString *)s accessGroup:(NSString *)group {
self = [super init];
if (self) {
if (!s) {
s = defaultService;
}
service = [s copy];
accessGroup = [group copy];
if (accessGroup) {
#if !TARGET_IPHONE_SIMULATOR
[itemsToUpdate setObject:accessGroup forKey:(id)kSecAttrAccessGroup];
#endif
}
NSMutableDictionary *query = [NSMutableDictionary dictionaryWithDictionary:itemsToUpdate];
[query setObject:(__bridge id)kSecMatchLimitAll forKey:(__bridge id)kSecMatchLimit];
[query setObject:(id)kCFBooleanTrue forKey:(__bridge id)kSecReturnAttributes];
NSMutableDictionary *result = nil;
CFTypeRef localResult = NULL;
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef *)&localResult);
if (status == errSecSuccess) {
result = objc_retainedObject(localResult);
itemsToUpdate = [[NSMutableDictionary alloc] initWithDictionary:result];
} else {
itemsToUpdate = [[NSMutableDictionary alloc] init];
}
}
return self;
}
#pragma mark -
- (NSString *)description {
NSArray *items = [UICKeyChainStore itemsForService:service accessGroup:accessGroup];
NSMutableArray *list = [NSMutableArray arrayWithCapacity:[items count]];
for (NSDictionary *attributes in items) {
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
[attrs setObject:[attributes objectForKey:(__bridge id)(kSecAttrService)] forKey:@"Service"];
[attrs setObject:[attributes objectForKey:(__bridge id)(kSecAttrAccount)] forKey:@"Account"];
[attrs setObject:[attributes objectForKey:(__bridge id)(kSecAttrAccessGroup)] forKey:@"AccessGroup"];
NSData *data = [attributes objectForKey:(__bridge id)(kSecValueData)];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (string) {
[attrs setObject:string forKey:@"Value"];
} else {
[attrs setObject:data forKey:@"Value"];
}
[list addObject:attrs];
}
return [list description];
}
#pragma mark -
- (void)setString:(NSString *)string forKey:(NSString *)key {
[self setData:[string dataUsingEncoding:NSUTF8StringEncoding] forKey:key];
}
- (NSString *)stringForKey:(id)key {
NSData *data = [self dataForKey:key];
if (data) {
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
return nil;
}
- (void)setData:(NSData *)data forKey:(NSString *)key {
if (!key) {
return;
}
if (!data) {
[self removeItemForKey:key];
} else {
[itemsToUpdate setObject:data forKey:key];
}
}
- (NSData *)dataForKey:(NSString *)key {
NSData *data = [itemsToUpdate objectForKey:key];
if (!data) {
data = [[self class] dataForKey:key service:service accessGroup:accessGroup];
}
return data;
}
- (void)removeItemForKey:(NSString *)key {
if ([itemsToUpdate objectForKey:key]) {
[itemsToUpdate removeObjectForKey:key];
} else {
[[self class] removeItemForKey:key service:service accessGroup:accessGroup];
}
}
#pragma mark -
- (void)removeAllItems {
[itemsToUpdate removeAllObjects];
[[self class] removeAllItemsForService:service accessGroup:accessGroup];
}
#pragma mark -
- (void)synchronize {
for (NSString *key in itemsToUpdate) {
[[self class] setData:[itemsToUpdate objectForKey:key] forKey:key service:service accessGroup:accessGroup];
}
}
@end