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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use http::{HeaderValue, header::InvalidHeaderValue};
use crate::core::{
platform::{Duration, SystemTime},
secrets::SecretString,
};
/// Represents an access token, either a `DPoP` token or a `Bearer` token.
#[derive(Debug, Clone)]
pub enum AccessToken {
/// A `DPoP` token.
Dpop(DpopAccessToken),
/// A `Bearer` token.
Bearer(BearerAccessToken),
}
impl AccessToken {
/// Exposes the token as a [`SecretString`].
#[must_use]
pub fn token(&self) -> &SecretString {
match self {
AccessToken::Dpop(token) => &token.token,
AccessToken::Bearer(token) => &token.token,
}
}
/// Exposes the token as a [`HeaderValue`], suitable for use in an `Authorization` header.
///
/// # Errors
///
/// Returns an [`InvalidHeaderValue`] if the token cannot be represented as a valid header value.
pub fn expose_header_value(&self) -> Result<HeaderValue, InvalidHeaderValue> {
match self {
AccessToken::Dpop(dpop_access_token) => dpop_access_token.expose_header_value(),
AccessToken::Bearer(bearer_access_token) => bearer_access_token.expose_header_value(),
}
}
/// Returns the `DPoP` JWT thumbprint, if the token is a `DPoP` token.
#[must_use]
pub fn dpop_jkt(&self) -> Option<&str> {
match self {
AccessToken::Dpop(token) => Some(token.jkt.as_str()),
AccessToken::Bearer(_) => None,
}
}
/// Returns the token type, either `"DPoP"` or `"Bearer"`.
#[must_use]
pub fn token_type(&self) -> &str {
match self {
AccessToken::Dpop(_) => "DPoP",
AccessToken::Bearer(_) => "Bearer",
}
}
/// Returns the effective expiry time of the token: `received_at + expires_in - margin`.
///
/// This is the point in time after which the token should be considered stale.
/// The token is valid while `SystemTime::now() < effective_expiry(...)`.
#[must_use]
pub fn effective_expiry(
&self,
default_expires_in: Duration,
expires_margin: Duration,
) -> SystemTime {
match self {
AccessToken::Dpop(dpop_access_token) => {
dpop_access_token.effective_expiry(default_expires_in, expires_margin)
}
AccessToken::Bearer(bearer_access_token) => {
bearer_access_token.effective_expiry(default_expires_in, expires_margin)
}
}
}
/// Returns `true` if the underlying access token has expired.
#[must_use]
pub fn is_expired(&self, default_expires_in: Duration, expires_margin: Duration) -> bool {
SystemTime::now() >= self.effective_expiry(default_expires_in, expires_margin)
}
}
/// An access token, with the `DPoP` token type.
#[derive(Debug, Clone)]
pub struct DpopAccessToken {
/// The `DPoP` access token.
token: SecretString,
/// The `DPoP` JWT thumbprint.
jkt: String,
/// The time at which the token was received.
received_at: SystemTime,
/// The duration for which the token is valid.
expires_in: Option<Duration>,
}
impl DpopAccessToken {
/// Creates a new [`DpopAccessToken`] with the given token, JWT thumbprint, received time, and expiration duration.
#[must_use]
pub fn new(
token: SecretString,
jkt: String,
received_at: SystemTime,
expires_in: Option<Duration>,
) -> Self {
Self {
token,
jkt,
received_at,
expires_in,
}
}
/// Returns a reference to the `DPoP` JWT thumbprint.
#[must_use]
pub fn jkt(&self) -> &str {
&self.jkt
}
/// Returns the token as a [`SecretString`].
#[must_use]
pub fn token(&self) -> &SecretString {
&self.token
}
/// Exposes the token as a [`HeaderValue`], suitable for use in an `Authorization` header.
///
/// # Errors
///
/// Returns an [`InvalidHeaderValue`] if the token cannot be represented as a valid header value.
pub fn expose_header_value(&self) -> Result<HeaderValue, InvalidHeaderValue> {
HeaderValue::from_str(&format!("DPoP {}", self.token.expose_secret()))
}
/// Returns the effective expiry time of the token: `received_at + expires_in - margin`.
///
/// This is the point in time after which the token should be considered stale.
/// The token is valid while `SystemTime::now() < effective_expiry(...)`.
#[must_use]
pub fn effective_expiry(
&self,
default_expires_in: Duration,
expires_margin: Duration,
) -> SystemTime {
let expires_in = self.expires_in.unwrap_or(default_expires_in);
self.received_at + expires_in - expires_margin
}
/// Returns `true` if the underlying access token has expired.
#[must_use]
pub fn is_expired(&self, default_expires_in: Duration, expires_margin: Duration) -> bool {
SystemTime::now() >= self.effective_expiry(default_expires_in, expires_margin)
}
}
/// A bearer access token, as used in the `Authorization: Bearer` header.
#[derive(Debug, Clone)]
pub struct BearerAccessToken {
/// The `Bearer` access token.
token: SecretString,
/// The time at which the token was received.
received_at: SystemTime,
/// The duration for which the token is valid.
expires_in: Option<Duration>,
}
impl BearerAccessToken {
/// Creates a new [`BearerAccessToken`] with the given token, received time, and expiration duration.
#[must_use]
pub fn new(token: SecretString, received_at: SystemTime, expires_in: Option<Duration>) -> Self {
Self {
token,
received_at,
expires_in,
}
}
/// Exposes the token as a [`str`].
#[must_use]
pub fn expose_token(&self) -> &str {
self.token.expose_secret()
}
/// Exposes the token as a [`HeaderValue`], suitable for use in an `Authorization` header.
///
/// # Errors
///
/// Returns an [`InvalidHeaderValue`] if the token cannot be represented as a valid header value.
pub fn expose_header_value(&self) -> Result<HeaderValue, InvalidHeaderValue> {
HeaderValue::from_str(&format!("Bearer {}", self.token.expose_secret()))
}
/// Returns the effective expiry time of the token: `received_at + expires_in - margin`.
///
/// This is the point in time after which the token should be considered stale.
/// The token is valid while `SystemTime::now() < effective_expiry(...)`.
#[must_use]
pub fn effective_expiry(
&self,
default_expires_in: Duration,
expires_margin: Duration,
) -> SystemTime {
let expires_in = self.expires_in.unwrap_or(default_expires_in);
self.received_at + expires_in - expires_margin
}
/// Returns `true` if the underlying access token has expired.
#[must_use]
pub fn is_expired(&self, default_expires_in: Duration, expires_margin: Duration) -> bool {
SystemTime::now() >= self.effective_expiry(default_expires_in, expires_margin)
}
}