Repository navigation
Expand file tree
/
Copy pathDBKeychain-iOS.m
More file actions
executable file
·81 lines (63 loc) · 2.49 KB
/
Copy pathDBKeychain-iOS.m
File metadata and controls
executable file
·81 lines (63 loc) · 2.49 KB
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
//
// DBKeychain.m
// DropboxSDK
//
// Created by Brian Smith on 4/5/12.
// Copyright (c) 2012 Dropbox, Inc. All rights reserved.
//
#import "DBKeychain.h"
#import "DBLog.h"
static NSDictionary *kDBKeychainDict;
@implementation DBKeychain
+ (void)initialize {
if ([self class] != [DBKeychain class]) return;
NSString *keychainId = [NSString stringWithFormat:@"%@.dropbox.auth", [[NSBundle mainBundle] bundleIdentifier]];
kDBKeychainDict = [[NSDictionary alloc] initWithObjectsAndKeys:
(id)kSecClassGenericPassword, (id)kSecClass,
keychainId, (id)kSecAttrService,
nil];
}
+ (NSDictionary *)credentials {
NSMutableDictionary *searchDict = [NSMutableDictionary dictionaryWithDictionary:kDBKeychainDict];
[searchDict setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
[searchDict setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnAttributes];
[searchDict setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
NSDictionary *attrDict = nil;
OSStatus status = SecItemCopyMatching((CFDictionaryRef)searchDict, (CFTypeRef *)&attrDict);
[attrDict autorelease];
NSData *foundValue = [attrDict objectForKey:(id)kSecValueData];
if (status == noErr && foundValue) {
return [NSKeyedUnarchiver unarchiveObjectWithData:foundValue];
} else {
if (status != errSecItemNotFound) {
DBLogWarning(@"DropboxSDK: error reading stored credentials (%li)", status);
}
return nil;
}
}
+ (void)setCredentials:(NSDictionary *)credentials {
NSData *credentialData = [NSKeyedArchiver archivedDataWithRootObject:credentials];
NSMutableDictionary *attrDict = [NSMutableDictionary dictionaryWithDictionary:kDBKeychainDict];
[attrDict setObject:credentialData forKey:(id)kSecValueData];
NSArray *version = [[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."];
if ([[version objectAtIndex:0] intValue] >= 4) {
[attrDict setObject:(id)kSecAttrAccessibleWhenUnlocked forKey:(id)kSecAttrAccessible];
}
OSStatus status = noErr;
if ([self credentials]) {
[attrDict removeObjectForKey:(id)kSecClass];
status = SecItemUpdate((CFDictionaryRef)kDBKeychainDict, (CFDictionaryRef)attrDict);
} else {
status = SecItemAdd((CFDictionaryRef)attrDict, NULL);
}
if (status != noErr) {
DBLogWarning(@"DropboxSDK: error saving credentials (%li)", status);
}
}
+ (void)deleteCredentials {
OSStatus status = SecItemDelete((CFDictionaryRef)kDBKeychainDict);
if (status != noErr) {
DBLogWarning(@"DropboxSDK: error deleting credentials (%li)", status);
}
}
@end