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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use serde_json::Value;

use crate::client::response::{FromResponse, ParseError};
use crate::token::{Token, Lifetime};

/// The bearer token type.
///
/// See [RFC 6750](http://tools.ietf.org/html/rfc6750).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Bearer<L: Lifetime> {
    access_token: String,
    scope: Option<String>,
    id_token: Option<String>,
    lifetime: L,
}

impl<L: Lifetime> Token<L> for Bearer<L> {
    fn access_token(&self) -> &str {
        &self.access_token
    }
    fn scope(&self) -> Option<&str> {
        self.scope.as_ref().map(|s| &s[..])
    }
    fn id_token(&self) -> Option<&str> {
        self.id_token.as_ref().map(|s| &s[..])
    }
    fn lifetime(&self) -> &L {
        &self.lifetime
    }
}

impl<L: Lifetime> Bearer<L> {
    fn from_response_and_lifetime(json: &Value, lifetime: L) -> Result<Self, ParseError> {
        let obj = json.as_object().ok_or(ParseError::ExpectedType("object"))?;

        let token_type = obj.get("token_type")
            .and_then(Value::as_str)
            .ok_or(ParseError::ExpectedFieldType("token_type", "string"))?;
        if token_type != "Bearer" && token_type != "bearer" {
            return Err(ParseError::ExpectedFieldValue("token_type", "Bearer"));
        }

        let access_token = obj.get("access_token")
            .and_then(Value::as_str)
            .ok_or(ParseError::ExpectedFieldType("access_token", "string"))?;
        let scope = obj.get("scope").and_then(Value::as_str);
        let id_token = obj.get("id_token").and_then(Value::as_str);

        Ok(Bearer {
            access_token: access_token.into(),
            scope: scope.map(Into::into),
            id_token: id_token.map(Into::into),
            lifetime,
        })
    }
}

impl<L: Lifetime> FromResponse for Bearer<L> {
    fn from_response(json: &Value) -> Result<Self, ParseError> {
        let lifetime = FromResponse::from_response(json)?;
        Bearer::from_response_and_lifetime(json, lifetime)
    }

    fn from_response_inherit(json: &Value, prev: &Self) -> Result<Self, ParseError> {
        let lifetime = FromResponse::from_response_inherit(json, &prev.lifetime)?;
        Bearer::from_response_and_lifetime(json, lifetime)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::token::{Bearer, Refresh, Static};
    use std::time::{Duration, SystemTime};

    #[test]
    fn from_response_with_invalid_token_type() {
        let json = r#"{"token_type":"MAC","access_token":"aaaaaaaa"}"#.parse().unwrap();
        assert_eq!(
            ParseError::ExpectedFieldValue("token_type", "Bearer"),
            Bearer::<Static>::from_response(&json).unwrap_err()
        );
    }

    #[test]
    fn from_response_capital_b() {
        let json = r#"{"token_type":"Bearer","access_token":"aaaaaaaa"}"#.parse().unwrap();
        assert_eq!(
            Bearer {
                access_token: String::from("aaaaaaaa"),
                scope: None,
                id_token: None,
                lifetime: Static,
            },
            Bearer::<Static>::from_response(&json).unwrap()
        );
    }

    #[test]
    fn from_response_little_b() {
        let json = r#"{"token_type":"bearer","access_token":"aaaaaaaa"}"#.parse().unwrap();
        assert_eq!(
            Bearer {
                access_token: String::from("aaaaaaaa"),
                scope: None,
                id_token: None,
                lifetime: Static,
            },
            Bearer::<Static>::from_response(&json).unwrap()
        );
    }

    #[test]
    fn from_response_with_scope() {
        let json = r#"{"token_type":"Bearer","access_token":"aaaaaaaa","scope":"foo"}"#
            .parse()
            .unwrap();
        assert_eq!(
            Bearer {
                access_token: String::from("aaaaaaaa"),
                scope: Some(String::from("foo")),
                id_token: None,
                lifetime: Static,
            },
            Bearer::<Static>::from_response(&json).unwrap()
        );
    }

    #[test]
    fn from_response_refresh() {
        let json = r#"
            {
                "token_type":"Bearer",
                "access_token":"aaaaaaaa",
                "expires_in":3600,
                "refresh_token":"bbbbbbbb"
            }
        "#.parse().unwrap();
        let bearer = Bearer::<Refresh>::from_response(&json).unwrap();
        assert_eq!("aaaaaaaa", bearer.access_token);
        assert_eq!(None, bearer.scope);
        let refresh = bearer.lifetime;
        assert_eq!("bbbbbbbb", refresh.refresh_token());
        assert!(refresh.expires() > SystemTime::now());
        assert!(refresh.expires() <= SystemTime::now() + Duration::from_secs(3600));
    }

    #[test]
    fn from_response_refresh_idtoken() {
        let json = r#"
            {
                "token_type":"Bearer",
                "access_token":"aaaaaaaa",
                "expires_in":3600,
                "refresh_token":"bbbbbbbb",
                "id_token":"zzzzzzzzz"
            }
        "#.parse().unwrap();
        let bearer = Bearer::<Refresh>::from_response(&json).unwrap();
        assert_eq!("aaaaaaaa", bearer.access_token);
        assert_eq!(None, bearer.scope);
        let refresh = bearer.lifetime;
        assert_eq!("bbbbbbbb", refresh.refresh_token());
        assert!(refresh.expires() > SystemTime::now());
        assert!(refresh.expires() <= SystemTime::now() + Duration::from_secs(3600));
        assert_eq!(Some("zzzzzzzzz".to_owned()), bearer.id_token);
    }

    #[test]
    fn from_response_inherit_refresh() {
        let json = r#"
            {
                "token_type":"Bearer",
                "access_token":"aaaaaaaa",
                "expires_in":3600,
                "refresh_token":"bbbbbbbb"
            }
        "#.parse().unwrap();
        let prev = Bearer::<Refresh>::from_response(&json).unwrap();

        let json = r#"
            {
                "token_type":"Bearer",
                "access_token":"cccccccc",
                "expires_in":3600
            }
        "#.parse().unwrap();
        let bearer = Bearer::<Refresh>::from_response_inherit(&json, &prev).unwrap();
        assert_eq!("cccccccc", bearer.access_token);
        assert_eq!(None, bearer.scope);
        let refresh = bearer.lifetime;
        assert_eq!("bbbbbbbb", refresh.refresh_token());
        assert!(refresh.expires() > SystemTime::now());
        assert!(refresh.expires() <= SystemTime::now() + Duration::from_secs(3600));
    }
}