summaryrefslogtreecommitdiff
path: root/src/routes/auth.rs
blob: 64ada2bb4c13ea397ced59a213f7972e92629d73 (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
57
58
59
60
61
62
63
64
65
66
use std::time::{SystemTime, UNIX_EPOCH};

use axum::extract::{Json, State};
use axum::response::IntoResponse;
use jsonwebtoken::{encode, Header};
use serde::{Deserialize, Serialize};

use crate::AppState;
use crate::auth::{check_password};
use super::errors::RouteError;

#[derive(Deserialize)]
pub struct LoginRequest {
    email: String,
    password: String,
}

#[derive(Serialize)]
struct LoginResponse {
    token: String,
}

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

pub async fn login(
    State(state): State<AppState>,
    Json(request): Json<LoginRequest>
) -> Result<impl IntoResponse, RouteError> {

    let user = match state.database.fetch_user_by_email(&request.email) {
        Err(_) => return Err(RouteError::Internal("database action failed".into())),
        Ok(None) => return Err(RouteError::UnregisteredEmail(request.email)),
        Ok(Some(user)) => user
    };

    match check_password(&request.password, user.password_hash()) {
        Err(_) => return Err(RouteError::Internal("failed to check password".into())),
        Ok(false) => return Err(RouteError::AuthorizationFailure()),
        Ok(true) => {},
    }

    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_err(|_| RouteError::Internal("failed to access system clock".into()))?
        .as_secs() as usize;

    let claims = JWTClaims {
        sub: user.email().to_owned(),
        iat: now,
        exp: now + 60 * 60 * 24,
        is_admin: false
    };
    
    let token= encode(&Header::default(), &claims, &state.api_key)
        .map_err(|e| RouteError::Internal(format!("failed to encode jwt: {e}")))?;
    
    Ok(Json(LoginResponse { token }))
}