File size: 4,888 Bytes
dc422dd
 
 
 
 
 
4524048
dc422dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4524048
 
dc422dd
 
 
 
 
 
 
 
4524048
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc422dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use argon2::{
    Argon2,
    password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, SaltString, rand_core::OsRng},
};
use axum::{
    extract::{FromRequestParts, State},
    http::{header, request::Parts},
};
use chrono::{Duration, Utc};
use jsonwebtoken::{Algorithm, DecodingKey, EncodingKey, Header, Validation, decode, encode};
use serde::{Deserialize, Serialize};
use sqlx::PgPool;
use uuid::Uuid;

use crate::{
    errors::{AppError, AppResult},
    models::{AuthUser, UserRecord},
    state::AppState,
};

#[derive(Debug, Serialize, Deserialize)]
struct Claims {
    sub: String,
    exp: usize,
}

#[derive(Debug, Clone)]
pub struct CurrentUser(pub AuthUser);

pub fn normalize_email(value: &str) -> String {
    value.trim().to_ascii_lowercase()
}

pub fn normalize_username(value: &str) -> String {
    value.trim().to_ascii_lowercase()
}

pub fn hash_password(password: &str) -> AppResult<String> {
    let salt = SaltString::generate(&mut OsRng);
    let hashed_password = Argon2::default()
        .hash_password(password.as_bytes(), &salt)
        .map_err(|error| {
            tracing::error!("password hash error: {error}");
            AppError::Internal
        })?
        .to_string();

    Ok(hashed_password)
}

pub fn verify_password(password: &str, password_hash: &str) -> AppResult<()> {
    let parsed_hash = PasswordHash::new(password_hash).map_err(|error| {
        tracing::error!("password hash parse error: {error}");
        AppError::Internal
    })?;
    Argon2::default()
        .verify_password(password.as_bytes(), &parsed_hash)
        .map_err(|error| {
            tracing::error!("password verify error: {error}");
            AppError::Unauthorized("Invalid credentials".to_owned())
        })?;
    Ok(())
}

pub fn create_token(user_id: Uuid, secret: &str) -> AppResult<String> {
    let claims = Claims {
        sub: user_id.to_string(),
        exp: (Utc::now() + Duration::days(7)).timestamp() as usize,
    };

    Ok(encode(
        &Header::default(),
        &claims,
        &EncodingKey::from_secret(secret.as_bytes()),
    )?)
}

pub fn decode_token(token: &str, secret: &str) -> AppResult<Uuid> {
    let claims = decode::<Claims>(
        token,
        &DecodingKey::from_secret(secret.as_bytes()),
        &Validation::new(Algorithm::HS256),
    )
    .map_err(|_| AppError::Unauthorized("Authentication failed".to_owned()))?;

    Uuid::parse_str(&claims.claims.sub)
        .map_err(|_| AppError::Unauthorized("Authentication failed".to_owned()))
}

async fn load_user(pool: &PgPool, user_id: Uuid) -> AppResult<AuthUser> {
    let user = sqlx::query_as::<_, UserRecord>(
        r#"
        SELECT id, first_name, email, username, password_hash
        FROM users
        WHERE id = $1
        "#,
    )
    .bind(user_id)
    .fetch_optional(pool)
    .await?
    .ok_or_else(|| AppError::Unauthorized("Authentication required".to_owned()))?;

    Ok(user.into())
}

impl<S> FromRequestParts<S> for CurrentUser
where
    AppState: axum::extract::FromRef<S>,
    S: Send + Sync,
{
    type Rejection = AppError;

    async fn from_request_parts(parts: &mut Parts, state: &S) -> AppResult<Self> {
        let State(app_state) = State::<AppState>::from_request_parts(parts, state)
            .await
            .map_err(|_| AppError::Internal)?;
        let token = extract_bearer_token(parts)?
            .ok_or_else(|| AppError::Unauthorized("Authentication required".to_owned()))?;

        let user_id = decode_token(&token, &app_state.config.jwt_secret)?;
        let user = load_user(&app_state.pool, user_id).await?;

        Ok(Self(user))
    }
}

fn extract_bearer_token(parts: &Parts) -> AppResult<Option<String>> {
    let Some(value) = parts.headers.get(header::AUTHORIZATION) else {
        return Ok(None);
    };

    let value = value
        .to_str()
        .map_err(|_| AppError::Unauthorized("Invalid authorization header".to_owned()))?;

    let Some(token) = value.strip_prefix("Bearer ") else {
        return Err(AppError::Unauthorized(
            "Authorization header must use Bearer token format".to_owned(),
        ));
    };

    let token = token.trim();
    if token.is_empty() {
        return Err(AppError::Unauthorized(
            "Authorization token cannot be empty".to_owned(),
        ));
    }

    Ok(Some(token.to_owned()))
}

#[cfg(test)]
mod tests {
    use super::{create_token, decode_token, normalize_email, normalize_username};
    use uuid::Uuid;

    #[test]
    fn normalizes_identifier_input() {
        assert_eq!(normalize_email(" Test@Example.com "), "test@example.com");
        assert_eq!(normalize_username(" Alice "), "alice");
    }

    #[test]
    fn token_roundtrip() {
        let user_id = Uuid::new_v4();
        let token = create_token(user_id, "secret").unwrap();
        assert_eq!(decode_token(&token, "secret").unwrap(), user_id);
    }
}