Skip to main content

openauth_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("OAuth HTTP request failed with status {status}: {body}")]
12    HttpStatus { status: u16, body: String },
13    #[error("OAuth error response `{error}`{description}")]
14    ErrorResponse {
15        error: String,
16        description: String,
17        uri: Option<String>,
18    },
19    #[error("missing OAuth token field `{0}`")]
20    MissingTokenField(&'static str),
21    #[error("invalid OAuth claim `{claim}`: {reason}")]
22    InvalidClaim { claim: &'static str, reason: String },
23    #[error("unsupported OAuth JWT algorithm `{0}`")]
24    UnsupportedAlgorithm(String),
25    #[error("invalid OAuth configuration: {0}")]
26    InvalidConfiguration(String),
27    #[error("invalid OAuth response: {0}")]
28    InvalidResponse(String),
29    #[error("invalid OAuth token response: {0}")]
30    InvalidTokenResponse(String),
31    #[error("invalid OAuth client authentication: {0}")]
32    InvalidClientAuthentication(String),
33    #[error("invalid OAuth JWKS cache configuration: {0}")]
34    JwksCache(String),
35    #[error("token verification failed: {0}")]
36    TokenVerification(String),
37    #[error("JOSE operation failed: {0}")]
38    Jose(String),
39}
40
41#[cfg(feature = "jose")]
42impl From<josekit::JoseError> for OAuthError {
43    fn from(error: josekit::JoseError) -> Self {
44        Self::Jose(error.to_string())
45    }
46}
47
48pub(crate) fn oauth_error_description(description: Option<String>) -> String {
49    description
50        .filter(|value| !value.is_empty())
51        .map(|value| format!(": {value}"))
52        .unwrap_or_default()
53}