| 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 pub.oauth2; | 5 library pub.oauth2; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:oauth2/oauth2.dart'; | 10 import 'package:oauth2/oauth2.dart'; |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 var grant = new AuthorizationCodeGrant( | 162 var grant = new AuthorizationCodeGrant( |
| 163 _identifier, | 163 _identifier, |
| 164 _secret, | 164 _secret, |
| 165 authorizationEndpoint, | 165 authorizationEndpoint, |
| 166 tokenEndpoint, | 166 tokenEndpoint, |
| 167 httpClient: httpClient); | 167 httpClient: httpClient); |
| 168 | 168 |
| 169 // Spin up a one-shot HTTP server to receive the authorization code from the | 169 // Spin up a one-shot HTTP server to receive the authorization code from the |
| 170 // Google OAuth2 server via redirect. This server will close itself as soon as | 170 // Google OAuth2 server via redirect. This server will close itself as soon as |
| 171 // the code is received. | 171 // the code is received. |
| 172 var server; | |
| 173 var completer = new Completer(); | 172 var completer = new Completer(); |
| 174 shelf_io.serve((request) { | 173 bindServer('localhost', 0).then((server) { |
| 175 if (request.url.path != "/") { | 174 shelf_io.serveRequests(server, (request) { |
| 176 return new shelf.Response.notFound('Invalid URI.'); | 175 if (request.url.path != "/") { |
| 177 } | 176 return new shelf.Response.notFound('Invalid URI.'); |
| 177 } |
| 178 | 178 |
| 179 log.message('Authorization received, processing...'); | 179 log.message('Authorization received, processing...'); |
| 180 var queryString = request.url.query; | 180 var queryString = request.url.query; |
| 181 if (queryString == null) queryString = ''; | 181 if (queryString == null) queryString = ''; |
| 182 | 182 |
| 183 // Closing the server here is safe, since it will wait until the response is | 183 // Closing the server here is safe, since it will wait until the response |
| 184 // sent to actually shut down. | 184 // is sent to actually shut down. |
| 185 server.close(); | 185 server.close(); |
| 186 chainToCompleter(grant.handleAuthorizationResponse(queryToMap(queryString)), | 186 chainToCompleter(grant.handleAuthorizationResponse(queryToMap(queryString)
), |
| 187 completer); | 187 completer); |
| 188 | 188 |
| 189 return new shelf.Response.found('http://pub.dartlang.org/authorized'); | 189 return new shelf.Response.found('http://pub.dartlang.org/authorized'); |
| 190 }, '127.0.0.1', 0).then((server_) { | 190 }); |
| 191 server = server_; | |
| 192 | 191 |
| 193 var authUrl = grant.getAuthorizationUrl( | 192 var authUrl = grant.getAuthorizationUrl( |
| 194 Uri.parse('http://127.0.0.1:${server.port}'), scopes: _scopes); | 193 Uri.parse('http://localhost:${server.port}'), scopes: _scopes); |
| 195 | 194 |
| 196 log.message( | 195 log.message( |
| 197 'Pub needs your authorization to upload packages on your behalf.\n' | 196 'Pub needs your authorization to upload packages on your behalf.\n' |
| 198 'In a web browser, go to $authUrl\n' | 197 'In a web browser, go to $authUrl\n' |
| 199 'Then click "Allow access".\n\n' | 198 'Then click "Allow access".\n\n' |
| 200 'Waiting for your authorization...'); | 199 'Waiting for your authorization...'); |
| 201 }); | 200 }); |
| 202 | 201 |
| 203 return completer.future.then((client) { | 202 return completer.future.then((client) { |
| 204 log.message('Successfully authorized.\n'); | 203 log.message('Successfully authorized.\n'); |
| 205 return client; | 204 return client; |
| 206 }); | 205 }); |
| 207 } | 206 } |
| OLD | NEW |