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
|
use sqlx::{sqlite::Sqlite, SqliteConnection, Transaction};
use crate::{
clock::DateTime,
event::{Instant, Sequence},
login::{self, password::StoredHash, History, Login},
};
pub trait Provider {
fn auth(&mut self) -> Auth;
}
impl<'c> Provider for Transaction<'c, Sqlite> {
fn auth(&mut self) -> Auth {
Auth(self)
}
}
pub struct Auth<'t>(&'t mut SqliteConnection);
impl<'t> Auth<'t> {
// Retrieves a login by name, plus its stored password hash for
// verification. If there's no login with the requested name, this will
// return [None].
pub async fn for_name(
&mut self,
name: &str,
) -> Result<Option<(History, StoredHash)>, sqlx::Error> {
let found = sqlx::query!(
r#"
select
id as "id: login::Id",
name,
password_hash as "password_hash: StoredHash",
created_sequence as "created_sequence: Sequence",
created_at as "created_at: DateTime"
from login
where name = $1
"#,
name,
)
.map(|row| {
(
History {
login: Login {
id: row.id,
name: row.name,
},
created: Instant {
at: row.created_at,
sequence: row.created_sequence,
},
},
row.password_hash,
)
})
.fetch_optional(&mut *self.0)
.await?;
Ok(found)
}
}
|