| 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:json' as JSON; |
| 9 | 9 |
| 10 import 'package:http/http.dart' as http; | 10 import 'package:http/http.dart' as http; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 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.parse(response.body); |
| 43 } catch (e) { | 43 } on FormatException catch (e) { |
| 44 // TODO(nweiz): narrow this catch clause once issue 6775 is fixed. | |
| 45 validate(false, 'invalid JSON'); | 44 validate(false, 'invalid JSON'); |
| 46 } | 45 } |
| 47 | 46 |
| 48 for (var requiredParameter in ['access_token', 'token_type']) { | 47 for (var requiredParameter in ['access_token', 'token_type']) { |
| 49 validate(parameters.containsKey(requiredParameter), | 48 validate(parameters.containsKey(requiredParameter), |
| 50 'did not contain required parameter "$requiredParameter"'); | 49 'did not contain required parameter "$requiredParameter"'); |
| 51 validate(parameters[requiredParameter] is String, | 50 validate(parameters[requiredParameter] is String, |
| 52 'required parameter "$requiredParameter" was not a string, was ' | 51 'required parameter "$requiredParameter" was not a string, was ' |
| 53 '"${parameters[requiredParameter]}"'); | 52 '"${parameters[requiredParameter]}"'); |
| 54 } | 53 } |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 var contentType = response.headers['content-type']; | 102 var contentType = response.headers['content-type']; |
| 104 if (contentType != null) { | 103 if (contentType != null) { |
| 105 contentType = ContentType.parse(contentType); | 104 contentType = ContentType.parse(contentType); |
| 106 } | 105 } |
| 107 validate(contentType != null && contentType.value == "application/json", | 106 validate(contentType != null && contentType.value == "application/json", |
| 108 'content-type was "$contentType", expected "application/json"'); | 107 'content-type was "$contentType", expected "application/json"'); |
| 109 | 108 |
| 110 var parameters; | 109 var parameters; |
| 111 try { | 110 try { |
| 112 parameters = JSON.parse(response.body); | 111 parameters = JSON.parse(response.body); |
| 113 } catch (e) { | 112 } on FormatException catch (e) { |
| 114 // TODO(nweiz): narrow this catch clause once issue 6775 is fixed. | |
| 115 validate(false, 'invalid JSON'); | 113 validate(false, 'invalid JSON'); |
| 116 } | 114 } |
| 117 | 115 |
| 118 validate(parameters.containsKey('error'), | 116 validate(parameters.containsKey('error'), |
| 119 'did not contain required parameter "error"'); | 117 'did not contain required parameter "error"'); |
| 120 validate(parameters["error"] is String, | 118 validate(parameters["error"] is String, |
| 121 'required parameter "error" was not a string, was ' | 119 'required parameter "error" was not a string, was ' |
| 122 '"${parameters["error"]}"'); | 120 '"${parameters["error"]}"'); |
| 123 | 121 |
| 124 for (var name in ['error_description', 'error_uri']) { | 122 for (var name in ['error_description', 'error_uri']) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 135 | 133 |
| 136 void _validate( | 134 void _validate( |
| 137 http.Response response, | 135 http.Response response, |
| 138 Uri tokenEndpoint, | 136 Uri tokenEndpoint, |
| 139 bool condition, | 137 bool condition, |
| 140 String message) { | 138 String message) { |
| 141 if (condition) return; | 139 if (condition) return; |
| 142 throw new FormatException('Invalid OAuth response for "$tokenEndpoint": ' | 140 throw new FormatException('Invalid OAuth response for "$tokenEndpoint": ' |
| 143 '$message.\n\n${response.body}'); | 141 '$message.\n\n${response.body}'); |
| 144 } | 142 } |
| OLD | NEW |