inth_oauth2/token/
bearer.rs1use serde_json::Value;
2
3use client::response::{FromResponse, ParseError};
4use token::{Token, Lifetime};
5
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
10pub struct Bearer<L: Lifetime> {
11 access_token: String,
12 scope: Option<String>,
13 lifetime: L,
14}
15
16impl<L: Lifetime> Token<L> for Bearer<L> {
17 fn access_token(&self) -> &str {
18 &self.access_token
19 }
20 fn scope(&self) -> Option<&str> {
21 self.scope.as_ref().map(|s| &s[..])
22 }
23 fn lifetime(&self) -> &L {
24 &self.lifetime
25 }
26}
27
28impl<L: Lifetime> Bearer<L> {
29 fn from_response_and_lifetime(json: &Value, lifetime: L) -> Result<Self, ParseError> {
30 let obj = json.as_object().ok_or(ParseError::ExpectedType("object"))?;
31
32 let token_type = obj.get("token_type")
33 .and_then(Value::as_str)
34 .ok_or(ParseError::ExpectedFieldType("token_type", "string"))?;
35 if token_type != "Bearer" && token_type != "bearer" {
36 return Err(ParseError::ExpectedFieldValue("token_type", "Bearer"));
37 }
38
39 let access_token = obj.get("access_token")
40 .and_then(Value::as_str)
41 .ok_or(ParseError::ExpectedFieldType("access_token", "string"))?;
42 let scope = obj.get("scope").and_then(Value::as_str);
43
44 Ok(Bearer {
45 access_token: access_token.into(),
46 scope: scope.map(Into::into),
47 lifetime: lifetime,
48 })
49 }
50}
51
52impl<L: Lifetime> FromResponse for Bearer<L> {
53 fn from_response(json: &Value) -> Result<Self, ParseError> {
54 let lifetime = FromResponse::from_response(json)?;
55 Bearer::from_response_and_lifetime(json, lifetime)
56 }
57
58 fn from_response_inherit(json: &Value, prev: &Self) -> Result<Self, ParseError> {
59 let lifetime = FromResponse::from_response_inherit(json, &prev.lifetime)?;
60 Bearer::from_response_and_lifetime(json, lifetime)
61 }
62}
63
64#[cfg(test)]
65mod tests {
66 use chrono::{Utc, Duration};
67
68 use client::response::{FromResponse, ParseError};
69 use token::{Static, Refresh};
70 use super::Bearer;
71
72 #[test]
73 fn from_response_with_invalid_token_type() {
74 let json = r#"{"token_type":"MAC","access_token":"aaaaaaaa"}"#.parse().unwrap();
75 assert_eq!(
76 ParseError::ExpectedFieldValue("token_type", "Bearer"),
77 Bearer::<Static>::from_response(&json).unwrap_err()
78 );
79 }
80
81 #[test]
82 fn from_response_capital_b() {
83 let json = r#"{"token_type":"Bearer","access_token":"aaaaaaaa"}"#.parse().unwrap();
84 assert_eq!(
85 Bearer {
86 access_token: String::from("aaaaaaaa"),
87 scope: None,
88 lifetime: Static,
89 },
90 Bearer::<Static>::from_response(&json).unwrap()
91 );
92 }
93
94 #[test]
95 fn from_response_little_b() {
96 let json = r#"{"token_type":"bearer","access_token":"aaaaaaaa"}"#.parse().unwrap();
97 assert_eq!(
98 Bearer {
99 access_token: String::from("aaaaaaaa"),
100 scope: None,
101 lifetime: Static,
102 },
103 Bearer::<Static>::from_response(&json).unwrap()
104 );
105 }
106
107 #[test]
108 fn from_response_with_scope() {
109 let json = r#"{"token_type":"Bearer","access_token":"aaaaaaaa","scope":"foo"}"#
110 .parse()
111 .unwrap();
112 assert_eq!(
113 Bearer {
114 access_token: String::from("aaaaaaaa"),
115 scope: Some(String::from("foo")),
116 lifetime: Static,
117 },
118 Bearer::<Static>::from_response(&json).unwrap()
119 );
120 }
121
122 #[test]
123 fn from_response_refresh() {
124 let json = r#"
125 {
126 "token_type":"Bearer",
127 "access_token":"aaaaaaaa",
128 "expires_in":3600,
129 "refresh_token":"bbbbbbbb"
130 }
131 "#.parse().unwrap();
132 let bearer = Bearer::<Refresh>::from_response(&json).unwrap();
133 assert_eq!("aaaaaaaa", bearer.access_token);
134 assert_eq!(None, bearer.scope);
135 let refresh = bearer.lifetime;
136 assert_eq!("bbbbbbbb", refresh.refresh_token());
137 assert!(refresh.expires() > &Utc::now());
138 assert!(refresh.expires() <= &(Utc::now() + Duration::seconds(3600)));
139 }
140
141 #[test]
142 fn from_response_inherit_refresh() {
143 let json = r#"
144 {
145 "token_type":"Bearer",
146 "access_token":"aaaaaaaa",
147 "expires_in":3600,
148 "refresh_token":"bbbbbbbb"
149 }
150 "#.parse().unwrap();
151 let prev = Bearer::<Refresh>::from_response(&json).unwrap();
152
153 let json = r#"
154 {
155 "token_type":"Bearer",
156 "access_token":"cccccccc",
157 "expires_in":3600
158 }
159 "#.parse().unwrap();
160 let bearer = Bearer::<Refresh>::from_response_inherit(&json, &prev).unwrap();
161 assert_eq!("cccccccc", bearer.access_token);
162 assert_eq!(None, bearer.scope);
163 let refresh = bearer.lifetime;
164 assert_eq!("bbbbbbbb", refresh.refresh_token());
165 assert!(refresh.expires() > &Utc::now());
166 assert!(refresh.expires() <= &(Utc::now() + Duration::seconds(3600)));
167 }
168}