-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAMKeychainManager.m
184 lines (146 loc) · 5.86 KB
/
AMKeychainManager.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
//
// AMKeychainManager.m
// Created by Joan Martin.
// Take a look to my repos at http://github.com/vilanovi
//
// Copyright (c) 2013 Joan Martin, [email protected].
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
#import "AMKeychainManager.h"
@implementation AMKeychainManager
#pragma mark Static Methods
+ (AMKeychainManager*)defaultManagerForService:(NSString*)service
{
__strong static NSMutableDictionary *_managers = nil;
if (!_managers)
_managers = [NSMutableDictionary dictionary];
id sharedObject = nil;
@synchronized(self)
{
sharedObject = [_managers valueForKey:service];
if (!sharedObject)
{
sharedObject = [[AMKeychainManager alloc] initWithService:service];
[_managers setValue:sharedObject forKey:service];
}
}
return sharedObject;
}
+ (AMKeychainManager*)defaultManager
{
AMKeychainManager *manager = [self defaultManagerForService:[[NSBundle mainBundle] bundleIdentifier]];
return manager;
}
#pragma mark Initializers
- (id)initWithService:(NSString*)service
{
self = [super init];
if (self)
{
_service = service;
}
return self;
}
#pragma mark Public Methods
- (NSData*)keychainDataForKey:(NSString*)key
{
return [self _retrieveFromKeychainDataForKey:key];
}
- (void)setKeychainData:(NSData*)data forKey:(NSString*)key
{
[self _storeInKeychainData:data forKey:key];
}
- (NSString*)keychainValueForKey:(NSString*)key
{
NSData *data = [self keychainDataForKey:key];
if (data)
{
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return string;
}
return nil;
}
- (void)setKeychainValue:(NSString*)value forKey:(NSString*)key
{
NSData *data = [value dataUsingEncoding:NSUTF8StringEncoding];
[self setKeychainData:data forKey:key];
}
- (void)removeKeychainEntryForKey:(NSString*)key;
{
[self _removeFromKeychainDataForKey:key];
}
#pragma mark Private Methods
- (void)_storeInKeychainData:(NSData*)data forKey:(NSString*)key
{
if (data == nil)
{
[self _removeFromKeychainDataForKey:key];
return;
}
// Build the keychain query
NSMutableDictionary *keychainQuery = [NSMutableDictionary dictionaryWithObjectsAndKeys:
(__bridge_transfer NSString *)kSecClassGenericPassword, (__bridge_transfer NSString *)kSecClass,
_service, kSecAttrService,
key, kSecAttrAccount,
kCFBooleanTrue, kSecReturnAttributes,
nil];
CFDictionaryRef query = (__bridge_retained CFDictionaryRef) keychainQuery;
// Delete an existing entry first:
SecItemDelete(query);
// Add the accounts query to the keychain query:
[keychainQuery setObject:data forKey:(__bridge_transfer NSString *)kSecValueData];
// Add the token data to the keychain
// Even if we never use resData, replacing with NULL in the call throws EXC_BAD_ACCESS
CFTypeRef resData = NULL;
SecItemAdd(query, (CFTypeRef *) &resData);
CFRelease(query);
}
- (NSData*)_retrieveFromKeychainDataForKey:(NSString*)key
{
NSData *data = nil;
// Build the keychain query
NSDictionary *keychainQuery = [NSDictionary dictionaryWithObjectsAndKeys:
(__bridge_transfer NSString *)kSecClassGenericPassword, (__bridge_transfer NSString *)kSecClass,
_service, kSecAttrService,
key, kSecAttrAccount,
kCFBooleanTrue, kSecReturnData,
kSecMatchLimitOne, kSecMatchLimit,
nil];
CFDictionaryRef query = (__bridge_retained CFDictionaryRef) keychainQuery;
// Get the token data from the keychain
CFTypeRef resData = NULL;
// Get the token dictionary from the keychain
if (SecItemCopyMatching(query, (CFTypeRef *) &resData) == noErr)
data = (__bridge_transfer NSData *)resData;
CFRelease(query);
return data;
}
- (void)_removeFromKeychainDataForKey:(NSString*)key
{
// Build the keychain query
NSDictionary *keychainQuery = [NSDictionary dictionaryWithObjectsAndKeys:
(__bridge_transfer NSString *)kSecClassGenericPassword, (__bridge_transfer NSString *)kSecClass,
_service, kSecAttrService,
key, kSecAttrAccount,
kCFBooleanTrue, kSecReturnAttributes,
nil];
CFDictionaryRef query = (__bridge_retained CFDictionaryRef) keychainQuery;
SecItemDelete(query);
CFRelease(query);
}
@end