2020-12-02 12:29:03 +09:00
|
|
|
use argon2::{self, Config};
|
2018-12-09 15:55:36 +00:00
|
|
|
|
2022-07-09 21:08:11 +01:00
|
|
|
use crate::errors::ServiceError;
|
|
|
|
|
2019-07-18 12:55:14 +01:00
|
|
|
lazy_static::lazy_static! {
|
2020-12-02 12:29:03 +09:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-10-07 03:04:59 +01:00
|
|
|
const SALT: &[u8] = b"supersecuresalt";
|
2020-12-02 12:29:03 +09: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> {
|
2020-12-02 12:29:03 +09:00
|
|
|
let config = Config {
|
|
|
|
secret: SECRET_KEY.as_bytes(),
|
|
|
|
..Default::default()
|
|
|
|
};
|
2021-10-07 03:04:59 +01:00
|
|
|
argon2::hash_encoded(password.as_bytes(), SALT, &config).map_err(|err| {
|
2020-12-02 12:29:03 +09:00
|
|
|
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> {
|
2022-02-18 02:44:02 +00:00
|
|
|
argon2::verify_encoded_ext(hash, password.as_bytes(), SECRET_KEY.as_bytes(), &[]).map_err(
|
|
|
|
|err| {
|
2019-07-18 12:55:14 +01:00
|
|
|
dbg!(err);
|
|
|
|
ServiceError::Unauthorized
|
2022-02-18 02:44:02 +00:00
|
|
|
},
|
|
|
|
)
|
2018-12-09 15:55:36 +00:00
|
|
|
}
|