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
use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// The bearer token type.
///
/// See [RFC 6750](http://tools.ietf.org/html/rfc6750).
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
pub struct Bearer {
    pub access_token: String,
    pub token_type: String,
    pub scope: Option<String>,
    pub state: Option<String>,
    pub refresh_token: Option<String>,
    pub expires_in: Option<u64>,
    pub id_token: Option<String>,
    #[serde(flatten)]
    pub extra: Option<HashMap<String, serde_json::Value>>,
}

/// Manages bearer tokens along with their expiration times.
pub struct TemporalBearerGuard {
    bearer: Bearer,
    expires_at: Option<DateTime<Utc>>,
}

impl TemporalBearerGuard {
    pub fn expired(&self) -> bool {
        self.expires_at
            .map(|expires_at| Utc::now() >= expires_at)
            .unwrap_or_default()
    }

    pub fn expires_at(&self) -> Option<DateTime<Utc>> {
        self.expires_at
    }
}

impl AsRef<Bearer> for TemporalBearerGuard {
    fn as_ref(&self) -> &Bearer {
        &self.bearer
    }
}

impl From<Bearer> for TemporalBearerGuard {
    fn from(bearer: Bearer) -> Self {
        let expires_at = bearer
            .expires_in
            .map(|expires_in| Utc::now() + Duration::seconds(expires_in as i64));
        Self { bearer, expires_at }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn from_successful_response() {
        let json = r#"
        {
            "access_token":"2YotnFZFEjr1zCsicMWpAA",
            "token_type":"example",
            "expires_in":3600,
            "refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA",
            "example_parameter":"example_value"
        }
        "#;
        let bearer: Bearer = serde_json::from_str(json).unwrap();
        assert_eq!("2YotnFZFEjr1zCsicMWpAA", bearer.access_token);
        assert_eq!("example", bearer.token_type);
        assert_eq!(Some(3600), bearer.expires_in);
        assert_eq!(Some("tGzv3JOkF0XG5Qx2TlKWIA".into()), bearer.refresh_token);
        assert_eq!(
            Some(
                [("example_parameter".into(), "example_value".into())]
                    .into_iter()
                    .collect()
            ),
            bearer.extra
        );
    }

    #[test]
    fn from_response_refresh() {
        let json = r#"
            {
                "token_type":"Bearer",
                "access_token":"aaaaaaaa",
                "expires_in":3600,
                "refresh_token":"bbbbbbbb"
            }
        "#;
        let bearer: Bearer = serde_json::from_str(json).unwrap();
        assert_eq!("aaaaaaaa", bearer.access_token);
        assert_eq!(None, bearer.scope);
        assert_eq!(Some("bbbbbbbb".into()), bearer.refresh_token);
        assert_eq!(Some(3600), bearer.expires_in);
    }

    #[test]
    fn from_response_static() {
        let json = r#"
            {
                "token_type":"Bearer",
                "access_token":"aaaaaaaa"
            }
        "#;
        let bearer: Bearer = serde_json::from_str(json).unwrap();
        assert_eq!("aaaaaaaa", bearer.access_token);
        assert_eq!(None, bearer.scope);
        assert_eq!(None, bearer.refresh_token);
        assert_eq!(None, bearer.expires_in);
    }
}