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:io'; | 7 import 'dart:io'; |
8 import 'dart:uri'; | 8 import 'dart:uri'; |
9 | 9 |
10 // TODO(nweiz): Make this a "package:" URL, or something nicer than this. | 10 // TODO(nweiz): Make this a "package:" URL, or something nicer than this. |
11 import '../../pkg/oauth2/lib/oauth2.dart'; | 11 import '../../pkg/oauth2/lib/oauth2.dart'; |
12 import 'io.dart'; | 12 import 'io.dart'; |
13 import 'log.dart' as log; | |
14 import 'system_cache.dart'; | 13 import 'system_cache.dart'; |
15 import 'utils.dart'; | 14 import 'utils.dart'; |
16 | 15 |
17 export '../../pkg/oauth2/lib/oauth2.dart'; | 16 export '../../pkg/oauth2/lib/oauth2.dart'; |
18 | 17 |
19 /// The pub client's OAuth2 identifier. | 18 /// The pub client's OAuth2 identifier. |
20 final _identifier = '818368855108-8grd2eg9tj9f38os6f1urbcvsq399u8n.apps.' | 19 final _identifier = '818368855108-8grd2eg9tj9f38os6f1urbcvsq399u8n.apps.' |
21 'googleusercontent.com'; | 20 'googleusercontent.com'; |
22 | 21 |
23 /// The pub client's OAuth2 secret. This isn't actually meant to be kept a | 22 /// The pub client's OAuth2 secret. This isn't actually meant to be kept a |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 _identifier, _secret, credentials, httpClient: curlClient)); | 93 _identifier, _secret, credentials, httpClient: curlClient)); |
95 }).chain((client) { | 94 }).chain((client) { |
96 return _saveCredentials(cache, client.credentials).transform((_) => client); | 95 return _saveCredentials(cache, client.credentials).transform((_) => client); |
97 }); | 96 }); |
98 } | 97 } |
99 | 98 |
100 /// Loads the user's OAuth2 credentials from the in-memory cache or the | 99 /// Loads the user's OAuth2 credentials from the in-memory cache or the |
101 /// filesystem if possible. If the credentials can't be loaded for any reason, | 100 /// filesystem if possible. If the credentials can't be loaded for any reason, |
102 /// the returned [Future] will complete to null. | 101 /// the returned [Future] will complete to null. |
103 Future<Credentials> _loadCredentials(SystemCache cache) { | 102 Future<Credentials> _loadCredentials(SystemCache cache) { |
104 log.fine('Loading OAuth2 credentials.'); | 103 if (_credentials != null) return new Future.immediate(_credentials); |
105 | 104 return fileExists(_credentialsFile(cache)).chain((credentialsExist) { |
106 if (_credentials != null) { | 105 if (!credentialsExist) return new Future.immediate(null); |
107 log.fine('Using already-loaded credentials.'); | |
108 return new Future.immediate(_credentials); | |
109 } | |
110 | |
111 var path = _credentialsFile(cache); | |
112 return fileExists(path).chain((credentialsExist) { | |
113 if (!credentialsExist) { | |
114 log.fine('No credentials found at $path.'); | |
115 return new Future.immediate(null); | |
116 } | |
117 | 106 |
118 return readTextFile(_credentialsFile(cache)).transform((credentialsJson) { | 107 return readTextFile(_credentialsFile(cache)).transform((credentialsJson) { |
119 var credentials = new Credentials.fromJson(credentialsJson); | 108 var credentials = new Credentials.fromJson(credentialsJson); |
120 if (credentials.isExpired && !credentials.canRefresh) { | 109 if (credentials.isExpired && !credentials.canRefresh) { |
121 log.error("Pub's authorization to upload packages has expired and " | 110 printError("Pub's authorization to upload packages has expired and " |
122 "can't be automatically refreshed."); | 111 "can't be automatically refreshed."); |
123 return null; // null means re-authorize | 112 return null; // null means re-authorize |
124 } | 113 } |
125 | 114 |
126 return credentials; | 115 return credentials; |
127 }); | 116 }); |
128 }).transformException((e) { | 117 }).transformException((e) { |
129 log.error('Warning: could not load the saved OAuth2 credentials: $e\n' | 118 printError('Warning: could not load the saved OAuth2 credentials:' |
| 119 ' $e\n' |
130 'Obtaining new credentials...'); | 120 'Obtaining new credentials...'); |
131 return null; // null means re-authorize | 121 return null; // null means re-authorize |
132 }); | 122 }); |
133 } | 123 } |
134 | 124 |
135 /// Save the user's OAuth2 credentials to the in-memory cache and the | 125 /// Save the user's OAuth2 credentials to the in-memory cache and the |
136 /// filesystem. | 126 /// filesystem. |
137 Future _saveCredentials(SystemCache cache, Credentials credentials) { | 127 Future _saveCredentials(SystemCache cache, Credentials credentials) { |
138 log.fine('Saving OAuth2 credentials.'); | |
139 _credentials = credentials; | 128 _credentials = credentials; |
140 var path = _credentialsFile(cache); | 129 var credentialsFile = _credentialsFile(cache); |
141 return ensureDir(dirname(path)).chain((_) => | 130 return ensureDir(dirname(credentialsFile)).chain((_) => |
142 writeTextFile(path, credentials.toJson())); | 131 writeTextFile(credentialsFile, credentials.toJson())); |
143 } | 132 } |
144 | 133 |
145 /// The path to the file in which the user's OAuth2 credentials are stored. | 134 /// The path to the file in which the user's OAuth2 credentials are stored. |
146 String _credentialsFile(SystemCache cache) => | 135 String _credentialsFile(SystemCache cache) => |
147 join(cache.rootDir, 'credentials.json'); | 136 join(cache.rootDir, 'credentials.json'); |
148 | 137 |
149 /// Gets the user to authorize pub as a client of pub.dartlang.org via oauth2. | 138 /// Gets the user to authorize pub as a client of pub.dartlang.org via oauth2. |
150 /// Returns a Future that will complete to a fully-authorized [Client]. | 139 /// Returns a Future that will complete to a fully-authorized [Client]. |
151 Future<Client> _authorize() { | 140 Future<Client> _authorize() { |
152 // Allow the tests to inject their own token endpoint URL. | 141 // Allow the tests to inject their own token endpoint URL. |
(...skipping 12 matching lines...) Expand all Loading... |
165 httpClient: curlClient); | 154 httpClient: curlClient); |
166 | 155 |
167 // Spin up a one-shot HTTP server to receive the authorization code from the | 156 // Spin up a one-shot HTTP server to receive the authorization code from the |
168 // Google OAuth2 server via redirect. This server will close itself as soon as | 157 // Google OAuth2 server via redirect. This server will close itself as soon as |
169 // the code is received. | 158 // the code is received. |
170 var completer = new Completer(); | 159 var completer = new Completer(); |
171 var server = new HttpServer(); | 160 var server = new HttpServer(); |
172 server.addRequestHandler((request) => request.path == "/", | 161 server.addRequestHandler((request) => request.path == "/", |
173 (request, response) { | 162 (request, response) { |
174 chainToCompleter(new Future.immediate(null).chain((_) { | 163 chainToCompleter(new Future.immediate(null).chain((_) { |
175 log.message('Authorization received, processing...'); | 164 print('Authorization received, processing...'); |
176 var queryString = request.queryString; | 165 var queryString = request.queryString; |
177 if (queryString == null) queryString = ''; | 166 if (queryString == null) queryString = ''; |
178 response.statusCode = 302; | 167 response.statusCode = 302; |
179 response.headers.set('location', 'http://pub.dartlang.org/authorized'); | 168 response.headers.set('location', 'http://pub.dartlang.org/authorized'); |
180 return Futures.wait([ | 169 return Futures.wait([ |
181 closeHttpResponse(request, response), | 170 closeHttpResponse(request, response), |
182 grant.handleAuthorizationResponse(queryToMap(queryString)) | 171 grant.handleAuthorizationResponse(queryToMap(queryString)) |
183 ]); | 172 ]); |
184 }).transform((results) { | 173 }).transform((results) { |
185 server.close(); | 174 server.close(); |
186 return results[1]; | 175 return results[1]; |
187 }), completer); | 176 }), completer); |
188 }); | 177 }); |
189 server.listen('127.0.0.1', 0); | 178 server.listen('127.0.0.1', 0); |
190 | 179 |
191 var authUrl = grant.getAuthorizationUrl( | 180 var authUrl = grant.getAuthorizationUrl( |
192 new Uri.fromString('http://localhost:${server.port}'), scopes: _scopes); | 181 new Uri.fromString('http://localhost:${server.port}'), scopes: _scopes); |
193 | 182 |
194 log.message( | 183 print('Pub needs your authorization to upload packages on your behalf.\n' |
195 'Pub needs your authorization to upload packages on your behalf.\n' | 184 'In a web browser, go to $authUrl\n' |
196 'In a web browser, go to $authUrl\n' | 185 'Then click "Allow access".\n\n' |
197 'Then click "Allow access".\n\n' | 186 'Waiting for your authorization...'); |
198 'Waiting for your authorization...'); | |
199 | 187 |
200 return completer.future.transform((client) { | 188 return completer.future.transform((client) { |
201 log.message('Successfully authorized.\n'); | 189 print('Successfully authorized.\n'); |
202 return client; | 190 return client; |
203 }); | 191 }); |
204 } | 192 } |
OLD | NEW |