summaryrefslogtreecommitdiff
path: root/src/auth.rs
blob: a621756051104cedf792106b1772ead674b3f603 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use axum::{extract::FromRequestParts, http::{StatusCode, request::Parts}};
use serde::Serialize;
use argon2::{Argon2, PasswordHash, PasswordVerifier, password_hash::{
    Error, PasswordHasher, SaltString, rand_core::OsRng
}};

pub fn hash_password(password: &str) -> Result<String, Error> {
    let argon2 = Argon2::default();
    let salt = SaltString::generate(&mut OsRng);
    Ok(argon2.hash_password(password.as_bytes(), &salt)?.to_string())
}

pub fn check_password(password: &str, password_hash: &str) -> Result<bool, Error> {
    let argon2 = Argon2::default();
    let hash = PasswordHash::new(password_hash)?;
    Ok(argon2.verify_password(password.as_bytes(), &hash).is_ok())
}

#[derive(Serialize)]
pub struct Claims {
    pub sub: String,
    pub exp: usize,
    pub iat: usize,
    pub is_admin: bool,
}

//pub fn create_jwt(email: &str, is_admin: bool) -> Result

pub struct AuthUser(pub Claims);

impl<S: Send + Sync> FromRequestParts<S> for AuthUser {
    type Rejection = StatusCode;

    async fn from_request_parts(
        parts: &mut Parts,
        state: &S,
    ) -> Result<Self, Self::Rejection> {
        todo!();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_password_hashing() {

        let passwords = vec!["password", "test1", "random"];

        for password in &passwords {
            let hash = hash_password(password).unwrap();
            assert!(check_password(password, &hash).unwrap());
        }
    }
}