-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimpleHttpClientWSSE.m
More file actions
103 lines (83 loc) · 2.8 KB
/
SimpleHttpClientWSSE.m
File metadata and controls
103 lines (83 loc) · 2.8 KB
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
#import "SimpleHttpClientWSSE.h"
#import "NSString+EncodingSHA1.h"
#import "NSData+EncodingBase64.h"
@implementation SimpleHttpClientWSSE
//----------------------------------------------------------------------------//
#pragma mark -- Initialize --
//----------------------------------------------------------------------------//
- (id)init
{
if (![super init]) {
return nil;
}
_credentials = [[NSMutableDictionary alloc] init];
_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:sszzz"];
[_dateFormatter setLocale:[
[[NSLocale alloc] initWithLocaleIdentifier:@"US"] autorelease
]];
return self;
}
- (void)dealloc
{
[_credentials release], _credentials = nil;
[_dateFormatter release], _dateFormatter = nil;
[super dealloc];
}
//----------------------------------------------------------------------------//
#pragma mark -- APIs --
//----------------------------------------------------------------------------//
- (void)setCredentialForHost:(NSString *)host
username:(NSString *)username
password:(NSString *)password
{
NSArray *username_password = [_credentials objectForKey:host];
if (username_password) {
[self removeCredentialForHost:host];
}
username_password = [NSArray arrayWithObjects:username, password, nil];
[_credentials setObject:username_password forKey:host];
}
- (void)removeCredentialForHost:(NSString *)host
{
[_credentials removeObjectForKey:host];
}
-(NSString *)valueForHost:(NSString *)host
{
NSArray *username_password = [_credentials objectForKey:host];
if (!username_password) {
return nil;
}
NSString *username = [username_password objectAtIndex:0];
NSString *password = [username_password objectAtIndex:1];
NSString *created = [_dateFormatter stringFromDate:[NSDate date]];
srand(time(nil));
NSString *tmpNonce = [
[NSString stringWithFormat:@"%@%d", created, rand()]
stringByEncodingSHA1Hex
];
NSString *passwordDigest = [
[[NSString stringWithFormat:@"%@%@%@", tmpNonce, created, password]
dataByEncodingSHA1
]
stringByEncodingBase64
];
NSString *nonce = [[tmpNonce dataUsingEncoding:NSASCIIStringEncoding]
stringByEncodingBase64
];
return [NSString
stringWithFormat:@"UsernameToken Username=\"%@\", "
@"PasswordDigest=\"%@=\", "
@"Nonce=\"%@\", Created=\"%@\"",
username, passwordDigest, nonce, created
];
}
- (NSDictionary *)headerForHost:(NSString *)host;
{
NSString *value = [self valueForHost:host];
if (!value) {
return nil;
}
return [NSDictionary dictionaryWithObject:value forKey:@"X-WSSE"];
}
@end