Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(378)

Side by Side Diff: utils/pub/oauth2.dart

Issue 12042053: Get rid of unneeded Future.immediate() calls. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 /// An in-memory cache of the user's OAuth2 credentials. This should always be 49 /// An in-memory cache of the user's OAuth2 credentials. This should always be
50 /// the same as the credentials file stored in the system cache. 50 /// the same as the credentials file stored in the system cache.
51 Credentials _credentials; 51 Credentials _credentials;
52 52
53 /// Delete the cached credentials, if they exist. 53 /// Delete the cached credentials, if they exist.
54 Future clearCredentials(SystemCache cache) { 54 Future clearCredentials(SystemCache cache) {
55 _credentials = null; 55 _credentials = null;
56 var credentialsFile = _credentialsFile(cache); 56 var credentialsFile = _credentialsFile(cache);
57 return fileExists(credentialsFile).then((exists) { 57 return fileExists(credentialsFile).then((exists) {
58 if (exists) return deleteFile(credentialsFile); 58 if (exists) return deleteFile(credentialsFile);
59 return new Future.immediate(null);
60 }); 59 });
61 } 60 }
62 61
63 /// Asynchronously passes an OAuth2 [Client] to [fn], and closes the client when 62 /// Asynchronously passes an OAuth2 [Client] to [fn], and closes the client when
64 /// the [Future] returned by [fn] completes. 63 /// the [Future] returned by [fn] completes.
65 /// 64 ///
66 /// This takes care of loading and saving the client's credentials, as well as 65 /// 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 66 /// prompting the user for their authorization. It will also re-authorize and
68 /// re-run [fn] if a recoverable authorization error is detected. 67 /// re-run [fn] if a recoverable authorization error is detected.
69 Future withClient(SystemCache cache, Future fn(Client client)) { 68 Future withClient(SystemCache cache, Future fn(Client client)) {
(...skipping 20 matching lines...) Expand all
90 throw asyncError; 89 throw asyncError;
91 } 90 }
92 }); 91 });
93 } 92 }
94 93
95 /// 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
96 /// used; otherwise, the user is prompted to authorize the pub client. 95 /// used; otherwise, the user is prompted to authorize the pub client.
97 Future<Client> _getClient(SystemCache cache) { 96 Future<Client> _getClient(SystemCache cache) {
98 return _loadCredentials(cache).then((credentials) { 97 return _loadCredentials(cache).then((credentials) {
99 if (credentials == null) return _authorize(); 98 if (credentials == null) return _authorize();
100 return new Future.immediate(new Client( 99 return new Client(_identifier, _secret, credentials,
101 _identifier, _secret, credentials, httpClient: curlClient)); 100 httpClient: curlClient);
102 }).then((client) { 101 }).then((client) {
103 return _saveCredentials(cache, client.credentials).then((_) => client); 102 return _saveCredentials(cache, client.credentials).then((_) => client);
104 }); 103 });
105 } 104 }
106 105
107 /// 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
108 /// 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,
109 /// the returned [Future] will complete to null. 108 /// the returned [Future] will complete to null.
110 Future<Credentials> _loadCredentials(SystemCache cache) { 109 Future<Credentials> _loadCredentials(SystemCache cache) {
111 log.fine('Loading OAuth2 credentials.'); 110 log.fine('Loading OAuth2 credentials.');
112 111
113 if (_credentials != null) { 112 if (_credentials != null) {
114 log.fine('Using already-loaded credentials.'); 113 log.fine('Using already-loaded credentials.');
115 return new Future.immediate(_credentials); 114 return new Future.immediate(_credentials);
116 } 115 }
117 116
118 var path = _credentialsFile(cache); 117 var path = _credentialsFile(cache);
119 return fileExists(path).then((credentialsExist) { 118 return fileExists(path).then((credentialsExist) {
120 if (!credentialsExist) { 119 if (!credentialsExist) {
121 log.fine('No credentials found at $path.'); 120 log.fine('No credentials found at $path.');
122 return new Future.immediate(null); 121 return;
123 } 122 }
124 123
125 return readTextFile(_credentialsFile(cache)).then((credentialsJson) { 124 return readTextFile(_credentialsFile(cache)).then((credentialsJson) {
126 var credentials = new Credentials.fromJson(credentialsJson); 125 var credentials = new Credentials.fromJson(credentialsJson);
127 if (credentials.isExpired && !credentials.canRefresh) { 126 if (credentials.isExpired && !credentials.canRefresh) {
128 log.error("Pub's authorization to upload packages has expired and " 127 log.error("Pub's authorization to upload packages has expired and "
129 "can't be automatically refreshed."); 128 "can't be automatically refreshed.");
130 return null; // null means re-authorize 129 return null; // null means re-authorize
131 } 130 }
132 131
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 'Pub needs your authorization to upload packages on your behalf.\n' 199 'Pub needs your authorization to upload packages on your behalf.\n'
201 'In a web browser, go to $authUrl\n' 200 'In a web browser, go to $authUrl\n'
202 'Then click "Allow access".\n\n' 201 'Then click "Allow access".\n\n'
203 'Waiting for your authorization...'); 202 'Waiting for your authorization...');
204 203
205 return completer.future.then((client) { 204 return completer.future.then((client) {
206 log.message('Successfully authorized.\n'); 205 log.message('Successfully authorized.\n');
207 return client; 206 return client;
208 }); 207 });
209 } 208 }
OLDNEW
« utils/pub/entrypoint.dart ('K') | « utils/pub/io.dart ('k') | utils/pub/pub.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698