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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 | 62 |
63 /// Asynchronously passes an OAuth2 [Client] to [fn], and closes the client when | 63 /// Asynchronously passes an OAuth2 [Client] to [fn], and closes the client when |
64 /// the [Future] returned by [fn] completes. | 64 /// the [Future] returned by [fn] completes. |
65 /// | 65 /// |
66 /// This takes care of loading and saving the client's credentials, as well as | 66 /// This takes care of loading and saving the client's credentials, as well as |
67 /// prompting the user for their authorization. It will also re-authorize and | 67 /// prompting the user for their authorization. It will also re-authorize and |
68 /// re-run [fn] if a recoverable authorization error is detected. | 68 /// re-run [fn] if a recoverable authorization error is detected. |
69 Future withClient(SystemCache cache, Future fn(Client client)) { | 69 Future withClient(SystemCache cache, Future fn(Client client)) { |
70 return _getClient(cache).then((client) { | 70 return _getClient(cache).then((client) { |
71 var completer = new Completer(); | 71 var completer = new Completer(); |
72 return asyncWhenComplete(fn(client), () { | 72 return fn(client).whenComplete(() { |
73 client.close(); | 73 client.close(); |
74 // Be sure to save the credentials even when an error happens. | 74 // Be sure to save the credentials even when an error happens. |
75 return _saveCredentials(cache, client.credentials); | 75 return _saveCredentials(cache, client.credentials); |
76 }); | 76 }); |
77 }).catchError((asyncError) { | 77 }).catchError((asyncError) { |
78 var e = getRealError(asyncError); | 78 var e = getRealError(asyncError); |
79 if (e is ExpirationException) { | 79 if (e is ExpirationException) { |
80 log.error("Pub's authorization to upload packages has expired and " | 80 log.error("Pub's authorization to upload packages has expired and " |
81 "can't be automatically refreshed."); | 81 "can't be automatically refreshed."); |
82 return withClient(cache, fn); | 82 return withClient(cache, fn); |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 |