| 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'; | |
| 8 import 'dart:convert'; | 7 import 'dart:convert'; |
| 9 | 8 |
| 10 import 'package:http/http.dart' as http; | 9 import 'package:http/http.dart' as http; |
| 10 import 'package:http_parser/http_parser.dart'; |
| 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; |
| 19 | 19 |
| 20 /// Handles a response from the authorization server that contains an access | 20 /// Handles a response from the authorization server that contains an access |
| 21 /// token. This response format is common across several different components of | 21 /// token. This response format is common across several different components of |
| 22 /// the OAuth2 flow. | 22 /// the OAuth2 flow. |
| 23 Credentials handleAccessTokenResponse( | 23 Credentials handleAccessTokenResponse( |
| 24 http.Response response, | 24 http.Response response, |
| 25 Uri tokenEndpoint, | 25 Uri tokenEndpoint, |
| 26 DateTime startTime, | 26 DateTime startTime, |
| 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) contentType = new MediaType.parse(contentType); |
| 35 contentType = ContentType.parse(contentType); | |
| 36 } | |
| 37 | 35 |
| 38 // The spec requires a content-type of application/json, but some endpoints | 36 // The spec requires a content-type of application/json, but some endpoints |
| 39 // (e.g. Dropbox) serve it as text/javascript instead. | 37 // (e.g. Dropbox) serve it as text/javascript instead. |
| 40 validate(contentType != null && | 38 validate(contentType != null && |
| 41 (contentType.value == "application/json" || | 39 (contentType.mimeType == "application/json" || |
| 42 contentType.value == "text/javascript"), | 40 contentType.mimeType == "text/javascript"), |
| 43 'content-type was "$contentType", expected "application/json"'); | 41 'content-type was "$contentType", expected "application/json"'); |
| 44 | 42 |
| 45 var parameters; | 43 var parameters; |
| 46 try { | 44 try { |
| 47 parameters = JSON.decode(response.body); | 45 parameters = JSON.decode(response.body); |
| 48 } on FormatException catch (e) { | 46 } on FormatException catch (e) { |
| 49 validate(false, 'invalid JSON'); | 47 validate(false, 'invalid JSON'); |
| 50 } | 48 } |
| 51 | 49 |
| 52 for (var requiredParameter in ['access_token', 'token_type']) { | 50 for (var requiredParameter in ['access_token', 'token_type']) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 if (response.statusCode != 400 && response.statusCode != 401) { | 96 if (response.statusCode != 400 && response.statusCode != 401) { |
| 99 var reason = ''; | 97 var reason = ''; |
| 100 if (response.reasonPhrase != null && !response.reasonPhrase.isEmpty) { | 98 if (response.reasonPhrase != null && !response.reasonPhrase.isEmpty) { |
| 101 ' ${response.reasonPhrase}'; | 99 ' ${response.reasonPhrase}'; |
| 102 } | 100 } |
| 103 throw new FormatException('OAuth request for "$tokenEndpoint" failed ' | 101 throw new FormatException('OAuth request for "$tokenEndpoint" failed ' |
| 104 'with status ${response.statusCode}$reason.\n\n${response.body}'); | 102 'with status ${response.statusCode}$reason.\n\n${response.body}'); |
| 105 } | 103 } |
| 106 | 104 |
| 107 var contentType = response.headers['content-type']; | 105 var contentType = response.headers['content-type']; |
| 108 if (contentType != null) { | 106 if (contentType != null) contentType = new MediaType.parse(contentType); |
| 109 contentType = ContentType.parse(contentType); | 107 validate(contentType != null && contentType.mimeType == "application/json", |
| 110 } | |
| 111 validate(contentType != null && contentType.value == "application/json", | |
| 112 'content-type was "$contentType", expected "application/json"'); | 108 'content-type was "$contentType", expected "application/json"'); |
| 113 | 109 |
| 114 var parameters; | 110 var parameters; |
| 115 try { | 111 try { |
| 116 parameters = JSON.decode(response.body); | 112 parameters = JSON.decode(response.body); |
| 117 } on FormatException catch (e) { | 113 } on FormatException catch (e) { |
| 118 validate(false, 'invalid JSON'); | 114 validate(false, 'invalid JSON'); |
| 119 } | 115 } |
| 120 | 116 |
| 121 validate(parameters.containsKey('error'), | 117 validate(parameters.containsKey('error'), |
| (...skipping 16 matching lines...) Expand all Loading... |
| 138 | 134 |
| 139 void _validate( | 135 void _validate( |
| 140 http.Response response, | 136 http.Response response, |
| 141 Uri tokenEndpoint, | 137 Uri tokenEndpoint, |
| 142 bool condition, | 138 bool condition, |
| 143 String message) { | 139 String message) { |
| 144 if (condition) return; | 140 if (condition) return; |
| 145 throw new FormatException('Invalid OAuth response for "$tokenEndpoint": ' | 141 throw new FormatException('Invalid OAuth response for "$tokenEndpoint": ' |
| 146 '$message.\n\n${response.body}'); | 142 '$message.\n\n${response.body}'); |
| 147 } | 143 } |
| OLD | NEW |