summaryrefslogtreecommitdiffstats
path: root/src/auth/error.rs
blob: 91aec5cf03a19a4cabd373164cea867a3242574f (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("Failed to parse header: {0}")]
    HeaderValue(#[from] axum::http::header::InvalidHeaderValue),

    #[error("Required header not found: {0}")]
    HeaderNotFound(axum::http::HeaderName),

    #[error("Failed to parse header: {0} (wrong token type?)")]
    HeaderRejection(axum_extra::typed_header::TypedHeaderRejection),

    #[error("Failed to append header: {0}")]
    HeaderMaxSizeReached(#[from] axum::http::header::MaxSizeReached),

    #[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("The user belonging to this token no longer exists")]
    UserNotFound,
}

impl From<axum_extra::typed_header::TypedHeaderRejection> for Error {
    fn from(value: axum_extra::typed_header::TypedHeaderRejection) -> Self {
        if value.is_missing() {
            Self::HeaderNotFound(value.name().clone())
        } else {
            Self::HeaderRejection(value)
        }
    }
}

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::{header::AUTHORIZATION, StatusCode};

        let status = match self {
            Self::HeaderNotFound(ref h) if h == AUTHORIZATION => StatusCode::UNAUTHORIZED,
            Self::HeaderNotFound(_) => StatusCode::BAD_REQUEST,
            Self::HeaderRejection(_) | 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()
    }
}