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 17 matching lines...) Expand all Loading... |
28 /// secret. | 28 /// secret. |
29 final _secret = 'SWeqj8seoJW0w7_CpEPFLX0K'; | 29 final _secret = 'SWeqj8seoJW0w7_CpEPFLX0K'; |
30 | 30 |
31 /// The URL to which the user will be directed to authorize the pub client to | 31 /// The URL to which the user will be directed to authorize the pub client to |
32 /// get an OAuth2 access token. | 32 /// get an OAuth2 access token. |
33 /// | 33 /// |
34 /// `access_type=offline` and `approval_prompt=force` ensures that we always get | 34 /// `access_type=offline` and `approval_prompt=force` ensures that we always get |
35 /// a refresh token from the server. See the [Google OAuth2 documentation][]. | 35 /// a refresh token from the server. See the [Google OAuth2 documentation][]. |
36 /// | 36 /// |
37 /// [Google OAuth2 documentation]: https://developers.google.com/accounts/docs/O
Auth2WebServer#offline | 37 /// [Google OAuth2 documentation]: https://developers.google.com/accounts/docs/O
Auth2WebServer#offline |
38 final _authorizationEndpoint = Uri.parse( | 38 final authorizationEndpoint = Uri.parse( |
39 'https://accounts.google.com/o/oauth2/auth?access_type=offline' | 39 'https://accounts.google.com/o/oauth2/auth?access_type=offline' |
40 '&approval_prompt=force'); | 40 '&approval_prompt=force'); |
41 | 41 |
42 /// The URL from which the pub client will request an access token once it's | 42 /// The URL from which the pub client will request an access token once it's |
43 /// been authorized by the user. | 43 /// been authorized by the user. This can be controlled externally by setting |
44 final _tokenEndpoint = Uri.parse( | 44 /// the _PUB_TEST_TOKEN_ENDPOINT environment variable. |
45 'https://accounts.google.com/o/oauth2/token'); | 45 Uri get tokenEndpoint { |
| 46 var tokenEndpoint = Platform.environment['_PUB_TEST_TOKEN_ENDPOINT']; |
| 47 if (tokenEndpoint != null) { |
| 48 return Uri.parse(tokenEndpoint); |
| 49 } else { |
| 50 return _tokenEndpoint; |
| 51 } |
| 52 } |
| 53 |
| 54 final _tokenEndpoint = Uri.parse('https://accounts.google.com/o/oauth2/token'); |
46 | 55 |
47 /// The OAuth2 scopes that the pub client needs. Currently the client only needs | 56 /// The OAuth2 scopes that the pub client needs. Currently the client only needs |
48 /// the user's email so that the server can verify their identity. | 57 /// the user's email so that the server can verify their identity. |
49 final _scopes = ['https://www.googleapis.com/auth/userinfo.email']; | 58 final _scopes = ['https://www.googleapis.com/auth/userinfo.email']; |
50 | 59 |
51 /// An in-memory cache of the user's OAuth2 credentials. This should always be | 60 /// An in-memory cache of the user's OAuth2 credentials. This should always be |
52 /// the same as the credentials file stored in the system cache. | 61 /// the same as the credentials file stored in the system cache. |
53 Credentials _credentials; | 62 Credentials _credentials; |
54 | 63 |
55 /// Delete the cached credentials, if they exist. | 64 /// Delete the cached credentials, if they exist. |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 writeTextFile(credentialsPath, credentials.toJson(), dontLogContents: true); | 154 writeTextFile(credentialsPath, credentials.toJson(), dontLogContents: true); |
146 } | 155 } |
147 | 156 |
148 /// The path to the file in which the user's OAuth2 credentials are stored. | 157 /// The path to the file in which the user's OAuth2 credentials are stored. |
149 String _credentialsFile(SystemCache cache) => | 158 String _credentialsFile(SystemCache cache) => |
150 path.join(cache.rootDir, 'credentials.json'); | 159 path.join(cache.rootDir, 'credentials.json'); |
151 | 160 |
152 /// Gets the user to authorize pub as a client of pub.dartlang.org via oauth2. | 161 /// Gets the user to authorize pub as a client of pub.dartlang.org via oauth2. |
153 /// Returns a Future that will complete to a fully-authorized [Client]. | 162 /// Returns a Future that will complete to a fully-authorized [Client]. |
154 Future<Client> _authorize() { | 163 Future<Client> _authorize() { |
155 // Allow the tests to inject their own token endpoint URL. | |
156 var tokenEndpoint = Platform.environment['_PUB_TEST_TOKEN_ENDPOINT']; | |
157 if (tokenEndpoint != null) { | |
158 tokenEndpoint = Uri.parse(tokenEndpoint); | |
159 } else { | |
160 tokenEndpoint = _tokenEndpoint; | |
161 } | |
162 | |
163 var grant = new AuthorizationCodeGrant( | 164 var grant = new AuthorizationCodeGrant( |
164 _identifier, | 165 _identifier, |
165 _secret, | 166 _secret, |
166 _authorizationEndpoint, | 167 authorizationEndpoint, |
167 tokenEndpoint, | 168 tokenEndpoint, |
168 httpClient: httpClient); | 169 httpClient: httpClient); |
169 | 170 |
170 // Spin up a one-shot HTTP server to receive the authorization code from the | 171 // 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 | 172 // Google OAuth2 server via redirect. This server will close itself as soon as |
172 // the code is received. | 173 // the code is received. |
173 return HttpServer.bind('127.0.0.1', 0).then((server) { | 174 return HttpServer.bind('127.0.0.1', 0).then((server) { |
174 var authUrl = grant.getAuthorizationUrl( | 175 var authUrl = grant.getAuthorizationUrl( |
175 Uri.parse('http://localhost:${server.port}'), scopes: _scopes); | 176 Uri.parse('http://localhost:${server.port}'), scopes: _scopes); |
176 | 177 |
(...skipping 21 matching lines...) Expand all Loading... |
198 response.statusCode = 404; | 199 response.statusCode = 404; |
199 response.close(); | 200 response.close(); |
200 } | 201 } |
201 }); | 202 }); |
202 }) | 203 }) |
203 .then((client) { | 204 .then((client) { |
204 log.message('Successfully authorized.\n'); | 205 log.message('Successfully authorized.\n'); |
205 return client; | 206 return client; |
206 }); | 207 }); |
207 } | 208 } |
OLD | NEW |