apps: sql + get valid one, get by id, insert, generate app id, generate secret

This commit is contained in:
Philippe Loctaux 2023-03-15 22:00:04 +01:00
parent b5c2be6c9f
commit 71b083895d
19 changed files with 490 additions and 0 deletions

View file

@ -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
View 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)
}
}