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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 } else { | 104 } else { |
105 throw error; | 105 throw error; |
106 } | 106 } |
107 }); | 107 }); |
108 } | 108 } |
109 | 109 |
110 /// Gets a new OAuth2 client. | 110 /// Gets a new OAuth2 client. |
111 /// | 111 /// |
112 /// If saved credentials are available, those are used; otherwise, the user is | 112 /// If saved credentials are available, those are used; otherwise, the user is |
113 /// prompted to authorize the pub client. | 113 /// prompted to authorize the pub client. |
114 Future<Client> _getClient(SystemCache cache) { | 114 Future<Client> _getClient(SystemCache cache) async { |
115 return new Future.sync(() { | 115 var credentials = _loadCredentials(cache); |
116 var credentials = _loadCredentials(cache); | 116 if (credentials == null) return await _authorize(); |
117 if (credentials == null) return _authorize(); | |
118 | 117 |
119 var client = new Client(_identifier, _secret, credentials, | 118 var client = new Client(credentials, |
120 httpClient: httpClient); | 119 identifier: _identifier, |
121 _saveCredentials(cache, client.credentials); | 120 secret: _secret, |
122 return client; | 121 // Google's OAuth2 API doesn't support basic auth. |
123 }); | 122 basicAuth: false, |
| 123 httpClient: httpClient); |
| 124 _saveCredentials(cache, client.credentials); |
| 125 return client; |
124 } | 126 } |
125 | 127 |
126 /// Loads the user's OAuth2 credentials from the in-memory cache or the | 128 /// Loads the user's OAuth2 credentials from the in-memory cache or the |
127 /// filesystem if possible. | 129 /// filesystem if possible. |
128 /// | 130 /// |
129 /// If the credentials can't be loaded for any reason, the returned [Future] | 131 /// If the credentials can't be loaded for any reason, the returned [Future] |
130 /// completes to `null`. | 132 /// completes to `null`. |
131 Credentials _loadCredentials(SystemCache cache) { | 133 Credentials _loadCredentials(SystemCache cache) { |
132 log.fine('Loading OAuth2 credentials.'); | 134 log.fine('Loading OAuth2 credentials.'); |
133 | 135 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 /// The path to the file in which the user's OAuth2 credentials are stored. | 167 /// The path to the file in which the user's OAuth2 credentials are stored. |
166 String _credentialsFile(SystemCache cache) => | 168 String _credentialsFile(SystemCache cache) => |
167 path.join(cache.rootDir, 'credentials.json'); | 169 path.join(cache.rootDir, 'credentials.json'); |
168 | 170 |
169 /// Gets the user to authorize pub as a client of pub.dartlang.org via oauth2. | 171 /// Gets the user to authorize pub as a client of pub.dartlang.org via oauth2. |
170 /// | 172 /// |
171 /// Returns a Future that completes to a fully-authorized [Client]. | 173 /// Returns a Future that completes to a fully-authorized [Client]. |
172 Future<Client> _authorize() { | 174 Future<Client> _authorize() { |
173 var grant = new AuthorizationCodeGrant( | 175 var grant = new AuthorizationCodeGrant( |
174 _identifier, | 176 _identifier, |
175 _secret, | |
176 authorizationEndpoint, | 177 authorizationEndpoint, |
177 tokenEndpoint, | 178 tokenEndpoint, |
| 179 secret: _secret, |
| 180 // Google's OAuth2 API doesn't support basic auth. |
| 181 basicAuth: false, |
178 httpClient: httpClient); | 182 httpClient: httpClient); |
179 | 183 |
180 // Spin up a one-shot HTTP server to receive the authorization code from the | 184 // Spin up a one-shot HTTP server to receive the authorization code from the |
181 // Google OAuth2 server via redirect. This server will close itself as soon as | 185 // Google OAuth2 server via redirect. This server will close itself as soon as |
182 // the code is received. | 186 // the code is received. |
183 var completer = new Completer(); | 187 var completer = new Completer(); |
184 bindServer('localhost', 0).then((server) { | 188 bindServer('localhost', 0).then((server) { |
185 shelf_io.serveRequests(server, (request) { | 189 shelf_io.serveRequests(server, (request) { |
186 if (request.url.path.isNotEmpty) { | 190 if (request.url.path.isNotEmpty) { |
187 return new shelf.Response.notFound('Invalid URI.'); | 191 return new shelf.Response.notFound('Invalid URI.'); |
(...skipping 20 matching lines...) Expand all Loading... |
208 'In a web browser, go to $authUrl\n' | 212 'In a web browser, go to $authUrl\n' |
209 'Then click "Allow access".\n\n' | 213 'Then click "Allow access".\n\n' |
210 'Waiting for your authorization...'); | 214 'Waiting for your authorization...'); |
211 }); | 215 }); |
212 | 216 |
213 return completer.future.then((client) { | 217 return completer.future.then((client) { |
214 log.message('Successfully authorized.\n'); | 218 log.message('Successfully authorized.\n'); |
215 return client; | 219 return client; |
216 }); | 220 }); |
217 } | 221 } |
OLD | NEW |