-
Notifications
You must be signed in to change notification settings - Fork 840
Expand file tree
/
Copy pathOIDRedirectHTTPHandler.m
More file actions
177 lines (150 loc) · 6.51 KB
/
OIDRedirectHTTPHandler.m
File metadata and controls
177 lines (150 loc) · 6.51 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
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
/*! @file OIDRedirectHTTPHandler.m
@brief AppAuth iOS SDK
@copyright
Copyright 2016 Google Inc. All Rights Reserved.
@copydetails
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 <TargetConditionals.h>
#if TARGET_OS_OSX
#import "OIDRedirectHTTPHandler.h"
#import "OIDAuthorizationService.h"
#import "OIDErrorUtilities.h"
#import "OIDExternalUserAgentSession.h"
#import "OIDLoopbackHTTPServer.h"
/*! @brief Page that is returned following a completed authorization. Show your own page instead by
supplying a URL in @c initWithSuccessURL that the user will be redirected to.
*/
static NSString *const kHTMLAuthorizationComplete =
@"<html><body>Authorization complete.<br> Return to the app.</body></html>";
/*! @brief Error warning that the @c currentAuthorizationFlow is not set on this object (likely a
developer error, unless the user stumbled upon the loopback server before the authorization
had started completely).
@description An object conforming to @c OIDExternalUserAgentSession is returned when the
authorization is presented with
@c OIDAuthorizationService::presentAuthorizationRequest:callback:. It should be set to
@c currentAuthorization when using a loopback redirect.
*/
static NSString *const kHTMLErrorMissingCurrentAuthorizationFlow =
@"<html><body>AppAuth Error: No <code>currentAuthorizationFlow</code> is set on the "
"<code>OIDRedirectHTTPHandler</code>. Cannot process redirect.</body></html>";
/*! @brief Error warning that the URL does not represent a valid redirect. This should be rare, may
happen if the user stumbles upon the loopback server randomly.
*/
static NSString *const kHTMLErrorRedirectNotValid =
@"<html><body>AppAuth Error: Not a valid redirect.</body></html>";
@implementation OIDRedirectHTTPHandler {
HTTPServer *_httpServ;
NSURL *_successURL;
}
- (instancetype)init {
return [self initWithSuccessURL:nil];
}
- (instancetype)initWithSuccessURL:(nullable NSURL *)successURL {
self = [super init];
if (self) {
_successURL = [successURL copy];
}
return self;
}
- (NSURL *)startHTTPListenerWithPort:(uint16_t)port error:(NSError **)returnError {
// Cancels any pending requests.
[self cancelHTTPListener];
// Starts a HTTP server on the loopback interface.
// By not specifying a port, a random available one will be assigned.
_httpServ = [[HTTPServer alloc] init];
[_httpServ setPort:port];
[_httpServ setDelegate:self];
NSError *error = nil;
if (![_httpServ start:&error]) {
if (returnError) {
*returnError = error;
}
return nil;
} else if ([_httpServ hasIPv4Socket]) {
// Prefer the IPv4 loopback address
NSString *serverURL = [NSString stringWithFormat:@"http://127.0.0.1:%d/", [_httpServ port]];
return [NSURL URLWithString:serverURL];
} else if ([_httpServ hasIPv6Socket]) {
// Use the IPv6 loopback address if IPv4 isn't available
NSString *serverURL = [NSString stringWithFormat:@"http://[::1]:%d/", [_httpServ port]];
return [NSURL URLWithString:serverURL];
}
return nil;
}
- (NSURL *)startHTTPListener:(NSError **)returnError {
// A port of 0 requests a random available port
return [self startHTTPListenerWithPort:0 error:returnError];
}
- (void)cancelHTTPListener {
[self stopHTTPListener];
// Cancels the pending authorization flow (if any) with error.
NSError *cancelledError =
[OIDErrorUtilities errorWithCode:OIDErrorCodeProgramCanceledAuthorizationFlow
underlyingError:nil
description:@"The HTTP listener was cancelled programmatically."];
[_currentAuthorizationFlow failExternalUserAgentFlowWithError:cancelledError];
_currentAuthorizationFlow = nil;
}
/*! @brief Stops listening on the loopback interface without modifying the state of the
@c currentAuthorizationFlow. Should be called when the authorization flow completes or is
cancelled.
*/
- (void)stopHTTPListener {
_httpServ.delegate = nil;
[_httpServ stop];
_httpServ = nil;
}
- (void)HTTPConnection:(HTTPConnection *)conn didReceiveRequest:(HTTPServerRequest *)mess {
// Sends URL to AppAuth.
CFURLRef url = CFHTTPMessageCopyRequestURL(mess.request);
BOOL handled = [_currentAuthorizationFlow resumeExternalUserAgentFlowWithURL:(__bridge NSURL *)url];
// Stops listening to further requests after the first valid authorization response.
if (handled) {
_currentAuthorizationFlow = nil;
[self stopHTTPListener];
}
// Responds to browser request.
NSString *bodyText = kHTMLAuthorizationComplete;
NSInteger httpResponseCode = (_successURL) ? 302 : 200;
// Returns an error page if a URL other than the expected redirect is requested.
if (!handled) {
if (_currentAuthorizationFlow) {
bodyText = kHTMLErrorRedirectNotValid;
httpResponseCode = 404;
} else {
bodyText = kHTMLErrorMissingCurrentAuthorizationFlow;
httpResponseCode = 400;
}
}
NSData *data = [bodyText dataUsingEncoding:NSUTF8StringEncoding];
CFHTTPMessageRef response = CFHTTPMessageCreateResponse(kCFAllocatorDefault,
httpResponseCode,
NULL,
kCFHTTPVersion1_1);
if (httpResponseCode == 302) {
CFHTTPMessageSetHeaderFieldValue(response,
(__bridge CFStringRef)@"Location",
(__bridge CFStringRef)_successURL.absoluteString);
}
CFHTTPMessageSetHeaderFieldValue(response,
(__bridge CFStringRef)@"Content-Length",
(__bridge CFStringRef)[NSString stringWithFormat:@"%lu",
(unsigned long)data.length]);
CFHTTPMessageSetBody(response, (__bridge CFDataRef)data);
[mess setResponse:response];
CFRelease(response);
}
- (void)dealloc {
[self cancelHTTPListener];
}
@end
#endif // TARGET_OS_OSX