| 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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 /// prompting the user for their authorization. It will also re-authorize and | 75 /// prompting the user for their authorization. It will also re-authorize and |
| 76 /// re-run [fn] if a recoverable authorization error is detected. | 76 /// re-run [fn] if a recoverable authorization error is detected. |
| 77 Future withClient(SystemCache cache, Future fn(Client client)) { | 77 Future withClient(SystemCache cache, Future fn(Client client)) { |
| 78 return _getClient(cache).then((client) { | 78 return _getClient(cache).then((client) { |
| 79 var completer = new Completer(); | 79 var completer = new Completer(); |
| 80 return fn(client).whenComplete(() { | 80 return fn(client).whenComplete(() { |
| 81 client.close(); | 81 client.close(); |
| 82 // Be sure to save the credentials even when an error happens. | 82 // Be sure to save the credentials even when an error happens. |
| 83 _saveCredentials(cache, client.credentials); | 83 _saveCredentials(cache, client.credentials); |
| 84 }); | 84 }); |
| 85 }).catchError((asyncError) { | 85 }).catchError((error) { |
| 86 if (asyncError.error is ExpirationException) { | 86 if (error is ExpirationException) { |
| 87 log.error("Pub's authorization to upload packages has expired and " | 87 log.error("Pub's authorization to upload packages has expired and " |
| 88 "can't be automatically refreshed."); | 88 "can't be automatically refreshed."); |
| 89 return withClient(cache, fn); | 89 return withClient(cache, fn); |
| 90 } else if (asyncError.error is AuthorizationException) { | 90 } else if (error is AuthorizationException) { |
| 91 var message = "OAuth2 authorization failed"; | 91 var message = "OAuth2 authorization failed"; |
| 92 if (asyncError.error.description != null) { | 92 if (error.description != null) { |
| 93 message = "$message (${asyncError.error.description})"; | 93 message = "$message (${error.description})"; |
| 94 } | 94 } |
| 95 log.error("$message."); | 95 log.error("$message."); |
| 96 clearCredentials(cache); | 96 clearCredentials(cache); |
| 97 return withClient(cache, fn); | 97 return withClient(cache, fn); |
| 98 } else { | 98 } else { |
| 99 throw asyncError; | 99 throw error; |
| 100 } | 100 } |
| 101 }); | 101 }); |
| 102 } | 102 } |
| 103 | 103 |
| 104 /// Gets a new OAuth2 client. If saved credentials are available, those are | 104 /// Gets a new OAuth2 client. If saved credentials are available, those are |
| 105 /// used; otherwise, the user is prompted to authorize the pub client. | 105 /// used; otherwise, the user is prompted to authorize the pub client. |
| 106 Future<Client> _getClient(SystemCache cache) { | 106 Future<Client> _getClient(SystemCache cache) { |
| 107 return new Future.of(() { | 107 return new Future.of(() { |
| 108 var credentials = _loadCredentials(cache); | 108 var credentials = _loadCredentials(cache); |
| 109 if (credentials == null) return _authorize(); | 109 if (credentials == null) return _authorize(); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 response.statusCode = 404; | 197 response.statusCode = 404; |
| 198 response.close(); | 198 response.close(); |
| 199 } | 199 } |
| 200 }); | 200 }); |
| 201 }) | 201 }) |
| 202 .then((client) { | 202 .then((client) { |
| 203 log.message('Successfully authorized.\n'); | 203 log.message('Successfully authorized.\n'); |
| 204 return client; | 204 return client; |
| 205 }); | 205 }); |
| 206 } | 206 } |
| OLD | NEW |