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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
55 return fileExists(credentialsFile).chain((exists) { | 55 return fileExists(credentialsFile).chain((exists) { |
56 if (exists) return deleteFile(credentialsFile); | 56 if (exists) return deleteFile(credentialsFile); |
57 return new Future.immediate(null); | 57 return new Future.immediate(null); |
58 }); | 58 }); |
59 } | 59 } |
60 | 60 |
61 /// Asynchronously passes an OAuth2 [Client] to [fn], and closes the client when | 61 /// Asynchronously passes an OAuth2 [Client] to [fn], and closes the client when |
62 /// the [Future] returned by [fn] completes. | 62 /// the [Future] returned by [fn] completes. |
63 /// | 63 /// |
64 /// This takes care of loading and saving the client's credentials, as well as | 64 /// This takes care of loading and saving the client's credentials, as well as |
65 /// prompting the user for their authorization. | 65 /// prompting the user for their authorization. It will also re-authorize and |
66 /// re-run [fn] if a recoverable authorization error is detected. | |
66 Future withClient(SystemCache cache, Future fn(Client client)) { | 67 Future withClient(SystemCache cache, Future fn(Client client)) { |
67 return _getClient(cache).chain((client) { | 68 return _getClient(cache).chain((client) { |
68 var completer = new Completer(); | 69 var completer = new Completer(); |
69 var future = fn(client); | 70 var future = fn(client); |
70 future.onComplete((_) { | 71 future.onComplete((_) { |
71 try { | 72 try { |
72 client.close(); | 73 client.close(); |
73 // Be sure to save the credentials even when an error happens. Also be | 74 // Be sure to save the credentials even when an error happens. Also be |
74 // sure to pipe the exception from `future` to `completer`. | 75 // sure to pipe the exception from `future` to `completer`. |
75 chainToCompleter( | 76 chainToCompleter( |
76 _saveCredentials(cache, client.credentials).chain((_) => future), | 77 _saveCredentials(cache, client.credentials).chain((_) => future), |
77 completer); | 78 completer); |
78 } catch (e, stackTrace) { | 79 } catch (e, stackTrace) { |
79 // onComplete will drop exceptions on the floor. We want to ensure that | 80 // onComplete will drop exceptions on the floor. We want to ensure that |
80 // any programming errors here don't go un-noticed. See issue 4127. | 81 // any programming errors here don't go un-noticed. See issue 4127. |
81 completer.completeException(e, stackTrace); | 82 completer.completeException(e, stackTrace); |
82 } | 83 } |
83 }); | 84 }); |
84 return completer.future; | 85 return completer.future; |
86 }).transformException((e) { | |
87 if (e is ExpirationException) { | |
88 log.error("Pub's authorization to upload packages has expired and " | |
89 "can't be automatically refreshed."); | |
90 return withClient(cache, fn); | |
Bob Nystrom
2012/12/17 18:41:58
Do we need to guard against getting stuck in an in
nweiz
2012/12/17 21:18:48
I don't think so -- even if the server keeps sendi
| |
91 } else if (e is AuthorizationException) { | |
92 var message = "OAuth2 authorization failed"; | |
93 if (e.description != null) message = "$message (${e.description})"; | |
94 log.error("$message."); | |
95 return clearCredentials(cache).chain((_) => withClient(cache, fn)); | |
96 } else { | |
97 throw e; | |
98 } | |
85 }); | 99 }); |
86 } | 100 } |
87 | 101 |
88 /// Gets a new OAuth2 client. If saved credentials are available, those are | 102 /// Gets a new OAuth2 client. If saved credentials are available, those are |
89 /// used; otherwise, the user is prompted to authorize the pub client. | 103 /// used; otherwise, the user is prompted to authorize the pub client. |
90 Future<Client> _getClient(SystemCache cache) { | 104 Future<Client> _getClient(SystemCache cache) { |
91 return _loadCredentials(cache).chain((credentials) { | 105 return _loadCredentials(cache).chain((credentials) { |
92 if (credentials == null) return _authorize(); | 106 if (credentials == null) return _authorize(); |
93 return new Future.immediate(new Client( | 107 return new Future.immediate(new Client( |
94 _identifier, _secret, credentials, httpClient: curlClient)); | 108 _identifier, _secret, credentials, httpClient: curlClient)); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
193 'Pub needs your authorization to upload packages on your behalf.\n' | 207 'Pub needs your authorization to upload packages on your behalf.\n' |
194 'In a web browser, go to $authUrl\n' | 208 'In a web browser, go to $authUrl\n' |
195 'Then click "Allow access".\n\n' | 209 'Then click "Allow access".\n\n' |
196 'Waiting for your authorization...'); | 210 'Waiting for your authorization...'); |
197 | 211 |
198 return completer.future.transform((client) { | 212 return completer.future.transform((client) { |
199 log.message('Successfully authorized.\n'); | 213 log.message('Successfully authorized.\n'); |
200 return client; | 214 return client; |
201 }); | 215 }); |
202 } | 216 } |
OLD | NEW |