This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
SFHFKeychainUtils.m
119 lines (97 loc) · 3.19 KB
/
SFHFKeychainUtils.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
//
// SFHFKeychainUtils.m
// Delicious Client
//
// Created by Laurence Andersen on Wed Oct 13 2004.
// Copyright (c) 2004 Sci-Fi Hi-Fi. All rights reserved.
//
#import "SFHFKeychainUtils.h"
@implementation SFHFKeychainUtils
+ (NSString *) getWebPasswordForUser: (NSString *) username URL: (NSURL *) url domain: (NSString *) domain itemReference: (SecKeychainItemRef *) itemRef {
const char *host = [[url host] UTF8String];
//const char *path = [[url path] UTF8String];
const char *user = [username UTF8String];
const char *dom = [domain UTF8String];
//UInt16 port = [[url port] shortValue];
void *password = NULL;
UInt32 passwordLength = 0;
OSStatus findResult = SecKeychainFindInternetPassword (
NULL, // default keychain
strlen(host), // server name length
host, // server name
strlen(dom), // security domain length
dom, // security domain
strlen(user), // account name length
user, // account name
0, // path length
NULL, // path
0, // port
kSecProtocolTypeHTTP, // protocol
kSecAuthenticationTypeDefault, // authentication type
&passwordLength, // password length
&password, // password
itemRef // item ref
);
if (findResult == noErr) {
NSString *returnString = [NSString stringWithCString: password length: passwordLength];
SecKeychainItemFreeContent(NULL, password);
return returnString;
}
return nil;
}
+ (BOOL) addWebPassword: (NSString *) password forUser: (NSString *) username URL: (NSURL *) url domain: (NSString *) domain {
const char *host = [[url host] UTF8String];
//const char *path = [[url path] UTF8String];
const char *user = [username UTF8String];
const char *pass = [password UTF8String];
const char *dom = [domain UTF8String];
UInt16 port = [[url port] shortValue];
SecKeychainItemRef itemRef;
NSString *currentPassword = [SFHFKeychainUtils getWebPasswordForUser: username URL: url domain: domain itemReference: &itemRef];
if (currentPassword) {
if ([currentPassword isEqualToString: password]) {
return YES;
}
return [self changePasswordForItem: itemRef to: password];
}
OSStatus addResult = SecKeychainAddInternetPassword (
NULL, // default keychain
strlen(host), // server name length
host, // server name
strlen(dom), // security domain length
dom, // security domain
strlen(user), // account name length
user, // account name
0, // path length
NULL, // path
port, // port
kSecProtocolTypeHTTP, // protocol
kSecAuthenticationTypeDefault, // authentication type
strlen(pass), // password length
pass, // password
NULL // item ref
);
if (addResult == noErr) {
return YES;
}
return NO;
}
+ (BOOL) changePasswordForItem: (SecKeychainItemRef) itemRef to: (NSString *) password {
if (!password || !itemRef) {
return NO;
}
const char *pass = [password cStringUsingEncoding: [NSString defaultCStringEncoding]];
NSLog(@"changing password");
/*OSErr status = SecKeychainItemModifyAttributesAndData (
itemRef, // the item reference
NULL, // no change to attributes
strlen(pass), // length of password
pass // pointer to password data
);*/
OSErr status = SecKeychainItemModifyContent(itemRef, nil, strlen(pass), pass);
if (status == noErr) {
return YES;
}
return NO;
}
@end