totp: new crate, sql migration + queries, enable totp page, save secret in database

This commit is contained in:
Philippe Loctaux 2023-04-25 23:37:24 +02:00
parent cb46556717
commit 233e26520c
26 changed files with 1116 additions and 364 deletions

34
crates/totp/src/lib.rs Normal file
View file

@ -0,0 +1,34 @@
use totp_rs::{Algorithm, TotpUrlError, TOTP};
pub use totp_rs::Secret;
// error
#[derive(thiserror::Error)]
// the rest
#[derive(Debug)]
pub enum Error {
#[error("Totp url error: {0}")]
Url(#[from] TotpUrlError),
#[error("Totp secret error: {0}")]
Secret(String),
}
pub fn new(secret: Vec<u8>, issuer: String, account_name: String) -> Result<TOTP, Error> {
TOTP::new(
Algorithm::SHA1,
6,
1,
30,
secret,
Some(issuer),
account_name,
)
.map_err(Error::Url)
}
pub fn secret_to_bytes(secret: &Secret) -> Result<Vec<u8>, Error> {
secret
.to_bytes()
.map_err(|e| Error::Secret(format!("{:?}", e)))
}