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. |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 } | 101 } |
102 | 102 |
103 /// Gets a new OAuth2 client. If saved credentials are available, those are | 103 /// Gets a new OAuth2 client. If saved credentials are available, those are |
104 /// used; otherwise, the user is prompted to authorize the pub client. | 104 /// used; otherwise, the user is prompted to authorize the pub client. |
105 Future<Client> _getClient(SystemCache cache) { | 105 Future<Client> _getClient(SystemCache cache) { |
106 return _loadCredentials(cache).chain((credentials) { | 106 return _loadCredentials(cache).chain((credentials) { |
107 if (credentials == null) return _authorize(); | 107 if (credentials == null) return _authorize(); |
108 return new Future.immediate(new Client( | 108 return new Future.immediate(new Client( |
109 _identifier, _secret, credentials, httpClient: curlClient)); | 109 _identifier, _secret, credentials, httpClient: curlClient)); |
110 }).chain((client) { | 110 }).chain((client) { |
111 return _saveCredentials(cache, client.credentials).transform((_) => client); | 111 return _saveCredentials(cache, client.credentials).then((_) => client); |
112 }); | 112 }); |
113 } | 113 } |
114 | 114 |
115 /// Loads the user's OAuth2 credentials from the in-memory cache or the | 115 /// Loads the user's OAuth2 credentials from the in-memory cache or the |
116 /// filesystem if possible. If the credentials can't be loaded for any reason, | 116 /// filesystem if possible. If the credentials can't be loaded for any reason, |
117 /// the returned [Future] will complete to null. | 117 /// the returned [Future] will complete to null. |
118 Future<Credentials> _loadCredentials(SystemCache cache) { | 118 Future<Credentials> _loadCredentials(SystemCache cache) { |
119 log.fine('Loading OAuth2 credentials.'); | 119 log.fine('Loading OAuth2 credentials.'); |
120 | 120 |
121 if (_credentials != null) { | 121 if (_credentials != null) { |
122 log.fine('Using already-loaded credentials.'); | 122 log.fine('Using already-loaded credentials.'); |
123 return new Future.immediate(_credentials); | 123 return new Future.immediate(_credentials); |
124 } | 124 } |
125 | 125 |
126 var path = _credentialsFile(cache); | 126 var path = _credentialsFile(cache); |
127 return fileExists(path).chain((credentialsExist) { | 127 return fileExists(path).chain((credentialsExist) { |
128 if (!credentialsExist) { | 128 if (!credentialsExist) { |
129 log.fine('No credentials found at $path.'); | 129 log.fine('No credentials found at $path.'); |
130 return new Future.immediate(null); | 130 return new Future.immediate(null); |
131 } | 131 } |
132 | 132 |
133 return readTextFile(_credentialsFile(cache)).transform((credentialsJson) { | 133 return readTextFile(_credentialsFile(cache)).then((credentialsJson) { |
134 var credentials = new Credentials.fromJson(credentialsJson); | 134 var credentials = new Credentials.fromJson(credentialsJson); |
135 if (credentials.isExpired && !credentials.canRefresh) { | 135 if (credentials.isExpired && !credentials.canRefresh) { |
136 log.error("Pub's authorization to upload packages has expired and " | 136 log.error("Pub's authorization to upload packages has expired and " |
137 "can't be automatically refreshed."); | 137 "can't be automatically refreshed."); |
138 return null; // null means re-authorize | 138 return null; // null means re-authorize |
139 } | 139 } |
140 | 140 |
141 return credentials; | 141 return credentials; |
142 }); | 142 }); |
143 }).transformException((e) { | 143 }).transformException((e) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 server.addRequestHandler((request) => request.path == "/", | 187 server.addRequestHandler((request) => request.path == "/", |
188 (request, response) { | 188 (request, response) { |
189 chainToCompleter(new Future.immediate(null).chain((_) { | 189 chainToCompleter(new Future.immediate(null).chain((_) { |
190 log.message('Authorization received, processing...'); | 190 log.message('Authorization received, processing...'); |
191 var queryString = request.queryString; | 191 var queryString = request.queryString; |
192 if (queryString == null) queryString = ''; | 192 if (queryString == null) queryString = ''; |
193 response.statusCode = 302; | 193 response.statusCode = 302; |
194 response.headers.set('location', 'http://pub.dartlang.org/authorized'); | 194 response.headers.set('location', 'http://pub.dartlang.org/authorized'); |
195 response.outputStream.close(); | 195 response.outputStream.close(); |
196 return grant.handleAuthorizationResponse(queryToMap(queryString)); | 196 return grant.handleAuthorizationResponse(queryToMap(queryString)); |
197 }).transform((client) { | 197 }).then((client) { |
198 server.close(); | 198 server.close(); |
199 return client; | 199 return client; |
200 }), completer); | 200 }), completer); |
201 }); | 201 }); |
202 server.listen('127.0.0.1', 0); | 202 server.listen('127.0.0.1', 0); |
203 | 203 |
204 var authUrl = grant.getAuthorizationUrl( | 204 var authUrl = grant.getAuthorizationUrl( |
205 new Uri.fromString('http://localhost:${server.port}'), scopes: _scopes); | 205 new Uri.fromString('http://localhost:${server.port}'), scopes: _scopes); |
206 | 206 |
207 log.message( | 207 log.message( |
208 'Pub needs your authorization to upload packages on your behalf.\n' | 208 'Pub needs your authorization to upload packages on your behalf.\n' |
209 'In a web browser, go to $authUrl\n' | 209 'In a web browser, go to $authUrl\n' |
210 'Then click "Allow access".\n\n' | 210 'Then click "Allow access".\n\n' |
211 'Waiting for your authorization...'); | 211 'Waiting for your authorization...'); |
212 | 212 |
213 return completer.future.transform((client) { | 213 return completer.future.then((client) { |
214 log.message('Successfully authorized.\n'); | 214 log.message('Successfully authorized.\n'); |
215 return client; | 215 return client; |
216 }); | 216 }); |
217 } | 217 } |
OLD | NEW |