| 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 var future = fn(client); | 72 return asyncWhenComplete(fn(client), () { |
| 73 future.whenComplete(() { | 73 client.close(); |
| 74 try { | 74 // Be sure to save the credentials even when an error happens. |
| 75 client.close(); | 75 return _saveCredentials(cache, client.credentials); |
| 76 // Be sure to save the credentials even when an error happens. Also be | |
| 77 // sure to pipe the exception from `future` to `completer`. | |
| 78 chainToCompleter( | |
| 79 _saveCredentials(cache, client.credentials).then((_) => future), | |
| 80 completer); | |
| 81 } catch (e, stackTrace) { | |
| 82 // whenComplete will drop exceptions on the floor. We want to ensure | |
| 83 // that any programming errors here don't go un-noticed. See issue 4127. | |
| 84 completer.completeError(e, stackTrace); | |
| 85 } | |
| 86 }); | 76 }); |
| 87 return completer.future; | 77 }).catchError((asyncError) { |
| 88 }).catchError((e) { | 78 var e = getRealError(asyncError); |
| 89 if (e is ExpirationException) { | 79 if (e is ExpirationException) { |
| 90 log.error("Pub's authorization to upload packages has expired and " | 80 log.error("Pub's authorization to upload packages has expired and " |
| 91 "can't be automatically refreshed."); | 81 "can't be automatically refreshed."); |
| 92 return withClient(cache, fn); | 82 return withClient(cache, fn); |
| 93 } else if (e is AuthorizationException) { | 83 } else if (e is AuthorizationException) { |
| 94 var message = "OAuth2 authorization failed"; | 84 var message = "OAuth2 authorization failed"; |
| 95 if (e.description != null) message = "$message (${e.description})"; | 85 if (e.description != null) message = "$message (${e.description})"; |
| 96 log.error("$message."); | 86 log.error("$message."); |
| 97 return clearCredentials(cache).then((_) => withClient(cache, fn)); | 87 return clearCredentials(cache).then((_) => withClient(cache, fn)); |
| 98 } else { | 88 } else { |
| 99 throw e; | 89 throw asyncError; |
| 100 } | 90 } |
| 101 }); | 91 }); |
| 102 } | 92 } |
| 103 | 93 |
| 104 /// 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 |
| 105 /// used; otherwise, the user is prompted to authorize the pub client. | 95 /// used; otherwise, the user is prompted to authorize the pub client. |
| 106 Future<Client> _getClient(SystemCache cache) { | 96 Future<Client> _getClient(SystemCache cache) { |
| 107 return _loadCredentials(cache).then((credentials) { | 97 return _loadCredentials(cache).then((credentials) { |
| 108 if (credentials == null) return _authorize(); | 98 if (credentials == null) return _authorize(); |
| 109 return new Future.immediate(new Client( | 99 return new Future.immediate(new Client( |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 'Pub needs your authorization to upload packages on your behalf.\n' | 199 'Pub needs your authorization to upload packages on your behalf.\n' |
| 210 'In a web browser, go to $authUrl\n' | 200 'In a web browser, go to $authUrl\n' |
| 211 'Then click "Allow access".\n\n' | 201 'Then click "Allow access".\n\n' |
| 212 'Waiting for your authorization...'); | 202 'Waiting for your authorization...'); |
| 213 | 203 |
| 214 return completer.future.then((client) { | 204 return completer.future.then((client) { |
| 215 log.message('Successfully authorized.\n'); | 205 log.message('Successfully authorized.\n'); |
| 216 return client; | 206 return client; |
| 217 }); | 207 }); |
| 218 } | 208 } |
| OLD | NEW |