fix(json): assure we understand json errors

We would actually fail to decode an error, and then assume it's a valid
result, unwrapping another failed attempt to decode the json string
returned by the server.

Cause seems to be that the json error structure now conains an
additional field, 'error_uri'.

* we removed a debug printing ... .
* incremented version
This commit is contained in:
Sebastian Thiel
2015-05-01 19:39:57 +02:00
parent df5fdc48a7
commit b08b239e88
5 changed files with 15 additions and 10 deletions

View File

@@ -1,7 +1,7 @@
[package]
name = "yup-oauth2"
version = "0.4.1"
version = "0.4.2"
authors = ["Sebastian Thiel <byronimo@gmail.com>"]
repository = "https://github.com/Byron/yup-oauth2"
description = "A partial oauth2 implementation, providing the 'device' authorization flow"

View File

@@ -13,12 +13,12 @@
],
},
],
"SublimeLinter":
"SublimeLinter":
{
"linters":
{
"rust": {
"@disable": false,
"@disable": true,
"args": [],
"crate-root": null,
"excludes": [],

View File

@@ -10,8 +10,9 @@ pub trait Flow {
#[derive(Deserialize)]
pub struct JsonError {
pub error: String,
pub error: Option<String>,
pub error_description: Option<String>,
pub error_uri: Option<String>,
}
/// Represents all implemented token types

View File

@@ -84,12 +84,13 @@ pub enum RequestError {
impl From<JsonError> for RequestError {
fn from(value: JsonError) -> RequestError {
match &*value.error {
let err_str = value.error.unwrap();
match &*err_str {
"invalid_client" => RequestError::InvalidClient,
"invalid_scope" => RequestError::InvalidScope(
value.error_description.unwrap_or("no description provided".to_string())
),
_ => RequestError::NegativeServerResponse(value.error, value.error_description),
_ => RequestError::NegativeServerResponse(err_str, value.error_description),
}
}
}
@@ -218,11 +219,12 @@ impl<C> DeviceFlow<C>
match json::from_str::<JsonError>(&json_str) {
Err(_) => {}, // ignore, move on
Ok(res) => {
return Err(RequestError::from(res))
if res.error.is_some() {
return Err(RequestError::from(res))
}
}
}
println!("{:?}", json_str);
let decoded: JsonData = json::from_str(&json_str).unwrap();
self.device_code = decoded.device_code;

View File

@@ -98,8 +98,10 @@ impl<C> RefreshFlow<C>
match json::from_str::<JsonError>(&json_str) {
Err(_) => {},
Ok(res) => {
self.result = RefreshResult::RefreshError(res.error, res.error_description);
return &self.result;
if let Some(err) = res.error {
self.result = RefreshResult::RefreshError(err, res.error_description);
return &self.result;
}
}
}