tiny_oidc_rp/
error.rs

1// SPDX-License-Identifier: MIT
2
3/// tiny-oidc-rp Errors
4#[derive(Debug)]
5pub enum Error {
6    /// Invalid request to RP - Implementer shoud return 400
7    BadRequest,
8    /// Authentication failed - Implementer shoud return 401
9    AuthenticationFailed(AuthenticationFailedError),
10    /// Internal error - Implementer shoud return 500
11    InternalError,
12    /// ID Provider token endpoint returns error - Implementer shoud return 502
13    BadGateway(reqwest::Error),
14}
15
16impl From<AuthenticationFailedError> for Error {
17    fn from(err: AuthenticationFailedError) -> Self {
18        Self::AuthenticationFailed(err)
19    }
20}
21
22impl From<reqwest::Error> for Error {
23    fn from(err: reqwest::Error) -> Self {
24        Self::BadGateway(err)
25    }
26}
27
28impl From<Error> for http::StatusCode {
29    /// For convinience, convert crate::Error into HTTP status code
30    fn from(e: Error) -> Self {
31        use Error::*;
32        match e {
33            BadRequest => http::StatusCode::BAD_REQUEST,
34            AuthenticationFailed(_) => http::StatusCode::UNAUTHORIZED,
35            InternalError => http::StatusCode::INTERNAL_SERVER_ERROR,
36            BadGateway(_) => http::StatusCode::BAD_GATEWAY,
37        }
38    }
39}
40
41/// Detail of authentication failure
42#[derive(Debug)]
43pub enum AuthenticationFailedError {
44    /// Invalid claim in ID token. e.g., `iss`, `aud` mismatch, `exp` expires, etc.
45    ClaimValidationError,
46    JwsDecodeError,
47}
48
49impl From<base64::DecodeError> for AuthenticationFailedError {
50    /// Base64 decode error in ID token JWS parser
51    fn from(err: base64::DecodeError) -> Self {
52        log::warn!("Invalid ID token: {:?}", err);
53        Self::JwsDecodeError
54    }
55}
56
57impl From<serde_json::Error> for AuthenticationFailedError {
58    /// JSON decode error in ID token JWS parser
59    fn from(err: serde_json::Error) -> Self {
60        log::warn!("Invalid ID token: {:?}", err);
61        Self::JwsDecodeError
62    }
63}