apps: sql + get valid one, get by id, insert, generate app id, generate secret
This commit is contained in:
parent
b5c2be6c9f
commit
71b083895d
19 changed files with 490 additions and 0 deletions
|
|
@ -7,3 +7,5 @@ edition = "2021"
|
|||
argon2 = { version = "0.4.1" }
|
||||
rand_core = { version = "0.6.4", features = ["std"] }
|
||||
thiserror = { workspace = true }
|
||||
nanoid = "0.4.0"
|
||||
nanoid-dictionary = "0.4.3"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
mod error;
|
||||
mod hash;
|
||||
mod password;
|
||||
mod secret;
|
||||
|
||||
pub use error::Error;
|
||||
pub use password::Password;
|
||||
pub use secret::{Secret, SecretString};
|
||||
|
|
|
|||
31
crates/hash/src/secret.rs
Normal file
31
crates/hash/src/secret.rs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
use crate::error::Error;
|
||||
use crate::hash::{hash, Hash};
|
||||
use nanoid::nanoid;
|
||||
use nanoid_dictionary::ALPHANUMERIC;
|
||||
|
||||
// Struct to generate the secret
|
||||
pub struct SecretString(String);
|
||||
const LENGTH: usize = 64;
|
||||
impl Default for SecretString {
|
||||
fn default() -> Self {
|
||||
Self(nanoid!(LENGTH, ALPHANUMERIC))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Secret(Hash);
|
||||
|
||||
impl Secret {
|
||||
pub fn new(plain: SecretString) -> Result<Self, Error> {
|
||||
Ok(Self(Hash::from_hash(hash(&plain.0)?)))
|
||||
}
|
||||
pub fn from_hash(hash: impl Into<String>) -> Self {
|
||||
Self(Hash::from_hash(hash))
|
||||
}
|
||||
pub fn hash(&self) -> &str {
|
||||
self.0.hash()
|
||||
}
|
||||
pub fn compare(&self, plain: &str) -> Result<bool, Error> {
|
||||
self.0.compare(plain).map_err(Error::from)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue