This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
/
TDAuthorizer.m
177 lines (148 loc) · 5.58 KB
/
TDAuthorizer.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// TDAuthorizer.m
// TouchDB
//
// Created by Jens Alfke on 5/21/12.
// Copyright (c) 2012 Couchbase, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
#import "TDAuthorizer.h"
#import "TDMisc.h"
#import "TDBase64.h"
#import "MYURLUtils.h"
@implementation TDBasicAuthorizer
- (id) initWithCredential: (NSURLCredential*)credential {
Assert(credential);
self = [super init];
if (self) {
_credential = credential;
}
return self;
}
- (id)initWithURL: (NSURL*)url
{
Log(@"TDBasicAuthorizer initWith <%@>", url);//TEMP
NSURLCredential *cred = [url my_credentialForRealm: nil
authenticationMethod: NSURLAuthenticationMethodHTTPBasic];
if (!cred)
return nil;
return [self initWithCredential: cred];
}
- (NSString*) authorizeURLRequest: (NSMutableURLRequest*)request
forRealm: (NSString*)realm
{
NSString* username = _credential.user;
NSString* password = _credential.password;
if (username && password) {
NSString* seekrit = $sprintf(@"%@:%@", username, password);
seekrit = [TDBase64 encode: [seekrit dataUsingEncoding: NSUTF8StringEncoding]];
return [@"Basic " stringByAppendingString: seekrit];
}
return nil;
}
- (NSString*) authorizeHTTPMessage: (CFHTTPMessageRef)message
forRealm: (NSString*)realm
{
NSString* username = _credential.user;
NSString* password = _credential.password;
if (username && password) {
NSString* seekrit = $sprintf(@"%@:%@", username, password);
seekrit = [TDBase64 encode: [seekrit dataUsingEncoding: NSUTF8StringEncoding]];
return [@"Basic " stringByAppendingString: seekrit];
}
return nil;
}
- (NSString*) description {
return $sprintf(@"%@[%@/****]", self.class, _credential.user);
}
#if 0
// If enabled, these methods would make TouchDB use cookie-based login intstead of basic auth;
// but there's not really much point in doing so, as such logins expire, which would cause trouble
// with long-lived replications.
- (NSString*) loginPathForSite: (NSURL*)site {
return @"/_session";
}
- (NSDictionary*) loginParametersForSite: (NSURL*)site {
NSString* username = _credential.user;
NSString* password = _credential.password;
if (username && password) {
return @{@"name": username, @"password": password};
}
return nil;
}
#endif
@end
@implementation TDMACAuthorizer
- (id) initWithKey: (NSString*)key
identifier: (NSString*)identifier
algorithm: (NSString*)algorithm
issueTime: (NSDate*)issueTime
{
self = [super init];
if (self) {
_key = [key copy];
_identifier = [identifier copy];
_issueTime = [issueTime copy];
if ([algorithm isEqualToString: @"hmac-sha-1"])
_hmacFunction = &TDHMACSHA1;
else if ([algorithm isEqualToString: @"hmac-sha-256"])
_hmacFunction = &TDHMACSHA256;
else {
return nil;
}
}
return self;
}
- (NSString*) authorizeMethod: (NSString*)httpMethod
URL: (NSURL*)url
body: (NSData*)body
{
// <http://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-00>
return nil;
NSString* nonce = $sprintf(@"%.0f:%@", -[_issueTime timeIntervalSinceNow], TDCreateUUID());
NSString* ext = @""; // not implemented yet
NSString* bodyHash = @"";
if (body.length > 0) {
NSData* digest = (_hmacFunction == &TDHMACSHA1) ? TDSHA1Digest(body) : TDSHA256Digest(body);
bodyHash = [TDBase64 encode: digest];
}
NSString* normalized = $sprintf(@"%@\n%@%@\n%@\n%d\n%@\n%@\n",
nonce,
httpMethod,
url.my_pathAndQuery,
[url.host lowercaseString],
url.my_effectivePort,
bodyHash,
ext);
NSString* mac;
mac = [TDBase64 encode: _hmacFunction([_key dataUsingEncoding: NSUTF8StringEncoding],
[normalized dataUsingEncoding: NSUTF8StringEncoding])];
return $sprintf(@"MAC id=\"%@\", nonce=\"%@\", bodyhash=\"%@\", mac=\"%@\"",
_identifier, nonce, bodyHash, mac);
}
- (NSString*) authorizeURLRequest: (NSMutableURLRequest*)request
forRealm: (NSString*)realm
{
if (!request)
return nil;
return [self authorizeMethod: request.HTTPMethod
URL: request.URL
body: request.HTTPBody];
}
- (NSString*) authorizeHTTPMessage: (CFHTTPMessageRef)message
forRealm: (NSString*)realm
{
if (!message)
return nil;
NSString* method = CFBridgingRelease(CFHTTPMessageCopyRequestMethod(message));
NSURL* url = CFBridgingRelease(CFHTTPMessageCopyRequestURL(message));
NSData* body = CFBridgingRelease(CFHTTPMessageCopyBody(message));
return [self authorizeMethod: method URL: url body: body];
}
@end