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
|
use sqlx::{sqlite::Sqlite, SqliteConnection, Transaction};
use uuid::Uuid;
use super::logins::Id as LoginId;
use crate::error::BoxedError;
type DateTime = chrono::DateTime<chrono::Utc>;
pub trait Provider {
fn tokens(&mut self) -> Tokens;
}
impl<'c> Provider for Transaction<'c, Sqlite> {
fn tokens(&mut self) -> Tokens {
Tokens(self)
}
}
pub struct Tokens<'t>(&'t mut SqliteConnection);
impl<'c> Tokens<'c> {
/// Issue a new token for an existing login. The issued_at timestamp will
/// be used to control expiry.
pub async fn issue(
&mut self,
login: &LoginId,
issued_at: DateTime,
) -> Result<String, BoxedError> {
let secret = Uuid::new_v4().to_string();
let secret = sqlx::query_scalar!(
r#"
insert
into token (secret, login, issued_at)
values ($1, $2, $3)
returning secret as "secret!"
"#,
secret,
login,
issued_at,
)
.fetch_one(&mut *self.0)
.await?;
Ok(secret)
}
}
|