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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 } | 90 } |
91 }); | 91 }); |
92 } | 92 } |
93 | 93 |
94 /// Gets a new OAuth2 client. If saved credentials are available, those are | 94 /// Gets a new OAuth2 client. If saved credentials are available, those are |
95 /// used; otherwise, the user is prompted to authorize the pub client. | 95 /// used; otherwise, the user is prompted to authorize the pub client. |
96 Future<Client> _getClient(SystemCache cache) { | 96 Future<Client> _getClient(SystemCache cache) { |
97 return _loadCredentials(cache).then((credentials) { | 97 return _loadCredentials(cache).then((credentials) { |
98 if (credentials == null) return _authorize(); | 98 if (credentials == null) return _authorize(); |
99 return new Client(_identifier, _secret, credentials, | 99 return new Client(_identifier, _secret, credentials, |
100 httpClient: curlClient); | 100 httpClient: httpClient); |
101 }).then((client) { | 101 }).then((client) { |
102 return _saveCredentials(cache, client.credentials).then((_) => client); | 102 return _saveCredentials(cache, client.credentials).then((_) => client); |
103 }); | 103 }); |
104 } | 104 } |
105 | 105 |
106 /// Loads the user's OAuth2 credentials from the in-memory cache or the | 106 /// Loads the user's OAuth2 credentials from the in-memory cache or the |
107 /// filesystem if possible. If the credentials can't be loaded for any reason, | 107 /// filesystem if possible. If the credentials can't be loaded for any reason, |
108 /// the returned [Future] will complete to null. | 108 /// the returned [Future] will complete to null. |
109 Future<Credentials> _loadCredentials(SystemCache cache) { | 109 Future<Credentials> _loadCredentials(SystemCache cache) { |
110 log.fine('Loading OAuth2 credentials.'); | 110 log.fine('Loading OAuth2 credentials.'); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 tokenEndpoint = Uri.parse(tokenEndpoint); | 161 tokenEndpoint = Uri.parse(tokenEndpoint); |
162 } else { | 162 } else { |
163 tokenEndpoint = _tokenEndpoint; | 163 tokenEndpoint = _tokenEndpoint; |
164 } | 164 } |
165 | 165 |
166 var grant = new AuthorizationCodeGrant( | 166 var grant = new AuthorizationCodeGrant( |
167 _identifier, | 167 _identifier, |
168 _secret, | 168 _secret, |
169 _authorizationEndpoint, | 169 _authorizationEndpoint, |
170 tokenEndpoint, | 170 tokenEndpoint, |
171 httpClient: curlClient); | 171 httpClient: httpClient); |
172 | 172 |
173 // Spin up a one-shot HTTP server to receive the authorization code from the | 173 // Spin up a one-shot HTTP server to receive the authorization code from the |
174 // Google OAuth2 server via redirect. This server will close itself as soon as | 174 // Google OAuth2 server via redirect. This server will close itself as soon as |
175 // the code is received. | 175 // the code is received. |
176 var completer = new Completer(); | 176 var completer = new Completer(); |
177 var server = new HttpServer(); | 177 var server = new HttpServer(); |
178 server.addRequestHandler((request) => request.path == "/", | 178 server.addRequestHandler((request) => request.path == "/", |
179 (request, response) { | 179 (request, response) { |
180 chainToCompleter(new Future.immediate(null).then((_) { | 180 chainToCompleter(new Future.immediate(null).then((_) { |
181 log.message('Authorization received, processing...'); | 181 log.message('Authorization received, processing...'); |
(...skipping 17 matching lines...) Expand all Loading... |
199 'Pub needs your authorization to upload packages on your behalf.\n' | 199 'Pub needs your authorization to upload packages on your behalf.\n' |
200 'In a web browser, go to $authUrl\n' | 200 'In a web browser, go to $authUrl\n' |
201 'Then click "Allow access".\n\n' | 201 'Then click "Allow access".\n\n' |
202 'Waiting for your authorization...'); | 202 'Waiting for your authorization...'); |
203 | 203 |
204 return completer.future.then((client) { | 204 return completer.future.then((client) { |
205 log.message('Successfully authorized.\n'); | 205 log.message('Successfully authorized.\n'); |
206 return client; | 206 return client; |
207 }); | 207 }); |
208 } | 208 } |
OLD | NEW |