summaryrefslogtreecommitdiffstats
path: root/src/auth/error.rs
blob: 8b1bb4c83d5e270bf70bdf43889ffada54884e57 (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
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
#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("Failed to parse header: {0}")]
    Header(#[from] axum::http::header::InvalidHeaderValue),

    #[error("Database error: {0}")]
    Sqlx(#[from] sqlx::Error),

    #[error("Time error: {0}")]
    Time(#[from] time::error::ComponentRange),

    #[error("Invalid authorization token: {0}")]
    JwtValidation(#[source] jsonwebtoken::errors::Error),

    #[error("Invalid authorization token format: {0}")]
    JwtFormat(#[source] jsonwebtoken::errors::Error),

    #[error("JWT server error: {0}")]
    JwtInternal(#[source] jsonwebtoken::errors::Error),

    #[error("Failed to hash password: {0}")]
    PasswordHash(#[source] argon2::password_hash::Error),

    #[error("Invalid UUID: {0}")]
    Uuid(String),

    #[error("Failed to register user")]
    Registration,

    #[error("Invalid email or password")]
    LoginInvalid,

    #[error("Authorization token not found")]
    JwtNotFound,

    #[error("Token found was invalid type")]
    InvalidTokenType,

    #[error("The user belonging to this token no longer exists")]
    UserNotFound,
}

impl From<argon2::password_hash::Error> for Error {
    fn from(value: argon2::password_hash::Error) -> Self {
        match value {
            argon2::password_hash::Error::Password => Self::LoginInvalid,
            _ => Self::PasswordHash(value),
        }
    }
}

impl From<jsonwebtoken::errors::Error> for Error {
    fn from(value: jsonwebtoken::errors::Error) -> Self {
        use jsonwebtoken::errors::ErrorKind;
        match value.kind() {
            ErrorKind::InvalidSignature
            | ErrorKind::MissingRequiredClaim(_)
            | ErrorKind::ExpiredSignature
            | ErrorKind::InvalidIssuer
            | ErrorKind::InvalidAudience
            | ErrorKind::InvalidSubject
            | ErrorKind::ImmatureSignature
            | ErrorKind::InvalidAlgorithm
            | ErrorKind::MissingAlgorithm => Error::JwtValidation(value),
            ErrorKind::InvalidToken => Error::JwtFormat(value),
            _ => Error::JwtInternal(value),
        }
    }
}

impl From<uuid::Error> for Error {
    fn from(value: uuid::Error) -> Self {
        Error::Uuid(value.to_string())
    }
}

impl axum::response::IntoResponse for Error {
    fn into_response(self) -> axum::response::Response {
        use axum::http::StatusCode;

        let status = match self {
            Error::JwtFormat(_) | Error::Uuid(_) => StatusCode::UNPROCESSABLE_ENTITY,
            Error::JwtValidation(_)
            | Error::LoginInvalid
            | Error::UserNotFound
            | Error::JwtNotFound => StatusCode::UNAUTHORIZED,
            _ => StatusCode::INTERNAL_SERVER_ERROR,
        };

        (status, self.to_string()).into_response()
    }
}