| 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 credentials; | 5 library credentials; |
| 6 | 6 |
| 7 import 'dart:json'; | 7 import 'dart:async'; |
| 8 import 'dart:json' as JSON; |
| 8 import 'dart:uri'; | 9 import 'dart:uri'; |
| 9 | 10 |
| 10 import '../../../http/lib/http.dart' as http; | 11 import '../../../http/lib/http.dart' as http; |
| 11 import 'handle_access_token_response.dart'; | 12 import 'handle_access_token_response.dart'; |
| 12 import 'utils.dart'; | 13 import 'utils.dart'; |
| 13 | 14 |
| 14 /// Credentials that prove that a client is allowed to access a resource on the | 15 /// Credentials that prove that a client is allowed to access a resource on the |
| 15 /// resource owner's behalf. These credentials are long-lasting and can be | 16 /// resource owner's behalf. These credentials are long-lasting and can be |
| 16 /// safely persisted across multiple runs of the program. | 17 /// safely persisted across multiple runs of the program. |
| 17 /// | 18 /// |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 String identifier, | 146 String identifier, |
| 146 String secret, | 147 String secret, |
| 147 {List<String> newScopes, | 148 {List<String> newScopes, |
| 148 http.Client httpClient}) { | 149 http.Client httpClient}) { |
| 149 var scopes = this.scopes; | 150 var scopes = this.scopes; |
| 150 if (newScopes != null) scopes = newScopes; | 151 if (newScopes != null) scopes = newScopes; |
| 151 if (scopes == null) scopes = <String>[]; | 152 if (scopes == null) scopes = <String>[]; |
| 152 if (httpClient == null) httpClient = new http.Client(); | 153 if (httpClient == null) httpClient = new http.Client(); |
| 153 | 154 |
| 154 var startTime = new Date.now(); | 155 var startTime = new Date.now(); |
| 155 return async.chain((_) { | 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 } |
| 163 | 164 |
| 164 var fields = { | 165 var fields = { |
| 165 "grant_type": "refresh_token", | 166 "grant_type": "refresh_token", |
| 166 "refresh_token": refreshToken, | 167 "refresh_token": refreshToken, |
| 167 // TODO(nweiz): the spec recommends that HTTP basic auth be used in | 168 // TODO(nweiz): the spec recommends that HTTP basic auth be used in |
| 168 // preference to form parameters, but Google doesn't support that. | 169 // preference to form parameters, but Google doesn't support that. |
| 169 // Should it be configurable? | 170 // Should it be configurable? |
| 170 "client_id": identifier, | 171 "client_id": identifier, |
| 171 "client_secret": secret | 172 "client_secret": secret |
| 172 }; | 173 }; |
| 173 if (!scopes.isEmpty) fields["scope"] = Strings.join(scopes, ' '); | 174 if (!scopes.isEmpty) fields["scope"] = Strings.join(scopes, ' '); |
| 174 | 175 |
| 175 return httpClient.post(tokenEndpoint, fields: fields); | 176 return httpClient.post(tokenEndpoint, fields: fields); |
| 176 }).transform((response) { | 177 }).then((response) { |
| 177 return handleAccessTokenResponse( | 178 return handleAccessTokenResponse( |
| 178 response, tokenEndpoint, startTime, scopes); | 179 response, tokenEndpoint, startTime, scopes); |
| 179 }).transform((credentials) { | 180 }).then((credentials) { |
| 180 // The authorization server may issue a new refresh token. If it doesn't, | 181 // The authorization server may issue a new refresh token. If it doesn't, |
| 181 // we should re-use the one we already have. | 182 // we should re-use the one we already have. |
| 182 if (credentials.refreshToken != null) return credentials; | 183 if (credentials.refreshToken != null) return credentials; |
| 183 return new Credentials( | 184 return new Credentials( |
| 184 credentials.accessToken, | 185 credentials.accessToken, |
| 185 this.refreshToken, | 186 this.refreshToken, |
| 186 credentials.tokenEndpoint, | 187 credentials.tokenEndpoint, |
| 187 credentials.scopes, | 188 credentials.scopes, |
| 188 credentials.expiration); | 189 credentials.expiration); |
| 189 }); | 190 }); |
| 190 } | 191 } |
| 191 } | 192 } |
| OLD | NEW |