1
0
mirror of https://github.com/actix/examples synced 2025-02-03 01:49:05 +01:00

31 lines
897 B
Rust
Raw Normal View History

2019-03-29 13:43:03 -07:00
use crate::errors::ServiceError;
use argonautica::{Hasher, Verifier};
lazy_static::lazy_static! {
pub static ref SECRET_KEY: String = std::env::var("SECRET_KEY").unwrap_or_else(|_| "0123".repeat(8));
}
// WARNING THIS IS ONLY FOR DEMO PLEASE DO MORE RESEARCH FOR PRODUCTION USE
pub fn hash_password(password: &str) -> Result<String, ServiceError> {
Hasher::default()
.with_password(password)
.with_secret_key(SECRET_KEY.as_str())
.hash()
.map_err(|err| {
dbg!(err);
ServiceError::InternalServerError
})
}
pub fn verify(hash: &str, password: &str) -> Result<bool, ServiceError> {
Verifier::default()
.with_hash(hash)
.with_password(password)
.with_secret_key(SECRET_KEY.as_str())
.verify()
.map_err(|err| {
dbg!(err);
ServiceError::Unauthorized
})
}