Skip to content

Commit

Permalink
fix(json): assure we understand json errors
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Byron committed May 1, 2015
1 parent df5fdc4 commit b08b239
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "yup-oauth2"
version = "0.4.1"
version = "0.4.2"
authors = ["Sebastian Thiel <[email protected]>"]
repository = "https://github.com/Byron/yup-oauth2"
description = "A partial oauth2 implementation, providing the 'device' authorization flow"
Expand Down
4 changes: 2 additions & 2 deletions etc/sublime-text/yup-oauth2-rs.sublime-project
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
],
},
],
"SublimeLinter":
"SublimeLinter":
{
"linters":
{
"rust": {
"@disable": false,
"@disable": true,
"args": [],
"crate-root": null,
"excludes": [],
Expand Down
3 changes: 2 additions & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
}
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down

0 comments on commit b08b239

Please sign in to comment.