This repository has been archived by the owner on Aug 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
ORSCredentialsManager.m
141 lines (130 loc) · 4.28 KB
/
ORSCredentialsManager.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
//
// ORSCredentialsManager.m
// Credentials Manager
//
// Created by Nicholas Toumpelis on 12/04/2009.
// Copyright 2009 Ocean Road Software. All rights reserved.
//
// Version 0.7
#import "ORSCredentialsManager.h"
@implementation ORSCredentialsManager
// Determines whether the specified user has a password in the keychain.
- (BOOL) hasPasswordForUser:(NSString *)username {
passwordData = nil;
OSStatus result;
@synchronized(self) {
result = SecKeychainFindGenericPassword(NULL, APP_CNAME_LENGTH,
APP_CNAME,
[username lengthOfBytesUsingEncoding:NSASCIIStringEncoding],
[username cStringUsingEncoding:NSASCIIStringEncoding],
&passwordLength, &passwordData, &loginItem);
}
lastCheckedUsername = username;
if (result == 0) {
return YES;
} else {
return NO;
}
}
// Retrieves the password for the specified user.
- (NSString *) passwordForUser:(NSString *)username {
if (![username isEqualToString:lastCheckedUsername]) {
passwordData = nil;
@synchronized(self) {
SecKeychainFindGenericPassword(NULL, APP_CNAME_LENGTH, APP_CNAME,
[username lengthOfBytesUsingEncoding:NSASCIIStringEncoding],
[username cStringUsingEncoding:NSASCIIStringEncoding],
&passwordLength, &passwordData, &loginItem);
}
}
NSString *password = [[NSString alloc] initWithBytes:passwordData
length:passwordLength
encoding:NSASCIIStringEncoding];
return password;
}
// Adds a password to the keychain manager for the specified username.
- (BOOL) addPassword:(NSString *)password
forUser:(NSString *)username {
OSStatus result;
@synchronized(self) {
result = SecKeychainAddGenericPassword(NULL, APP_CNAME_LENGTH,
APP_CNAME,
[username lengthOfBytesUsingEncoding:NSASCIIStringEncoding],
[username cStringUsingEncoding:NSASCIIStringEncoding],
[password lengthOfBytesUsingEncoding:NSASCIIStringEncoding],
[password cStringUsingEncoding:NSASCIIStringEncoding], &loginItem);
}
if (result == 0) {
return YES;
} else {
return NO;
}
}
// Sets a password in the keychain manager for the specified username.
- (BOOL) setPassword:(NSString *)password
forUser:(NSString *)username {
[self hasPasswordForUser:username]; // gets loginItem
OSStatus result;
@synchronized(self) {
result = SecKeychainItemModifyAttributesAndData(loginItem,
NULL, [password lengthOfBytesUsingEncoding:NSASCIIStringEncoding],
[password cStringUsingEncoding:NSASCIIStringEncoding]);
}
if (result == 0) {
return YES;
} else {
return NO;
}
}
// Determines whether the specified user has a twitter password in
// the keychain
- (BOOL) hasStoredTwitterPasswordForUser:(NSString *)username {
twitterPasswordData = nil;
OSStatus result;
@synchronized(self) {
result = SecKeychainFindInternetPassword(NULL, 11,
"twitter.com", 0, NULL,
[username lengthOfBytesUsingEncoding:NSASCIIStringEncoding],
[username cStringUsingEncoding:NSASCIIStringEncoding], 1, "/", 0,
kSecProtocolTypeHTTPS, kSecAuthenticationTypeDefault,
&twitterPasswordLength, &twitterPasswordData, NULL);
}
lastCheckedTwitterUsername = username;
if (result == 0) {
return YES;
} else {
return NO;
}
}
// Retrieves the current Twitter web password (saved in the keychain).
- (NSString *) storedTwitterPasswordForUser:(NSString *)username {
if (![username isEqualToString:lastCheckedTwitterUsername]) {
twitterPasswordData = nil;
@synchronized(self) {
SecKeychainFindInternetPassword(NULL, 11, "twitter.com", 0, NULL,
[username lengthOfBytesUsingEncoding:NSASCIIStringEncoding],
[username cStringUsingEncoding:NSASCIIStringEncoding], 1, "/",
0, kSecProtocolTypeHTTPS, kSecAuthenticationTypeDefault,
&twitterPasswordLength, &twitterPasswordData, NULL);
}
}
NSString *password = [[NSString alloc] initWithBytes:twitterPasswordData
length:twitterPasswordLength
encoding:NSASCIIStringEncoding];
return password;
}
// Returns the previously retrieved password.
- (NSString *) fetchedPassword {
NSString *password = [[NSString alloc] initWithBytes:passwordData
length:passwordLength
encoding:NSASCIIStringEncoding];
return password;
}
// Frees the data buffer.
- (void) freeBuffer {
@synchronized(self) {
SecKeychainItemFreeContent(NULL, passwordData);
SecKeychainItemFreeContent(NULL, twitterPasswordData);
}
}
@end