-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.m
123 lines (99 loc) · 3.65 KB
/
User.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
#import "User.h"
#import "Session.h"
@interface User ()
- (void)loadCredentialsFromKeychain;
- (NSURLProtectionSpace *)protectionSpace;
@end
@implementation User
@synthesize login;
@synthesize password;
@synthesize siteURL;
#pragma mark -
#pragma mark Memory management
- (void)dealloc {
[login release];
[password release];
[siteURL release];
[super dealloc];
}
+ (User *)currentUserForSite:(NSURL *)aSiteURL {
User *user = [[[self alloc] init] autorelease];
user.siteURL = aSiteURL;
[user loadCredentialsFromKeychain];
return user;
}
- (BOOL)hasCredentials {
return (self.login != nil && self.password != nil);
}
- (BOOL)authenticate:(NSError **)error {
if (![self hasCredentials]) {
NSLog(@"User does not have cred");
return NO;
}
Session *session = [[[Session alloc] init] autorelease];
session.login = self.login;
session.password = self.password;
NSLog(@"User has street cred");
return YES;
}
- (void)saveCredentialsToKeychain {
NSURLCredential *credentials =
[NSURLCredential credentialWithUser:self.login
password:self.password
persistence:NSURLCredentialPersistencePermanent];
[[NSURLCredentialStorage sharedCredentialStorage]
setDefaultCredential:credentials forProtectionSpace:[self protectionSpace]];
}
#pragma mark -
#pragma mark Key-value observing
- (void)addObserver:(id)observer {
[self addObserver:observer forKeyPath:kUserLoginKey options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:observer forKeyPath:kUserPasswordKey options:NSKeyValueObservingOptionNew context:nil];
}
- (void)removeObserver:(id)observer {
[self removeObserver:observer forKeyPath:kUserLoginKey];
[self removeObserver:observer forKeyPath:kUserPasswordKey];
}
-(id)valueForKey:(NSString *)key {
if ([key isEqualToString:@"login"]) {
if (login == nil)
return [NSNull null];
return login;
}
if ([key isEqualToString:@"password"]) {
if (password == nil)
return [NSNull null];
return password;
}
@throw [NSException exceptionWithName:NSUndefinedKeyException reason:[NSString stringWithFormat:@"key '%@' not found", key] userInfo:nil];
return nil;
}
-(NSArray *)allKeys {
return [NSArray arrayWithObjects:@"login", @"password", nil];
}
-(NSArray *)allValues {
return [NSArray arrayWithObjects:login, password, nil];
}
#pragma mark -
#pragma mark Private methods
- (void)loadCredentialsFromKeychain {
NSDictionary *credentialInfo = [[NSURLCredentialStorage sharedCredentialStorage] credentialsForProtectionSpace:[self protectionSpace]];
// Assumes there's only one set of credentials, and since we
// don't have the username key in hand, we pull the first key.
NSArray *keys = [credentialInfo allKeys];
if ([keys count] > 0) {
//NSString *userNameKey = [[credentialInfo allKeys] objectAtIndex:0];
//NSURLCredential *credential = [credentialInfo valueForKey:userNameKey];
NSURLCredential *credential = [[ NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:[self protectionSpace]];
self.login = credential.user;
self.password = credential.password;
}
}
- (NSURLProtectionSpace *)protectionSpace {
return [[[NSURLProtectionSpace alloc] initWithHost:[siteURL host]
port:[[siteURL port] intValue]
protocol:[siteURL scheme]
realm:@"Web Password"
authenticationMethod:NSURLAuthenticationMethodDefault] autorelease];
}
@end