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:convert'; |
9 | 9 |
10 import 'package:http/http.dart' as http; | 10 import 'package:http/http.dart' as http; |
11 | 11 |
12 import 'handle_access_token_response.dart'; | 12 import 'handle_access_token_response.dart'; |
13 import 'utils.dart'; | 13 import 'utils.dart'; |
14 | 14 |
15 /// 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 |
16 /// 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 |
17 /// safely persisted across multiple runs of the program. | 17 /// safely persisted across multiple runs of the program. |
18 /// | 18 /// |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 /// [FormatException] if the JSON is incorrectly formatted. | 73 /// [FormatException] if the JSON is incorrectly formatted. |
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.decode(json); |
84 } on FormatException catch (e) { | 84 } on FormatException catch (e) { |
85 validate(false, 'invalid JSON'); | 85 validate(false, 'invalid JSON'); |
86 } | 86 } |
87 | 87 |
88 validate(parsed is Map, 'was not a JSON map'); | 88 validate(parsed is Map, 'was not a JSON map'); |
89 validate(parsed.containsKey('accessToken'), | 89 validate(parsed.containsKey('accessToken'), |
90 'did not contain required field "accessToken"'); | 90 'did not contain required field "accessToken"'); |
91 validate(parsed['accessToken'] is String, | 91 validate(parsed['accessToken'] is String, |
92 'required field "accessToken" was not a string, was ' | 92 'required field "accessToken" was not a string, was ' |
93 '${parsed["accessToken"]}'); | 93 '${parsed["accessToken"]}'); |
(...skipping 24 matching lines...) Expand all Loading... |
118 parsed['accessToken'], | 118 parsed['accessToken'], |
119 parsed['refreshToken'], | 119 parsed['refreshToken'], |
120 tokenEndpoint, | 120 tokenEndpoint, |
121 scopes, | 121 scopes, |
122 expiration); | 122 expiration); |
123 } | 123 } |
124 | 124 |
125 /// Serializes a set of credentials to JSON. Nothing is guaranteed about the | 125 /// Serializes a set of credentials to JSON. Nothing is guaranteed about the |
126 /// output except that it's valid JSON and compatible with | 126 /// output except that it's valid JSON and compatible with |
127 /// [Credentials.toJson]. | 127 /// [Credentials.toJson]. |
128 String toJson() => JSON.stringify({ | 128 String toJson() => JSON.encode({ |
129 'accessToken': accessToken, | 129 'accessToken': accessToken, |
130 'refreshToken': refreshToken, | 130 'refreshToken': refreshToken, |
131 'tokenEndpoint': tokenEndpoint == null ? null : tokenEndpoint.toString(), | 131 'tokenEndpoint': tokenEndpoint == null ? null : tokenEndpoint.toString(), |
132 'scopes': scopes, | 132 'scopes': scopes, |
133 'expiration': expiration == null ? null : expiration.millisecondsSinceEpoch | 133 'expiration': expiration == null ? null : expiration.millisecondsSinceEpoch |
134 }); | 134 }); |
135 | 135 |
136 /// Returns a new set of refreshed credentials. See [Client.identifier] and | 136 /// Returns a new set of refreshed credentials. See [Client.identifier] and |
137 /// [Client.secret] for explanations of those parameters. | 137 /// [Client.secret] for explanations of those parameters. |
138 /// | 138 /// |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 if (credentials.refreshToken != null) return credentials; | 183 if (credentials.refreshToken != null) return credentials; |
184 return new Credentials( | 184 return new Credentials( |
185 credentials.accessToken, | 185 credentials.accessToken, |
186 this.refreshToken, | 186 this.refreshToken, |
187 credentials.tokenEndpoint, | 187 credentials.tokenEndpoint, |
188 credentials.scopes, | 188 credentials.scopes, |
189 credentials.expiration); | 189 credentials.expiration); |
190 }); | 190 }); |
191 } | 191 } |
192 } | 192 } |
OLD | NEW |