| 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 15 matching lines...) Expand all Loading... |
| 26 /// secret. | 26 /// secret. |
| 27 final _secret = 'SWeqj8seoJW0w7_CpEPFLX0K'; | 27 final _secret = 'SWeqj8seoJW0w7_CpEPFLX0K'; |
| 28 | 28 |
| 29 /// The URL to which the user will be directed to authorize the pub client to | 29 /// The URL to which the user will be directed to authorize the pub client to |
| 30 /// get an OAuth2 access token. | 30 /// get an OAuth2 access token. |
| 31 /// | 31 /// |
| 32 /// `access_type=offline` and `approval_prompt=force` ensures that we always get | 32 /// `access_type=offline` and `approval_prompt=force` ensures that we always get |
| 33 /// a refresh token from the server. See the [Google OAuth2 documentation][]. | 33 /// a refresh token from the server. See the [Google OAuth2 documentation][]. |
| 34 /// | 34 /// |
| 35 /// [Google OAuth2 documentation]: https://developers.google.com/accounts/docs/O
Auth2WebServer#offline | 35 /// [Google OAuth2 documentation]: https://developers.google.com/accounts/docs/O
Auth2WebServer#offline |
| 36 final _authorizationEndpoint = new Uri.fromString( | 36 final _authorizationEndpoint = Uri.parse( |
| 37 'https://accounts.google.com/o/oauth2/auth?access_type=offline' | 37 'https://accounts.google.com/o/oauth2/auth?access_type=offline' |
| 38 '&approval_prompt=force'); | 38 '&approval_prompt=force'); |
| 39 | 39 |
| 40 /// The URL from which the pub client will request an access token once it's | 40 /// The URL from which the pub client will request an access token once it's |
| 41 /// been authorized by the user. | 41 /// been authorized by the user. |
| 42 final _tokenEndpoint = new Uri.fromString( | 42 final _tokenEndpoint = Uri.parse( |
| 43 'https://accounts.google.com/o/oauth2/token'); | 43 'https://accounts.google.com/o/oauth2/token'); |
| 44 | 44 |
| 45 /// The OAuth2 scopes that the pub client needs. Currently the client only needs | 45 /// The OAuth2 scopes that the pub client needs. Currently the client only needs |
| 46 /// the user's email so that the server can verify their identity. | 46 /// the user's email so that the server can verify their identity. |
| 47 final _scopes = ['https://www.googleapis.com/auth/userinfo.email']; | 47 final _scopes = ['https://www.googleapis.com/auth/userinfo.email']; |
| 48 | 48 |
| 49 /// An in-memory cache of the user's OAuth2 credentials. This should always be | 49 /// An in-memory cache of the user's OAuth2 credentials. This should always be |
| 50 /// the same as the credentials file stored in the system cache. | 50 /// the same as the credentials file stored in the system cache. |
| 51 Credentials _credentials; | 51 Credentials _credentials; |
| 52 | 52 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 /// The path to the file in which the user's OAuth2 credentials are stored. | 152 /// The path to the file in which the user's OAuth2 credentials are stored. |
| 153 String _credentialsFile(SystemCache cache) => | 153 String _credentialsFile(SystemCache cache) => |
| 154 join(cache.rootDir, 'credentials.json'); | 154 join(cache.rootDir, 'credentials.json'); |
| 155 | 155 |
| 156 /// Gets the user to authorize pub as a client of pub.dartlang.org via oauth2. | 156 /// Gets the user to authorize pub as a client of pub.dartlang.org via oauth2. |
| 157 /// Returns a Future that will complete to a fully-authorized [Client]. | 157 /// Returns a Future that will complete to a fully-authorized [Client]. |
| 158 Future<Client> _authorize() { | 158 Future<Client> _authorize() { |
| 159 // Allow the tests to inject their own token endpoint URL. | 159 // Allow the tests to inject their own token endpoint URL. |
| 160 var tokenEndpoint = Platform.environment['_PUB_TEST_TOKEN_ENDPOINT']; | 160 var tokenEndpoint = Platform.environment['_PUB_TEST_TOKEN_ENDPOINT']; |
| 161 if (tokenEndpoint != null) { | 161 if (tokenEndpoint != null) { |
| 162 tokenEndpoint = new Uri.fromString(tokenEndpoint); | 162 tokenEndpoint = Uri.parse(tokenEndpoint); |
| 163 } else { | 163 } else { |
| 164 tokenEndpoint = _tokenEndpoint; | 164 tokenEndpoint = _tokenEndpoint; |
| 165 } | 165 } |
| 166 | 166 |
| 167 var grant = new AuthorizationCodeGrant( | 167 var grant = new AuthorizationCodeGrant( |
| 168 _identifier, | 168 _identifier, |
| 169 _secret, | 169 _secret, |
| 170 _authorizationEndpoint, | 170 _authorizationEndpoint, |
| 171 tokenEndpoint, | 171 tokenEndpoint, |
| 172 httpClient: curlClient); | 172 httpClient: curlClient); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 187 response.outputStream.close(); | 187 response.outputStream.close(); |
| 188 return grant.handleAuthorizationResponse(queryToMap(queryString)); | 188 return grant.handleAuthorizationResponse(queryToMap(queryString)); |
| 189 }).then((client) { | 189 }).then((client) { |
| 190 server.close(); | 190 server.close(); |
| 191 return client; | 191 return client; |
| 192 }), completer); | 192 }), completer); |
| 193 }); | 193 }); |
| 194 server.listen('127.0.0.1', 0); | 194 server.listen('127.0.0.1', 0); |
| 195 | 195 |
| 196 var authUrl = grant.getAuthorizationUrl( | 196 var authUrl = grant.getAuthorizationUrl( |
| 197 new Uri.fromString('http://localhost:${server.port}'), scopes: _scopes); | 197 Uri.parse('http://localhost:${server.port}'), scopes: _scopes); |
| 198 | 198 |
| 199 log.message( | 199 log.message( |
| 200 'Pub needs your authorization to upload packages on your behalf.\n' | 200 'Pub needs your authorization to upload packages on your behalf.\n' |
| 201 'In a web browser, go to $authUrl\n' | 201 'In a web browser, go to $authUrl\n' |
| 202 'Then click "Allow access".\n\n' | 202 'Then click "Allow access".\n\n' |
| 203 'Waiting for your authorization...'); | 203 'Waiting for your authorization...'); |
| 204 | 204 |
| 205 return completer.future.then((client) { | 205 return completer.future.then((client) { |
| 206 log.message('Successfully authorized.\n'); | 206 log.message('Successfully authorized.\n'); |
| 207 return client; | 207 return client; |
| 208 }); | 208 }); |
| 209 } | 209 } |
| OLD | NEW |