Skip to main content

rustauth_oauth/oauth2/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum OAuthError {
5    #[error("missing OAuth provider option `{0}`")]
6    MissingOption(&'static str),
7    #[error("invalid OAuth URL: {0}")]
8    InvalidUrl(#[from] url::ParseError),
9    #[error("OAuth HTTP request failed: {0}")]
10    Http(#[from] reqwest::Error),
11    #[error("refusing to connect: request URL resolves to a private or internal IP address")]
12    BlockedRequestUrl,
13    #[error("OAuth HTTP request failed with status {status}: {body}")]
14    HttpStatus { status: u16, body: String },
15    #[error("OAuth error response `{error}`{description}")]
16    ErrorResponse {
17        error: String,
18        description: String,
19        uri: Option<String>,
20    },
21    #[error("missing OAuth token field `{0}`")]
22    MissingTokenField(&'static str),
23    #[error("invalid OAuth claim `{claim}`: {reason}")]
24    InvalidClaim { claim: &'static str, reason: String },
25    #[error("unsupported OAuth JWT algorithm `{0}`")]
26    UnsupportedAlgorithm(String),
27    #[error("invalid OAuth configuration: {0}")]
28    InvalidConfiguration(String),
29    #[error("invalid PKCE code_verifier: {0}")]
30    InvalidCodeVerifier(String),
31    #[error("invalid OAuth response: {0}")]
32    InvalidResponse(String),
33    #[error("invalid OAuth token response: {0}")]
34    InvalidTokenResponse(String),
35    #[error("invalid OAuth client authentication: {0}")]
36    InvalidClientAuthentication(String),
37    #[error("invalid OAuth JWKS cache configuration: {0}")]
38    JwksCache(String),
39    #[error("token verification failed: {0}")]
40    TokenVerification(String),
41    #[error("JOSE operation failed: {0}")]
42    Jose(String),
43}
44
45#[cfg(feature = "jose")]
46impl From<josekit::JoseError> for OAuthError {
47    fn from(error: josekit::JoseError) -> Self {
48        Self::Jose(error.to_string())
49    }
50}
51
52pub(crate) fn oauth_error_description(description: Option<String>) -> String {
53    description
54        .filter(|value| !value.is_empty())
55        .map(|value| format!(": {value}"))
56        .unwrap_or_default()
57}