mirror of
https://github.com/actix/examples
synced 2025-02-26 02:52:49 +01:00
31 lines
897 B
Rust
31 lines
897 B
Rust
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
|
|
})
|
|
}
|