-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathOAToken+iOSKeychain.m
66 lines (56 loc) · 2.39 KB
/
OAToken+iOSKeychain.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
//
// OAToken+iOSKeychain.m
// OAuthConsumer
//
// Created by Gernot Poetsch on 30.11.11.
// Copyright (c) 2011 nxtbgthng GmbH. All rights reserved.
//
#import "OAToken+iOSKeychain.h"
@implementation OAToken (iOSKeychain)
+ (NSString *)serviceNameWithProvider:(NSString *)provider;
{
NSString *appName = [[NSBundle mainBundle] bundleIdentifier];
return [NSString stringWithFormat:@"%@::OAuth::%@", appName, provider];
}
+ (id)tokenFromDefaultKeychainWithServiceProviderName:(NSString *)provider;
{
NSString *serviceName = [[self class] serviceNameWithProvider:provider];
NSDictionary *result = nil;
NSDictionary *query = [NSDictionary dictionaryWithObjectsAndKeys:
(NSString *)kSecClassGenericPassword, kSecClass,
serviceName, kSecAttrService,
kCFBooleanTrue, kSecReturnAttributes,
nil];
OSStatus status = SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef *)&result);
[result autorelease];
if (status != noErr) {
NSAssert1(status == errSecItemNotFound, @"unexpected error while fetching token from keychain: %ld", status);
return nil;
}
return [NSKeyedUnarchiver unarchiveObjectWithData:[result objectForKey:(NSString *)kSecAttrGeneric]];
}
- (void)storeInDefaultKeychainWithServiceProviderName:(NSString *)provider;
{
NSString *serviceName = [[self class] serviceNameWithProvider:provider];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self];
NSDictionary *query = [NSDictionary dictionaryWithObjectsAndKeys:
(NSString *)kSecClassGenericPassword, kSecClass,
serviceName, kSecAttrService,
@"OAuth Access Token", kSecAttrLabel,
data, kSecAttrGeneric,
nil];
[self removeFromDefaultKeychainWithServiceProviderName:provider];
OSStatus __attribute__((unused)) err = SecItemAdd((CFDictionaryRef)query, NULL);
NSAssert1(err == noErr, @"error while adding token to keychain: %ld", err);
}
- (void)removeFromDefaultKeychainWithServiceProviderName:(NSString *)provider;
{
NSString *serviceName = [[self class] serviceNameWithProvider:provider];
NSDictionary *query = [NSDictionary dictionaryWithObjectsAndKeys:
(NSString *)kSecClassGenericPassword, kSecClass,
serviceName, kSecAttrService,
nil];
OSStatus __attribute__((unused)) err = SecItemDelete((CFDictionaryRef)query);
NSAssert1((err == noErr || err == errSecItemNotFound), @"error while deleting token from keychain: %ld", err);
}
@end