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

Side by Side Diff: lib/src/credentials.dart

Issue 1308163004: Async-ify. (Closed) Base URL: git@github.com:dart-lang/oauth2.git@master
Patch Set: Created 5 years, 3 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
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 credentials; 5 library credentials;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:convert'; 8 import 'dart:convert';
9 9
10 import 'package:http/http.dart' as http; 10 import 'package:http/http.dart' as http;
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 /// You may request different scopes than the default by passing in 139 /// You may request different scopes than the default by passing in
140 /// [newScopes]. These must be a subset of [scopes]. 140 /// [newScopes]. These must be a subset of [scopes].
141 /// 141 ///
142 /// This will throw a [StateError] if these credentials can't be refreshed, an 142 /// This will throw a [StateError] if these credentials can't be refreshed, an
143 /// [AuthorizationException] if refreshing the credentials fails, or a 143 /// [AuthorizationException] if refreshing the credentials fails, or a
144 /// [FormatError] if the authorization server returns invalid responses. 144 /// [FormatError] if the authorization server returns invalid responses.
145 Future<Credentials> refresh( 145 Future<Credentials> refresh(
146 String identifier, 146 String identifier,
147 String secret, 147 String secret,
148 {List<String> newScopes, 148 {List<String> newScopes,
149 http.Client httpClient}) { 149 http.Client httpClient}) async {
150 var scopes = this.scopes; 150 var scopes = this.scopes;
151 if (newScopes != null) scopes = newScopes; 151 if (newScopes != null) scopes = newScopes;
152 if (scopes == null) scopes = <String>[]; 152 if (scopes == null) scopes = <String>[];
153 if (httpClient == null) httpClient = new http.Client(); 153 if (httpClient == null) httpClient = new http.Client();
154 154
155 var startTime = new DateTime.now(); 155 var startTime = new DateTime.now();
156 return async.then((_) { 156 if (refreshToken == null) {
157 if (refreshToken == null) { 157 throw new StateError("Can't refresh credentials without a refresh "
158 throw new StateError("Can't refresh credentials without a refresh " 158 "token.");
159 "token."); 159 } else if (tokenEndpoint == null) {
160 } else if (tokenEndpoint == null) { 160 throw new StateError("Can't refresh credentials without a token "
161 throw new StateError("Can't refresh credentials without a token " 161 "endpoint.");
162 "endpoint."); 162 }
163 }
164 163
165 var fields = { 164 var fields = {
166 "grant_type": "refresh_token", 165 "grant_type": "refresh_token",
167 "refresh_token": refreshToken, 166 "refresh_token": refreshToken,
168 // TODO(nweiz): the spec recommends that HTTP basic auth be used in 167 // TODO(nweiz): the spec recommends that HTTP basic auth be used in
169 // preference to form parameters, but Google doesn't support that. 168 // preference to form parameters, but Google doesn't support that.
170 // Should it be configurable? 169 // Should it be configurable?
171 "client_id": identifier, 170 "client_id": identifier,
172 "client_secret": secret 171 "client_secret": secret
173 }; 172 };
174 if (!scopes.isEmpty) fields["scope"] = scopes.join(' '); 173 if (!scopes.isEmpty) fields["scope"] = scopes.join(' ');
175 174
176 return httpClient.post(tokenEndpoint, body: fields); 175 var response = await httpClient.post(tokenEndpoint, body: fields);
177 }).then((response) { 176 var credentials = await handleAccessTokenResponse(
178 return handleAccessTokenResponse(
179 response, tokenEndpoint, startTime, scopes); 177 response, tokenEndpoint, startTime, scopes);
180 }).then((credentials) { 178
181 // The authorization server may issue a new refresh token. If it doesn't, 179 // The authorization server may issue a new refresh token. If it doesn't,
182 // we should re-use the one we already have. 180 // we should re-use the one we already have.
183 if (credentials.refreshToken != null) return credentials; 181 if (credentials.refreshToken != null) return credentials;
184 return new Credentials( 182 return new Credentials(
185 credentials.accessToken, 183 credentials.accessToken,
186 this.refreshToken, 184 this.refreshToken,
187 credentials.tokenEndpoint, 185 credentials.tokenEndpoint,
188 credentials.scopes, 186 credentials.scopes,
189 credentials.expiration); 187 credentials.expiration);
190 });
191 } 188 }
192 } 189 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698