-
Notifications
You must be signed in to change notification settings - Fork 38
/
tokens.rs
431 lines (391 loc) · 16.2 KB
/
tokens.rs
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! Various implementations and traits for loading and handling tokens. These are used in the
//! [`Client`](super::Client) to provide tokens when configured
use std::{
path::{Path, PathBuf},
sync::Arc,
};
use base64::Engine;
use oauth2::reqwest::async_http_client;
use oauth2::{
basic::*, devicecode::DeviceAuthorizationResponse, AuthUrl, Client as Oauth2Client, ClientId,
RefreshToken, StandardRevocableToken, StandardTokenResponse, TokenResponse, TokenUrl,
};
use reqwest::{
header::{HeaderValue, AUTHORIZATION},
Client as HttpClient, RequestBuilder,
};
use time::{serde::timestamp, OffsetDateTime};
use tokio::fs::OpenOptions;
use tokio::io::AsyncWriteExt;
use tokio::sync::RwLock;
use super::{ClientError, Result};
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone)]
struct OidcTokenExtraFields {
pub id_token: String,
#[serde(default)]
pub issuer: String,
#[serde(default)]
pub client_id: String,
// TODO(thomastaylor312): Maybe the refresh endpoint should be through the api so we don't have
// to pass configuration back?
#[serde(default)]
pub token_url: String,
}
impl oauth2::ExtraTokenFields for OidcTokenExtraFields {}
#[derive(serde::Deserialize, Debug)]
struct Claims {
pub iss: String,
#[serde(with = "timestamp")]
pub exp: OffsetDateTime,
}
/// A trait that can be implemented by anything that can provide a valid token for use in a client.
/// Implementors of this trait should ensure that any token refresh/validation is done as part of
/// applying the authentication header
#[async_trait::async_trait]
pub trait TokenManager {
/// Adds the necessary header to the request, returning the newly updated request builder or an
/// error if there was a problem generating the token
async fn apply_auth_header(&self, builder: RequestBuilder) -> Result<RequestBuilder>;
}
/// A token manager that does nothing. For use when authentication is not enabled or anonymous auth
/// is desired
#[derive(Clone, Default)]
pub struct NoToken;
#[async_trait::async_trait]
impl TokenManager for NoToken {
async fn apply_auth_header(&self, builder: RequestBuilder) -> Result<RequestBuilder> {
Ok(builder)
}
}
/// A token manager for long lived tokens (such as service account tokens or personal access
/// tokens). This will simply configure the request to always return the provided token
#[derive(Clone)]
pub struct LongLivedToken {
token: String,
}
impl LongLivedToken {
/// Create a new LongLivedToken with the given token value
pub fn new(token: &str) -> Self {
LongLivedToken {
token: token.to_owned(),
}
}
}
#[async_trait::async_trait]
impl TokenManager for LongLivedToken {
async fn apply_auth_header(&self, builder: RequestBuilder) -> Result<RequestBuilder> {
let mut header_val = HeaderValue::from_str(&format!("Bearer {}", self.token))
.map_err(|e| ClientError::Other(e.to_string()))?;
header_val.set_sensitive(true);
Ok(builder.header(AUTHORIZATION, header_val))
}
}
#[derive(Clone)]
pub struct HttpBasic {
username: String,
password: String,
}
impl HttpBasic {
pub fn new(username: &str, password: &str) -> Self {
HttpBasic {
username: username.to_owned(),
password: password.to_owned(),
}
}
}
#[async_trait::async_trait]
impl TokenManager for HttpBasic {
async fn apply_auth_header(&self, builder: RequestBuilder) -> Result<RequestBuilder> {
let data = base64::engine::general_purpose::STANDARD
.encode(format!("{}:{}", self.username, self.password));
let mut header_val = HeaderValue::from_str(&format!("Basic {}", data))
.map_err(|e| ClientError::Other(e.to_string()))?;
header_val.set_sensitive(true);
Ok(builder.header(AUTHORIZATION, header_val))
}
}
type LockData<T> = Arc<RwLock<T>>;
/// A token manager for JWTs issued by an OIDC provider. This token manager expects a refresh token
/// and will attempt to refresh the token when it is close to expiry.
///
/// Note that any clone of this token manager will reuse the same underlying tokens to ensure that
/// only one refresh is done as needed
#[derive(Clone)]
pub struct OidcToken {
id_token: LockData<String>,
refresh_token: LockData<RefreshToken>,
expiry_time: LockData<OffsetDateTime>,
// We still need these available on the struct even though we don't use them here
#[allow(dead_code)]
issuer: String,
#[allow(dead_code)]
scopes: Vec<String>,
client_id: String,
token_url: String,
token_file: Option<PathBuf>,
}
impl OidcToken {
/// Create a new OidcToken from an ID token, refresh token, client ID, token url, and a set of
/// scopes. Only use this method if you do not have a token file available. Because there is no
/// token file, refreshed tokens will not be saved on disk
pub async fn new_from_parts(
id_token: &str,
refresh_token: &str,
client_id: &str,
token_url: &str,
scopes: Vec<String>,
) -> Result<Self> {
let (expiry_time, issuer) = data_from_token(id_token)?;
let me = OidcToken {
id_token: Arc::new(RwLock::new(id_token.to_owned())),
refresh_token: Arc::new(RwLock::new(RefreshToken::new(refresh_token.to_owned()))),
expiry_time: Arc::new(RwLock::new(expiry_time)),
issuer,
scopes,
client_id: client_id.to_owned(),
token_url: token_url.to_owned(),
token_file: None,
};
// Make sure we don't need a refresh
me.ensure_token().await?;
Ok(me)
}
/// Create a new OidcToken by loading a token file from the path. This token file is what is
/// generated from the `login` method
pub async fn new_from_file(token_file: impl AsRef<Path>) -> Result<Self> {
let path = token_file.as_ref().to_owned();
let raw = tokio::fs::read(&path).await?;
let token_res: StandardTokenResponse<OidcTokenExtraFields, BasicTokenType> =
toml::from_slice(&raw)?;
let mut me = Self::new_from_parts(
&token_res.extra_fields().id_token,
token_res
.refresh_token()
.ok_or_else(|| {
ClientError::TokenError(
"Token response does not contain a refresh token".into(),
)
})?
.secret(),
&token_res.extra_fields().client_id,
&token_res.extra_fields().token_url,
token_res
.scopes()
.map(|s| s.iter().map(|s| s.to_string()).collect())
.unwrap_or_default(),
)
.await?;
me.token_file = Some(path);
Ok(me)
}
/// Creates a new OidcToken by logging in with the given bindle server base URL (e.g.
/// https::https://my.bindle.com/v1) and then saving the resulting token at the given path
///
/// The token file is the OAuth2 response body from the authorization flow serialized to disk as
/// TOML
///
/// NOTE: This function requires user interaction and will print to stdout
pub async fn login(bindle_base_url: &str, token_file: impl AsRef<Path>) -> Result<Self> {
let (base_url, headers) = super::base_url_and_headers(bindle_base_url)?;
let login_resp = HttpClient::builder()
.build()?
.get(base_url.join(super::LOGIN_ENDPOINT).unwrap())
.query(&crate::LoginParams {
provider: "nothing".into(), // TODO: this will matter once we allow multiple kinds of auth
})
.headers(headers)
.send()
.await?;
let login_resp =
super::unwrap_status(login_resp, super::Endpoint::Login, super::Operation::Login)
.await?;
let device_code_details: DeviceAuthorizationResponse<
crate::DeviceAuthorizationExtraFields,
> = toml::from_slice(&login_resp.bytes().await?)?;
println!(
"Open this URL in your browser:\n{}\nand then enter the code when prompted: {}",
**device_code_details.verification_uri(),
device_code_details.user_code().secret()
);
let oauth_client: Oauth2Client<
BasicErrorResponse,
StandardTokenResponse<OidcTokenExtraFields, BasicTokenType>,
BasicTokenType,
BasicTokenIntrospectionResponse,
StandardRevocableToken,
BasicRevocationErrorResponse,
> = Oauth2Client::new(
ClientId::new(device_code_details.extra_fields().client_id.clone()),
None,
AuthUrl::new("https://not.needed.com".into()).unwrap(),
Some(TokenUrl::new(device_code_details.extra_fields().token_url.clone()).unwrap()),
)
.set_auth_type(oauth2::AuthType::RequestBody);
let token_res = match oauth_client
.exchange_device_access_token(&device_code_details)
.request_async(async_http_client, tokio::time::sleep, None)
.await
{
Ok(t) => t,
Err(e) => {
return Err(ClientError::Other(format!("{:?}", e)));
}
};
let (expiry_time, issuer) = data_from_token(&token_res.extra_fields().id_token)?;
let me = OidcToken {
id_token: Arc::new(RwLock::new(token_res.extra_fields().id_token.to_owned())),
refresh_token: Arc::new(RwLock::new(RefreshToken::new(
token_res
.refresh_token()
.ok_or_else(|| {
ClientError::TokenError(
"Token response does not contain a refresh token".into(),
)
})?
.secret()
.to_owned(),
))),
expiry_time: Arc::new(RwLock::new(expiry_time)),
issuer,
scopes: token_res
.scopes()
.map(|s| s.iter().map(|s| s.to_string()).collect())
.unwrap_or_default(),
client_id: device_code_details.extra_fields().client_id.clone(),
token_url: device_code_details.extra_fields().token_url.clone(),
token_file: Some(token_file.as_ref().to_owned()),
};
me.write_token_file(token_res).await?;
Ok(me)
}
/// Ensures that the current token is valid, refreshing if necessary and writing to the token
/// file (if one was used)
async fn ensure_token(&self) -> Result<()> {
// Magic number: wiggle room of 1 minute. If we are under 1 minute of expiring, then we
// should refresh. Also, we are locking inline here so the read lock is dropped as soon as
// possible
let is_expired = OffsetDateTime::now_utc() - time::Duration::minutes(1)
>= *self.expiry_time.read().await;
if is_expired {
tracing::debug!("Token has expired, attempting to refresh token");
let oauth_client: Oauth2Client<
BasicErrorResponse,
StandardTokenResponse<OidcTokenExtraFields, BasicTokenType>,
BasicTokenType,
BasicTokenIntrospectionResponse,
StandardRevocableToken,
BasicRevocationErrorResponse,
> =
Oauth2Client::new(
ClientId::new(self.client_id.clone()),
None,
AuthUrl::new("https://not.needed.com".into()).unwrap(),
Some(TokenUrl::new(self.token_url.clone()).map_err(|e| {
ClientError::TokenError(format!("Invalid token url: {}", e))
})?),
)
.set_auth_type(oauth2::AuthType::RequestBody);
// Block for holding the write locks as short as possible
let token_res = {
// We are taking a write lock here because we are going to overwrite it
let mut refresh_token = self.refresh_token.write().await;
let token_res = match oauth_client
.exchange_refresh_token(&refresh_token)
.request_async(async_http_client)
.await
{
Ok(t) => t,
Err(e) => {
return Err(ClientError::TokenError(format!(
"Unable to refresh token {:?}",
e
)));
}
};
let (expiry, _) = data_from_token(&token_res.extra_fields().id_token)?;
let mut expiry_time = self.expiry_time.write().await;
let mut id_token = self.id_token.write().await;
*expiry_time = expiry;
*id_token = token_res.extra_fields().id_token.clone();
*refresh_token = RefreshToken::new(
token_res
.refresh_token()
.ok_or_else(|| {
ClientError::TokenError(
"Token response does not contain a refresh token".into(),
)
})?
.secret()
.to_owned(),
);
token_res
};
if let Some(p) = self.token_file.as_ref() {
tracing::trace!(path = %p.display(), "Token refreshed and token file is set. Updating with token data");
self.write_token_file(token_res).await?;
}
}
Ok(())
}
async fn write_token_file(
&self,
mut token_res: StandardTokenResponse<OidcTokenExtraFields, BasicTokenType>,
) -> Result<()> {
let token_file = match self.token_file.as_ref() {
Some(p) => p,
// If the file is not set, just return
None => return Ok(()),
};
// Parse the issuer from the id token so it can be stored for refresh. We aren't worrying
// about validating the token here as we just need the issuer
let mut extra = token_res.extra_fields().to_owned();
let (_, issuer) = data_from_token(&token_res.extra_fields().id_token)?;
extra.issuer = issuer.clone();
extra.client_id = self.client_id.clone();
extra.token_url = self.token_url.clone();
token_res.set_extra_fields(extra);
tracing::info!(path = %token_file.display(), "Writing access token to file");
#[cfg(not(target_family = "windows"))]
let mut file = OpenOptions::new()
.create(true)
.write(true)
.mode(0o600)
.truncate(true)
.open(token_file)
.await?;
// TODO: Figure out the proper permission for a token on disk for windows
#[cfg(target_family = "windows")]
let mut file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(token_file)
.await?;
file.write_all(&toml::to_vec(&token_res)?).await?;
// Make sure everything is flushed out to disk
file.flush().await?;
Ok(())
}
}
#[async_trait::async_trait]
impl TokenManager for OidcToken {
async fn apply_auth_header(&self, builder: RequestBuilder) -> Result<RequestBuilder> {
self.ensure_token().await?;
let mut header_val =
HeaderValue::from_str(&format!("Bearer {}", (*self.id_token.read().await).clone()))
.map_err(|e| ClientError::Other(e.to_string()))?;
header_val.set_sensitive(true);
Ok(builder.header(AUTHORIZATION, header_val))
}
}
fn data_from_token(token: &str) -> Result<(OffsetDateTime, String)> {
// This basically turns all validation off as all we are trying to do is parse the data from the
// token. Validation happens on the server
let mut validation = jsonwebtoken::Validation::default();
validation.validate_exp = false;
validation.insecure_disable_signature_validation();
let fake_key = jsonwebtoken::DecodingKey::from_secret(b"fake");
let parsed_token = jsonwebtoken::decode::<Claims>(token, &fake_key, &validation)
.map_err(|e| ClientError::TokenError(format!("Invalid token data: {}", e)))?;
Ok((parsed_token.claims.exp, parsed_token.claims.iss))
}