1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::error::Error;
use std::fmt;
use serde_json::Value;
use crate::client::response::{FromResponse, ParseError};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OAuth2ErrorCode {
InvalidRequest,
InvalidClient,
InvalidGrant,
UnauthorizedClient,
UnsupportedGrantType,
InvalidScope,
Unrecognized(String),
}
impl<'a> From<&'a str> for OAuth2ErrorCode {
fn from(s: &str) -> OAuth2ErrorCode {
match s {
"invalid_request" => OAuth2ErrorCode::InvalidRequest,
"invalid_client" => OAuth2ErrorCode::InvalidClient,
"invalid_grant" => OAuth2ErrorCode::InvalidGrant,
"unauthorized_client" => OAuth2ErrorCode::UnauthorizedClient,
"unsupported_grant_type" => OAuth2ErrorCode::UnsupportedGrantType,
"invalid_scope" => OAuth2ErrorCode::InvalidScope,
s => OAuth2ErrorCode::Unrecognized(s.to_owned()),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OAuth2Error {
pub code: OAuth2ErrorCode,
pub description: Option<String>,
pub uri: Option<String>,
}
impl fmt::Display for OAuth2Error {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{:?}", self.code)?;
if let Some(ref description) = self.description {
write!(f, ": {}", description)?;
}
if let Some(ref uri) = self.uri {
write!(f, " ({})", uri)?;
}
Ok(())
}
}
impl Error for OAuth2Error {
fn description(&self) -> &str { "OAuth 2.0 API error" }
}
impl FromResponse for OAuth2Error {
fn from_response(json: &Value) -> Result<Self, ParseError> {
let obj = json.as_object().ok_or(ParseError::ExpectedType("object"))?;
let code = obj.get("error")
.and_then(Value::as_str)
.ok_or(ParseError::ExpectedFieldType("error", "string"))?;
let description = obj.get("error_description").and_then(Value::as_str);
let uri = obj.get("error_uri").and_then(Value::as_str);
Ok(OAuth2Error {
code: code.into(),
description: description.map(Into::into),
uri: uri.map(Into::into),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn from_response_empty() {
let json = "{}".parse().unwrap();
assert_eq!(
ParseError::ExpectedFieldType("error", "string"),
OAuth2Error::from_response(&json).unwrap_err()
);
}
#[test]
fn from_response() {
let json = r#"{"error":"invalid_request"}"#.parse().unwrap();
assert_eq!(
OAuth2Error {
code: OAuth2ErrorCode::InvalidRequest,
description: None,
uri: None,
},
OAuth2Error::from_response(&json).unwrap()
);
}
#[test]
fn from_response_with_description() {
let json = r#"{"error":"invalid_request","error_description":"foo"}"#
.parse()
.unwrap();
assert_eq!(
OAuth2Error {
code: OAuth2ErrorCode::InvalidRequest,
description: Some(String::from("foo")),
uri: None,
},
OAuth2Error::from_response(&json).unwrap()
);
}
#[test]
fn from_response_with_uri() {
let json = r#"{"error":"invalid_request","error_uri":"http://example.com"}"#
.parse()
.unwrap();
assert_eq!(
OAuth2Error {
code: OAuth2ErrorCode::InvalidRequest,
description: None,
uri: Some(String::from("http://example.com")),
},
OAuth2Error::from_response(&json).unwrap()
);
}
}