OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library oauth2; | 5 library oauth2; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 import 'dart:uri'; | 9 import 'dart:uri'; |
10 | 10 |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 var grant = new AuthorizationCodeGrant( | 163 var grant = new AuthorizationCodeGrant( |
164 _identifier, | 164 _identifier, |
165 _secret, | 165 _secret, |
166 _authorizationEndpoint, | 166 _authorizationEndpoint, |
167 tokenEndpoint, | 167 tokenEndpoint, |
168 httpClient: httpClient); | 168 httpClient: httpClient); |
169 | 169 |
170 // Spin up a one-shot HTTP server to receive the authorization code from the | 170 // Spin up a one-shot HTTP server to receive the authorization code from the |
171 // Google OAuth2 server via redirect. This server will close itself as soon as | 171 // Google OAuth2 server via redirect. This server will close itself as soon as |
172 // the code is received. | 172 // the code is received. |
173 var completer = new Completer(); | 173 return HttpServer.bind('127.0.0.1', 0).then((server) { |
174 var server = new HttpServer(); | 174 var authUrl = grant.getAuthorizationUrl( |
175 server.addRequestHandler((request) => request.path == "/", | 175 Uri.parse('http://localhost:${server.port}'), scopes: _scopes); |
176 (request, response) { | |
177 chainToCompleter(defer(() { | |
178 log.message('Authorization received, processing...'); | |
179 var queryString = request.queryString; | |
180 if (queryString == null) queryString = ''; | |
181 response.statusCode = 302; | |
182 response.headers.set('location', 'http://pub.dartlang.org/authorized'); | |
183 response.outputStream.close(); | |
184 return grant.handleAuthorizationResponse(queryToMap(queryString)); | |
185 }).then((client) { | |
186 server.close(); | |
187 return client; | |
188 }), completer); | |
189 }); | |
190 server.listen('127.0.0.1', 0); | |
191 | 176 |
192 var authUrl = grant.getAuthorizationUrl( | 177 log.message( |
193 Uri.parse('http://localhost:${server.port}'), scopes: _scopes); | 178 'Pub needs your authorization to upload packages on your behalf.\n' |
194 | 179 'In a web browser, go to $authUrl\n' |
195 log.message( | 180 'Then click "Allow access".\n\n' |
196 'Pub needs your authorization to upload packages on your behalf.\n' | 181 'Waiting for your authorization...'); |
197 'In a web browser, go to $authUrl\n' | 182 return server.first.then((request) { |
198 'Then click "Allow access".\n\n' | 183 var response = request.response; |
199 'Waiting for your authorization...'); | 184 if (request.uri.path == "/") { |
200 | 185 log.message('Authorization received, processing...'); |
201 return completer.future.then((client) { | 186 var queryString = request.uri.query; |
| 187 if (queryString == null) queryString = ''; |
| 188 response.statusCode = 302; |
| 189 response.headers.set('location', |
| 190 'http://pub.dartlang.org/authorized'); |
| 191 response.close(); |
| 192 return grant.handleAuthorizationResponse(queryToMap(queryString)) |
| 193 .then((client) { |
| 194 server.close(); |
| 195 return client; |
| 196 }); |
| 197 } else { |
| 198 response.statusCode = 404; |
| 199 response.close(); |
| 200 } |
| 201 }); |
| 202 }) |
| 203 .then((client) { |
202 log.message('Successfully authorized.\n'); | 204 log.message('Successfully authorized.\n'); |
203 return client; | 205 return client; |
204 }); | 206 }); |
205 } | 207 } |
OLD | NEW |