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

Unified Diff: lib/src/authorization_code_grant.dart

Issue 1308163004: Async-ify. (Closed) Base URL: git@github.com:dart-lang/oauth2.git@master
Patch Set: Created 5 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « README.md ('k') | lib/src/client.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/authorization_code_grant.dart
diff --git a/lib/src/authorization_code_grant.dart b/lib/src/authorization_code_grant.dart
index c879e8ca1dece0593e8b7d418bd66b3e36051d2b..9ec4a274d19d7df359061f26f94cb3eafd80db5d 100644
--- a/lib/src/authorization_code_grant.dart
+++ b/lib/src/authorization_code_grant.dart
@@ -96,12 +96,12 @@ class AuthorizationCodeGrant {
/// [httpClient] is used for all HTTP requests made by this grant, as well as
/// those of the [Client] is constructs.
AuthorizationCodeGrant(
- this.identifier,
- this.secret,
- this.authorizationEndpoint,
- this.tokenEndpoint,
- {http.Client httpClient})
- : _httpClient = httpClient == null ? new http.Client() : httpClient;
+ this.identifier,
+ this.secret,
+ this.authorizationEndpoint,
+ this.tokenEndpoint,
+ {http.Client httpClient})
+ : _httpClient = httpClient == null ? new http.Client() : httpClient;
/// Returns the URL to which the resource owner should be redirected to
/// authorize this client. The resource owner will then be redirected to
@@ -157,42 +157,41 @@ class AuthorizationCodeGrant {
/// [FormatError] if the `state` parameter doesn't match the original value.
///
/// Throws [AuthorizationException] if the authorization fails.
- Future<Client> handleAuthorizationResponse(Map<String, String> parameters) {
- return async.then((_) {
- if (_state == _INITIAL_STATE) {
- throw new StateError(
- 'The authorization URL has not yet been generated.');
- } else if (_state == _FINISHED_STATE) {
- throw new StateError(
- 'The authorization code has already been received.');
- }
- _state = _FINISHED_STATE;
-
- if (_stateString != null) {
- if (!parameters.containsKey('state')) {
- throw new FormatException('Invalid OAuth response for '
- '"$authorizationEndpoint": parameter "state" expected to be '
- '"$_stateString", was missing.');
- } else if (parameters['state'] != _stateString) {
- throw new FormatException('Invalid OAuth response for '
- '"$authorizationEndpoint": parameter "state" expected to be '
- '"$_stateString", was "${parameters['state']}".');
- }
- }
+ Future<Client> handleAuthorizationResponse(Map<String, String> parameters)
+ async {
+ if (_state == _INITIAL_STATE) {
+ throw new StateError(
+ 'The authorization URL has not yet been generated.');
+ } else if (_state == _FINISHED_STATE) {
Bob Nystrom 2015/08/24 21:36:51 Nit, but the "else" isn't useful.
nweiz 2015/08/24 23:58:34 I like using "else"s when they make the code terse
+ throw new StateError(
+ 'The authorization code has already been received.');
+ }
+ _state = _FINISHED_STATE;
- if (parameters.containsKey('error')) {
- var description = parameters['error_description'];
- var uriString = parameters['error_uri'];
- var uri = uriString == null ? null : Uri.parse(uriString);
- throw new AuthorizationException(parameters['error'], description, uri);
- } else if (!parameters.containsKey('code')) {
+ if (_stateString != null) {
+ if (!parameters.containsKey('state')) {
throw new FormatException('Invalid OAuth response for '
- '"$authorizationEndpoint": did not contain required parameter '
- '"code".');
+ '"$authorizationEndpoint": parameter "state" expected to be '
+ '"$_stateString", was missing.');
+ } else if (parameters['state'] != _stateString) {
+ throw new FormatException('Invalid OAuth response for '
+ '"$authorizationEndpoint": parameter "state" expected to be '
+ '"$_stateString", was "${parameters['state']}".');
}
+ }
- return _handleAuthorizationCode(parameters['code']);
- });
+ if (parameters.containsKey('error')) {
+ var description = parameters['error_description'];
+ var uriString = parameters['error_uri'];
+ var uri = uriString == null ? null : Uri.parse(uriString);
+ throw new AuthorizationException(parameters['error'], description, uri);
+ } else if (!parameters.containsKey('code')) {
+ throw new FormatException('Invalid OAuth response for '
+ '"$authorizationEndpoint": did not contain required parameter '
+ '"code".');
+ }
+
+ return await _handleAuthorizationCode(parameters['code']);
}
/// Processes an authorization code directly. Usually
@@ -209,26 +208,24 @@ class AuthorizationCodeGrant {
/// responses while retrieving credentials.
///
/// Throws [AuthorizationException] if the authorization fails.
- Future<Client> handleAuthorizationCode(String authorizationCode) {
- return async.then((_) {
- if (_state == _INITIAL_STATE) {
- throw new StateError(
- 'The authorization URL has not yet been generated.');
- } else if (_state == _FINISHED_STATE) {
- throw new StateError(
- 'The authorization code has already been received.');
- }
- _state = _FINISHED_STATE;
+ Future<Client> handleAuthorizationCode(String authorizationCode) async {
+ if (_state == _INITIAL_STATE) {
+ throw new StateError(
+ 'The authorization URL has not yet been generated.');
+ } else if (_state == _FINISHED_STATE) {
+ throw new StateError(
+ 'The authorization code has already been received.');
+ }
+ _state = _FINISHED_STATE;
- return _handleAuthorizationCode(authorizationCode);
- });
+ return await _handleAuthorizationCode(authorizationCode);
}
/// This works just like [handleAuthorizationCode], except it doesn't validate
/// the state beforehand.
- Future<Client> _handleAuthorizationCode(String authorizationCode) {
+ Future<Client> _handleAuthorizationCode(String authorizationCode) async {
var startTime = new DateTime.now();
- return _httpClient.post(this.tokenEndpoint, body: {
+ var response = await _httpClient.post(this.tokenEndpoint, body: {
"grant_type": "authorization_code",
"code": authorizationCode,
"redirect_uri": this._redirectEndpoint.toString(),
@@ -237,12 +234,12 @@ class AuthorizationCodeGrant {
// it be configurable?
"client_id": this.identifier,
"client_secret": this.secret
- }).then((response) {
- var credentials = handleAccessTokenResponse(
- response, tokenEndpoint, startTime, _scopes);
- return new Client(
- this.identifier, this.secret, credentials, httpClient: _httpClient);
});
+
+ var credentials = handleAccessTokenResponse(
+ response, tokenEndpoint, startTime, _scopes);
+ return new Client(
+ this.identifier, this.secret, credentials, httpClient: _httpClient);
}
/// Closes the grant and frees its resources.
« no previous file with comments | « README.md ('k') | lib/src/client.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698