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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:collection'; | 6 import 'dart:collection'; |
7 import 'dart:convert'; | 7 import 'dart:convert'; |
8 | 8 |
9 import 'package:http/http.dart' as http; | 9 import 'package:http/http.dart' as http; |
10 | 10 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 if (expiration != null) { | 125 if (expiration != null) { |
126 validate(expiration is int, | 126 validate(expiration is int, |
127 'field "expiration" was not an int, was "$expiration"'); | 127 'field "expiration" was not an int, was "$expiration"'); |
128 expiration = new DateTime.fromMillisecondsSinceEpoch(expiration); | 128 expiration = new DateTime.fromMillisecondsSinceEpoch(expiration); |
129 } | 129 } |
130 | 130 |
131 return new Credentials( | 131 return new Credentials( |
132 parsed['accessToken'], | 132 parsed['accessToken'], |
133 refreshToken: parsed['refreshToken'], | 133 refreshToken: parsed['refreshToken'], |
134 tokenEndpoint: tokenEndpoint, | 134 tokenEndpoint: tokenEndpoint, |
135 scopes: scopes, | 135 scopes: (scopes as List).map((scope) => scope as String), |
136 expiration: expiration); | 136 expiration: expiration); |
137 } | 137 } |
138 | 138 |
139 /// Serializes a set of credentials to JSON. | 139 /// Serializes a set of credentials to JSON. |
140 /// | 140 /// |
141 /// Nothing is guaranteed about the output except that it's valid JSON and | 141 /// Nothing is guaranteed about the output except that it's valid JSON and |
142 /// compatible with [Credentials.toJson]. | 142 /// compatible with [Credentials.toJson]. |
143 String toJson() => JSON.encode({ | 143 String toJson() => JSON.encode({ |
144 'accessToken': accessToken, | 144 'accessToken': accessToken, |
145 'refreshToken': refreshToken, | 145 'refreshToken': refreshToken, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 | 177 |
178 var startTime = new DateTime.now(); | 178 var startTime = new DateTime.now(); |
179 if (refreshToken == null) { | 179 if (refreshToken == null) { |
180 throw new StateError("Can't refresh credentials without a refresh " | 180 throw new StateError("Can't refresh credentials without a refresh " |
181 "token."); | 181 "token."); |
182 } else if (tokenEndpoint == null) { | 182 } else if (tokenEndpoint == null) { |
183 throw new StateError("Can't refresh credentials without a token " | 183 throw new StateError("Can't refresh credentials without a token " |
184 "endpoint."); | 184 "endpoint."); |
185 } | 185 } |
186 | 186 |
187 var headers = {}; | 187 var headers = <String, String>{}; |
188 | 188 |
189 var body = { | 189 var body = { |
190 "grant_type": "refresh_token", | 190 "grant_type": "refresh_token", |
191 "refresh_token": refreshToken | 191 "refresh_token": refreshToken |
192 }; | 192 }; |
193 if (!scopes.isEmpty) body["scope"] = scopes.join(' '); | 193 if (!scopes.isEmpty) body["scope"] = scopes.join(' '); |
194 | 194 |
195 if (basicAuth && secret != null) { | 195 if (basicAuth && secret != null) { |
196 headers["Authorization"] = basicAuthHeader(identifier, secret); | 196 headers["Authorization"] = basicAuthHeader(identifier, secret); |
197 } else { | 197 } else { |
(...skipping 10 matching lines...) Expand all Loading... |
208 // we should re-use the one we already have. | 208 // we should re-use the one we already have. |
209 if (credentials.refreshToken != null) return credentials; | 209 if (credentials.refreshToken != null) return credentials; |
210 return new Credentials( | 210 return new Credentials( |
211 credentials.accessToken, | 211 credentials.accessToken, |
212 refreshToken: this.refreshToken, | 212 refreshToken: this.refreshToken, |
213 tokenEndpoint: credentials.tokenEndpoint, | 213 tokenEndpoint: credentials.tokenEndpoint, |
214 scopes: credentials.scopes, | 214 scopes: credentials.scopes, |
215 expiration: credentials.expiration); | 215 expiration: credentials.expiration); |
216 } | 216 } |
217 } | 217 } |
OLD | NEW |