| 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:io'; | 7 import 'dart:io'; |
| 8 import 'dart:uri'; | 8 import 'dart:uri'; |
| 9 | 9 |
| 10 // TODO(nweiz): Make this a "package:" URL, or something nicer than this. | 10 // TODO(nweiz): Make this a "package:" URL, or something nicer than this. |
| 11 import '../../pkg/oauth2/lib/oauth2.dart'; | 11 import '../../pkg/oauth2/lib/oauth2.dart'; |
| 12 import 'curl_client.dart'; | |
| 13 import 'io.dart'; | 12 import 'io.dart'; |
| 14 import 'system_cache.dart'; | 13 import 'system_cache.dart'; |
| 15 import 'utils.dart'; | 14 import 'utils.dart'; |
| 16 | 15 |
| 17 export '../../pkg/oauth2/lib/oauth2.dart'; | 16 export '../../pkg/oauth2/lib/oauth2.dart'; |
| 18 | 17 |
| 19 /// The pub client's OAuth2 identifier. | 18 /// The pub client's OAuth2 identifier. |
| 20 final _identifier = '818368855108-8grd2eg9tj9f38os6f1urbcvsq399u8n.apps.' | 19 final _identifier = '818368855108-8grd2eg9tj9f38os6f1urbcvsq399u8n.apps.' |
| 21 'googleusercontent.com'; | 20 'googleusercontent.com'; |
| 22 | 21 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 return completer.future; | 73 return completer.future; |
| 75 }); | 74 }); |
| 76 } | 75 } |
| 77 | 76 |
| 78 /// Gets a new OAuth2 client. If saved credentials are available, those are | 77 /// Gets a new OAuth2 client. If saved credentials are available, those are |
| 79 /// used; otherwise, the user is prompted to authorize the pub client. | 78 /// used; otherwise, the user is prompted to authorize the pub client. |
| 80 Future<Client> _getClient(SystemCache cache) { | 79 Future<Client> _getClient(SystemCache cache) { |
| 81 return _loadCredentials(cache).chain((credentials) { | 80 return _loadCredentials(cache).chain((credentials) { |
| 82 if (credentials == null) return _authorize(); | 81 if (credentials == null) return _authorize(); |
| 83 return new Future.immediate(new Client( | 82 return new Future.immediate(new Client( |
| 84 _identifier, _secret, credentials, httpClient: new CurlClient())); | 83 _identifier, _secret, credentials, httpClient: curlClient)); |
| 85 }).chain((client) { | 84 }).chain((client) { |
| 86 return _saveCredentials(cache, client.credentials).transform((_) => client); | 85 return _saveCredentials(cache, client.credentials).transform((_) => client); |
| 87 }); | 86 }); |
| 88 } | 87 } |
| 89 | 88 |
| 90 /// Loads the user's OAuth2 credentials from the in-memory cache or the | 89 /// Loads the user's OAuth2 credentials from the in-memory cache or the |
| 91 /// filesystem if possible. If the credentials can't be loaded for any reason, | 90 /// filesystem if possible. If the credentials can't be loaded for any reason, |
| 92 /// the returned [Future] will complete to null. | 91 /// the returned [Future] will complete to null. |
| 93 Future<Credentials> _loadCredentials(SystemCache cache) { | 92 Future<Credentials> _loadCredentials(SystemCache cache) { |
| 94 if (_credentials != null) return new Future.immediate(_credentials); | 93 if (_credentials != null) return new Future.immediate(_credentials); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 tokenEndpoint = new Uri.fromString(tokenEndpoint); | 134 tokenEndpoint = new Uri.fromString(tokenEndpoint); |
| 136 } else { | 135 } else { |
| 137 tokenEndpoint = _tokenEndpoint; | 136 tokenEndpoint = _tokenEndpoint; |
| 138 } | 137 } |
| 139 | 138 |
| 140 var grant = new AuthorizationCodeGrant( | 139 var grant = new AuthorizationCodeGrant( |
| 141 _identifier, | 140 _identifier, |
| 142 _secret, | 141 _secret, |
| 143 _authorizationEndpoint, | 142 _authorizationEndpoint, |
| 144 tokenEndpoint, | 143 tokenEndpoint, |
| 145 httpClient: new CurlClient()); | 144 httpClient: curlClient); |
| 146 | 145 |
| 147 // Spin up a one-shot HTTP server to receive the authorization code from the | 146 // Spin up a one-shot HTTP server to receive the authorization code from the |
| 148 // Google OAuth2 server via redirect. This server will close itself as soon as | 147 // Google OAuth2 server via redirect. This server will close itself as soon as |
| 149 // the code is received. | 148 // the code is received. |
| 150 var completer = new Completer(); | 149 var completer = new Completer(); |
| 151 var server = new HttpServer(); | 150 var server = new HttpServer(); |
| 152 server.addRequestHandler((request) => request.path == "/", | 151 server.addRequestHandler((request) => request.path == "/", |
| 153 (request, response) { | 152 (request, response) { |
| 154 chainToCompleter(new Future.immediate(null).chain((_) { | 153 chainToCompleter(new Future.immediate(null).chain((_) { |
| 155 print('Authorization received, processing...'); | 154 print('Authorization received, processing...'); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 174 print('Pub needs your authorization to upload packages on your behalf.\n' | 173 print('Pub needs your authorization to upload packages on your behalf.\n' |
| 175 'In a web browser, go to $authUrl\n' | 174 'In a web browser, go to $authUrl\n' |
| 176 'Then click "Allow access".\n\n' | 175 'Then click "Allow access".\n\n' |
| 177 'Waiting for your authorization...'); | 176 'Waiting for your authorization...'); |
| 178 | 177 |
| 179 return completer.future.transform((client) { | 178 return completer.future.transform((client) { |
| 180 print('Successfully authorized.\n'); | 179 print('Successfully authorized.\n'); |
| 181 return client; | 180 return client; |
| 182 }); | 181 }); |
| 183 } | 182 } |
| OLD | NEW |