Chromium Code Reviews| Index: utils/pub/oauth2.dart |
| diff --git a/utils/pub/oauth2.dart b/utils/pub/oauth2.dart |
| index 21565a7bd524490dcddc5ea9bab4ba59f59e756d..816c90c705cf4874eb0a8e6312c70d0e5e3a62fa 100644 |
| --- a/utils/pub/oauth2.dart |
| +++ b/utils/pub/oauth2.dart |
| @@ -51,12 +51,12 @@ final _scopes = ['https://www.googleapis.com/auth/userinfo.email']; |
| Credentials _credentials; |
| /// Delete the cached credentials, if they exist. |
| -Future clearCredentials(SystemCache cache) { |
| +void clearCredentials(SystemCache cache) { |
| _credentials = null; |
| var credentialsFile = _credentialsFile(cache); |
| - return fileExists(credentialsFile).then((exists) { |
| - if (exists) return deleteFile(credentialsFile); |
| - }); |
| + if (!fileExists(credentialsFile)) return; |
| + |
| + deleteFile(credentialsFile); |
| } |
| /// Asynchronously passes an OAuth2 [Client] to [fn], and closes the client when |
| @@ -66,12 +66,14 @@ Future clearCredentials(SystemCache cache) { |
| /// prompting the user for their authorization. It will also re-authorize and |
| /// re-run [fn] if a recoverable authorization error is detected. |
| Future withClient(SystemCache cache, Future fn(Client client)) { |
| - return _getClient(cache).then((client) { |
| + // Make sure all errors propagate through future. |
| + return defer(() { |
| + var client = _getClient(cache); |
| var completer = new Completer(); |
| return fn(client).whenComplete(() { |
| client.close(); |
| // Be sure to save the credentials even when an error happens. |
| - return _saveCredentials(cache, client.credentials); |
| + _saveCredentials(cache, client.credentials); |
| }); |
| }).catchError((asyncError) { |
| if (asyncError.error is ExpirationException) { |
| @@ -84,7 +86,8 @@ Future withClient(SystemCache cache, Future fn(Client client)) { |
| message = "$message (${asyncError.error.description})"; |
| } |
| log.error("$message."); |
| - return clearCredentials(cache).then((_) => withClient(cache, fn)); |
| + clearCredentials(cache); |
| + return withClient(cache, fn); |
| } else { |
| throw asyncError; |
| } |
| @@ -93,59 +96,52 @@ Future withClient(SystemCache cache, Future fn(Client client)) { |
| /// Gets a new OAuth2 client. If saved credentials are available, those are |
| /// used; otherwise, the user is prompted to authorize the pub client. |
| -Future<Client> _getClient(SystemCache cache) { |
| - return _loadCredentials(cache).then((credentials) { |
| - if (credentials == null) return _authorize(); |
| - return new Client(_identifier, _secret, credentials, |
| - httpClient: curlClient); |
| - }).then((client) { |
| - return _saveCredentials(cache, client.credentials).then((_) => client); |
| - }); |
| +Client _getClient(SystemCache cache) { |
| + var credentials = _loadCredentials(cache); |
| + if (credentials == null) return _authorize(); |
| + var client = new Client(_identifier, _secret, credentials, |
| + httpClient: curlClient); |
| + _saveCredentials(cache, client.credentials); |
| + return client; |
| } |
| /// Loads the user's OAuth2 credentials from the in-memory cache or the |
| /// filesystem if possible. If the credentials can't be loaded for any reason, |
| /// the returned [Future] will complete to null. |
| -Future<Credentials> _loadCredentials(SystemCache cache) { |
| +Credentials _loadCredentials(SystemCache cache) { |
| log.fine('Loading OAuth2 credentials.'); |
| - if (_credentials != null) { |
| - log.fine('Using already-loaded credentials.'); |
| - return new Future.immediate(_credentials); |
| - } |
| + try { |
| + if (_credentials != null) return _credentials; |
|
nweiz
2013/02/01 02:05:55
Why no more logging?
Bob Nystrom
2013/02/01 23:17:21
Two reasons:
1. The logging is most helpful when
|
| - var path = _credentialsFile(cache); |
| - return fileExists(path).then((credentialsExist) { |
| - if (!credentialsExist) { |
| - log.fine('No credentials found at $path.'); |
| - return; |
| - } |
| + var path = _credentialsFile(cache); |
| + var credentialsExist = fileExists(path); |
| + if (!credentialsExist) return; |
|
nweiz
2013/02/01 02:05:55
Style nit: merge this and the previous line.
Bob Nystrom
2013/02/01 23:17:21
Done.
|
| - return readTextFile(_credentialsFile(cache)).then((credentialsJson) { |
| - var credentials = new Credentials.fromJson(credentialsJson); |
| - if (credentials.isExpired && !credentials.canRefresh) { |
| - log.error("Pub's authorization to upload packages has expired and " |
| - "can't be automatically refreshed."); |
| - return null; // null means re-authorize |
| - } |
| + var credentials = new Credentials.fromJson( |
| + readTextFile(_credentialsFile(cache))); |
|
nweiz
2013/02/01 02:05:55
"_credentialsFile(cache)" -> "path"
Bob Nystrom
2013/02/01 23:17:21
Done.
|
| + if (credentials.isExpired && !credentials.canRefresh) { |
| + log.error("Pub's authorization to upload packages has expired and " |
| + "can't be automatically refreshed."); |
| + return null; // null means re-authorize. |
| + } |
| - return credentials; |
| - }); |
| - }).catchError((e) { |
| + return credentials; |
| + } catch (e) { |
| log.error('Warning: could not load the saved OAuth2 credentials: $e\n' |
| 'Obtaining new credentials...'); |
| - return null; // null means re-authorize |
| - }); |
| + return null; // null means re-authorize. |
| + } |
| } |
| /// Save the user's OAuth2 credentials to the in-memory cache and the |
| /// filesystem. |
| -Future _saveCredentials(SystemCache cache, Credentials credentials) { |
| +void _saveCredentials(SystemCache cache, Credentials credentials) { |
| log.fine('Saving OAuth2 credentials.'); |
| _credentials = credentials; |
| var path = _credentialsFile(cache); |
| - return ensureDir(dirname(path)).then((_) => |
| - writeTextFile(path, credentials.toJson(), dontLogContents: true)); |
| + ensureDir(dirname(path)); |
| + writeTextFile(path, credentials.toJson(), dontLogContents: true); |
| } |
| /// The path to the file in which the user's OAuth2 credentials are stored. |
| @@ -177,7 +173,7 @@ Future<Client> _authorize() { |
| var server = new HttpServer(); |
| server.addRequestHandler((request) => request.path == "/", |
| (request, response) { |
| - chainToCompleter(new Future.immediate(null).then((_) { |
| + chainToCompleter(defer(() { |
| log.message('Authorization received, processing...'); |
| var queryString = request.queryString; |
| if (queryString == null) queryString = ''; |