totp: new crate, sql migration + queries, enable totp page, save secret in database
This commit is contained in:
parent
cb46556717
commit
233e26520c
26 changed files with 1116 additions and 364 deletions
34
crates/totp/src/lib.rs
Normal file
34
crates/totp/src/lib.rs
Normal 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)))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue