2019-03-29 13:43:03 -07:00
|
|
|
use crate::errors::ServiceError;
|
2019-07-18 12:55:14 +01:00
|
|
|
use argonautica::{Hasher, Verifier};
|
2018-12-09 15:55:36 +00:00
|
|
|
|
2019-07-18 12:55:14 +01:00
|
|
|
lazy_static::lazy_static! {
|
|
|
|
pub static ref SECRET_KEY: String = std::env::var("SECRET_KEY").unwrap_or_else(|_| "0123".repeat(8));
|
2018-12-09 15:55:36 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 12:55:14 +01:00
|
|
|
// 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
|
|
|
|
})
|
2018-12-09 15:55:36 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 12:55:14 +01:00
|
|
|
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
|
|
|
|
})
|
2018-12-09 15:55:36 +00:00
|
|
|
}
|