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 handle_access_token_response; | 5 library handle_access_token_response; |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
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 'credentials.dart'; | 12 import 'credentials.dart'; |
13 import 'authorization_exception.dart'; | 13 import 'authorization_exception.dart'; |
14 | 14 |
15 /// The amount of time, in seconds, to add as a "grace period" for credential | 15 /// The amount of time, in seconds, to add as a "grace period" for credential |
16 /// expiration. This allows credential expiration checks to remain valid for a | 16 /// expiration. This allows credential expiration checks to remain valid for a |
17 /// reasonable amount of time. | 17 /// reasonable amount of time. |
18 const _EXPIRATION_GRACE = 10; | 18 const _EXPIRATION_GRACE = 10; |
(...skipping 13 matching lines...) Expand all Loading... |
32 | 32 |
33 var contentType = response.headers['content-type']; | 33 var contentType = response.headers['content-type']; |
34 if (contentType != null) { | 34 if (contentType != null) { |
35 contentType = ContentType.parse(contentType); | 35 contentType = ContentType.parse(contentType); |
36 } | 36 } |
37 validate(contentType != null && contentType.value == "application/json", | 37 validate(contentType != null && contentType.value == "application/json", |
38 'content-type was "$contentType", expected "application/json"'); | 38 'content-type was "$contentType", expected "application/json"'); |
39 | 39 |
40 var parameters; | 40 var parameters; |
41 try { | 41 try { |
42 parameters = JSON.parse(response.body); | 42 parameters = JSON.decode(response.body); |
43 } on FormatException catch (e) { | 43 } on FormatException catch (e) { |
44 validate(false, 'invalid JSON'); | 44 validate(false, 'invalid JSON'); |
45 } | 45 } |
46 | 46 |
47 for (var requiredParameter in ['access_token', 'token_type']) { | 47 for (var requiredParameter in ['access_token', 'token_type']) { |
48 validate(parameters.containsKey(requiredParameter), | 48 validate(parameters.containsKey(requiredParameter), |
49 'did not contain required parameter "$requiredParameter"'); | 49 'did not contain required parameter "$requiredParameter"'); |
50 validate(parameters[requiredParameter] is String, | 50 validate(parameters[requiredParameter] is String, |
51 'required parameter "$requiredParameter" was not a string, was ' | 51 'required parameter "$requiredParameter" was not a string, was ' |
52 '"${parameters[requiredParameter]}"'); | 52 '"${parameters[requiredParameter]}"'); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 | 101 |
102 var contentType = response.headers['content-type']; | 102 var contentType = response.headers['content-type']; |
103 if (contentType != null) { | 103 if (contentType != null) { |
104 contentType = ContentType.parse(contentType); | 104 contentType = ContentType.parse(contentType); |
105 } | 105 } |
106 validate(contentType != null && contentType.value == "application/json", | 106 validate(contentType != null && contentType.value == "application/json", |
107 'content-type was "$contentType", expected "application/json"'); | 107 'content-type was "$contentType", expected "application/json"'); |
108 | 108 |
109 var parameters; | 109 var parameters; |
110 try { | 110 try { |
111 parameters = JSON.parse(response.body); | 111 parameters = JSON.decode(response.body); |
112 } on FormatException catch (e) { | 112 } on FormatException catch (e) { |
113 validate(false, 'invalid JSON'); | 113 validate(false, 'invalid JSON'); |
114 } | 114 } |
115 | 115 |
116 validate(parameters.containsKey('error'), | 116 validate(parameters.containsKey('error'), |
117 'did not contain required parameter "error"'); | 117 'did not contain required parameter "error"'); |
118 validate(parameters["error"] is String, | 118 validate(parameters["error"] is String, |
119 'required parameter "error" was not a string, was ' | 119 'required parameter "error" was not a string, was ' |
120 '"${parameters["error"]}"'); | 120 '"${parameters["error"]}"'); |
121 | 121 |
(...skipping 11 matching lines...) Expand all Loading... |
133 | 133 |
134 void _validate( | 134 void _validate( |
135 http.Response response, | 135 http.Response response, |
136 Uri tokenEndpoint, | 136 Uri tokenEndpoint, |
137 bool condition, | 137 bool condition, |
138 String message) { | 138 String message) { |
139 if (condition) return; | 139 if (condition) return; |
140 throw new FormatException('Invalid OAuth response for "$tokenEndpoint": ' | 140 throw new FormatException('Invalid OAuth response for "$tokenEndpoint": ' |
141 '$message.\n\n${response.body}'); | 141 '$message.\n\n${response.body}'); |
142 } | 142 } |
OLD | NEW |