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

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

Issue 11569046: Add a pub command for managing uploaders for packages. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes Created 8 years 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
« no previous file with comments | « utils/pub/io.dart ('k') | utils/pub/pub.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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: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 'http.dart';
12 import 'io.dart'; 13 import 'io.dart';
13 import 'log.dart' as log; 14 import 'log.dart' as log;
14 import 'system_cache.dart'; 15 import 'system_cache.dart';
15 import 'utils.dart'; 16 import 'utils.dart';
16 17
17 export '../../pkg/oauth2/lib/oauth2.dart'; 18 export '../../pkg/oauth2/lib/oauth2.dart';
18 19
19 /// The pub client's OAuth2 identifier. 20 /// The pub client's OAuth2 identifier.
20 final _identifier = '818368855108-8grd2eg9tj9f38os6f1urbcvsq399u8n.apps.' 21 final _identifier = '818368855108-8grd2eg9tj9f38os6f1urbcvsq399u8n.apps.'
21 'googleusercontent.com'; 22 'googleusercontent.com';
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 return fileExists(credentialsFile).chain((exists) { 56 return fileExists(credentialsFile).chain((exists) {
56 if (exists) return deleteFile(credentialsFile); 57 if (exists) return deleteFile(credentialsFile);
57 return new Future.immediate(null); 58 return new Future.immediate(null);
58 }); 59 });
59 } 60 }
60 61
61 /// 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
62 /// the [Future] returned by [fn] completes. 63 /// the [Future] returned by [fn] completes.
63 /// 64 ///
64 /// 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
65 /// prompting the user for their authorization. 66 /// prompting the user for their authorization. It will also re-authorize and
67 /// re-run [fn] if a recoverable authorization error is detected.
66 Future withClient(SystemCache cache, Future fn(Client client)) { 68 Future withClient(SystemCache cache, Future fn(Client client)) {
67 return _getClient(cache).chain((client) { 69 return _getClient(cache).chain((client) {
68 var completer = new Completer(); 70 var completer = new Completer();
69 var future = fn(client); 71 var future = fn(client);
70 future.onComplete((_) { 72 future.onComplete((_) {
71 try { 73 try {
72 client.close(); 74 client.close();
73 // Be sure to save the credentials even when an error happens. Also be 75 // Be sure to save the credentials even when an error happens. Also be
74 // sure to pipe the exception from `future` to `completer`. 76 // sure to pipe the exception from `future` to `completer`.
75 chainToCompleter( 77 chainToCompleter(
76 _saveCredentials(cache, client.credentials).chain((_) => future), 78 _saveCredentials(cache, client.credentials).chain((_) => future),
77 completer); 79 completer);
78 } catch (e, stackTrace) { 80 } catch (e, stackTrace) {
79 // onComplete will drop exceptions on the floor. We want to ensure that 81 // 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. 82 // any programming errors here don't go un-noticed. See issue 4127.
81 completer.completeException(e, stackTrace); 83 completer.completeException(e, stackTrace);
82 } 84 }
83 }); 85 });
84 return completer.future; 86 return completer.future;
87 }).transformException((e) {
88 if (e is ExpirationException) {
89 log.error("Pub's authorization to upload packages has expired and "
90 "can't be automatically refreshed.");
91 return withClient(cache, fn);
92 } else if (e is AuthorizationException) {
93 var message = "OAuth2 authorization failed";
94 if (e.description != null) message = "$message (${e.description})";
95 log.error("$message.");
96 return clearCredentials(cache).chain((_) => withClient(cache, fn));
97 } else {
98 throw e;
99 }
85 }); 100 });
86 } 101 }
87 102
88 /// 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
89 /// used; otherwise, the user is prompted to authorize the pub client. 104 /// used; otherwise, the user is prompted to authorize the pub client.
90 Future<Client> _getClient(SystemCache cache) { 105 Future<Client> _getClient(SystemCache cache) {
91 return _loadCredentials(cache).chain((credentials) { 106 return _loadCredentials(cache).chain((credentials) {
92 if (credentials == null) return _authorize(); 107 if (credentials == null) return _authorize();
93 return new Future.immediate(new Client( 108 return new Future.immediate(new Client(
94 _identifier, _secret, credentials, httpClient: curlClient)); 109 _identifier, _secret, credentials, httpClient: curlClient));
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 'Pub needs your authorization to upload packages on your behalf.\n' 208 'Pub needs your authorization to upload packages on your behalf.\n'
194 'In a web browser, go to $authUrl\n' 209 'In a web browser, go to $authUrl\n'
195 'Then click "Allow access".\n\n' 210 'Then click "Allow access".\n\n'
196 'Waiting for your authorization...'); 211 'Waiting for your authorization...');
197 212
198 return completer.future.transform((client) { 213 return completer.future.transform((client) {
199 log.message('Successfully authorized.\n'); 214 log.message('Successfully authorized.\n');
200 return client; 215 return client;
201 }); 216 });
202 } 217 }
OLDNEW
« no previous file with comments | « utils/pub/io.dart ('k') | utils/pub/pub.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698