| 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:async'; | 7 import 'dart:async'; |
| 8 import 'dart:json' as JSON; | 8 import 'dart:json' as JSON; |
| 9 | 9 |
| 10 import 'package:http/http.dart' as http; | 10 import 'package:http/http.dart' as http; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 factory Credentials.fromJson(String json) { | 74 factory Credentials.fromJson(String json) { |
| 75 void validate(bool condition, String message) { | 75 void validate(bool condition, String message) { |
| 76 if (condition) return; | 76 if (condition) return; |
| 77 throw new FormatException( | 77 throw new FormatException( |
| 78 "Failed to load credentials: $message.\n\n$json"); | 78 "Failed to load credentials: $message.\n\n$json"); |
| 79 } | 79 } |
| 80 | 80 |
| 81 var parsed; | 81 var parsed; |
| 82 try { | 82 try { |
| 83 parsed = JSON.parse(json); | 83 parsed = JSON.parse(json); |
| 84 } catch (e) { | 84 } on FormatException catch (e) { |
| 85 // TODO(nweiz): narrow this catch clause once issue 6775 is fixed. | |
| 86 validate(false, 'invalid JSON'); | 85 validate(false, 'invalid JSON'); |
| 87 } | 86 } |
| 88 | 87 |
| 89 validate(parsed is Map, 'was not a JSON map'); | 88 validate(parsed is Map, 'was not a JSON map'); |
| 90 validate(parsed.containsKey('accessToken'), | 89 validate(parsed.containsKey('accessToken'), |
| 91 'did not contain required field "accessToken"'); | 90 'did not contain required field "accessToken"'); |
| 92 validate(parsed['accessToken'] is String, | 91 validate(parsed['accessToken'] is String, |
| 93 'required field "accessToken" was not a string, was ' | 92 'required field "accessToken" was not a string, was ' |
| 94 '${parsed["accessToken"]}'); | 93 '${parsed["accessToken"]}'); |
| 95 | 94 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 if (credentials.refreshToken != null) return credentials; | 183 if (credentials.refreshToken != null) return credentials; |
| 185 return new Credentials( | 184 return new Credentials( |
| 186 credentials.accessToken, | 185 credentials.accessToken, |
| 187 this.refreshToken, | 186 this.refreshToken, |
| 188 credentials.tokenEndpoint, | 187 credentials.tokenEndpoint, |
| 189 credentials.scopes, | 188 credentials.scopes, |
| 190 credentials.expiration); | 189 credentials.expiration); |
| 191 }); | 190 }); |
| 192 } | 191 } |
| 193 } | 192 } |
| OLD | NEW |