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:convert'; | 8 import 'dart:convert'; |
9 | 9 |
10 import 'package:http/http.dart' as http; | 10 import 'package:http/http.dart' as http; |
(...skipping 16 matching lines...) Expand all Loading... |
27 List<String> scopes) { | 27 List<String> scopes) { |
28 if (response.statusCode != 200) _handleErrorResponse(response, tokenEndpoint); | 28 if (response.statusCode != 200) _handleErrorResponse(response, tokenEndpoint); |
29 | 29 |
30 void validate(bool condition, String message) => | 30 void validate(bool condition, String message) => |
31 _validate(response, tokenEndpoint, condition, message); | 31 _validate(response, tokenEndpoint, condition, message); |
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" || co
ntentType.value == "text/javascript"), |
38 'content-type was "$contentType", expected "application/json"'); | 38 'content-type was "$contentType", expected "application/json" or "text/jav
ascript"'); |
39 | 39 |
40 var parameters; | 40 var parameters; |
41 try { | 41 try { |
42 parameters = JSON.decode(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), |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 |