rustbasic-passport 0.1.1

OAuth2 server and personal access tokens for Rust applications, a port of laravel/passport for the RustBasic ecosystem.
Documentation
use serde::Serialize;
use thiserror::Error as ThisError;

#[derive(Debug, ThisError)]
pub enum Error {
    #[error("invalid client credentials")]
    InvalidClient,
    #[error("client not found")]
    ClientNotFound,
    #[error("invalid grant: {0}")]
    InvalidGrant(String),
    #[error("invalid request: {0}")]
    InvalidRequest(String),
    #[error("invalid scope: {0}")]
    InvalidScope(String),
    #[error("unauthorized: {0}")]
    Unauthorized(String),
    #[error("token has been revoked")]
    TokenRevoked,
    #[error("token has expired")]
    TokenExpired,
    #[error("storage error: {0}")]
    Storage(String),
    #[error("personal access client has not been configured")]
    PersonalAccessClientNotFound,
    #[error("unsupported grant type: {0}")]
    UnknownGrant(String),
    #[error("unsupported response type: {0}")]
    UnsupportedResponseType(String),
    #[error("redirect uri does not match the client")]
    RedirectUriMismatch,
    #[error("pkce verification failed")]
    PkceVerificationFailed,
    #[error("unsupported code challenge method: {0}")]
    UnsupportedPkceMethod(String),
    #[error("device code not approved yet")]
    AuthorizationPending,
    #[error("polling too frequently, slow down")]
    SlowDown,
    #[error("device code has expired")]
    DeviceCodeExpired,
    #[error("device authorization was denied by the user")]
    AccessDenied,
    #[error("jwt error: {0}")]
    Jwt(#[from] jsonwebtoken::errors::Error),
    #[error("rsa error: {0}")]
    Rsa(#[from] rsa::errors::Error),
    #[error("pkcs8 error: {0}")]
    Pkcs8(#[from] rsa::pkcs8::Error),
    #[error("spki error: {0}")]
    Spki(#[from] rsa::pkcs8::spki::Error),
    #[error("base64 error: {0}")]
    Base64(#[from] base64::DecodeError),
    #[error("io error: {0}")]
    Io(#[from] std::io::Error),
}

#[derive(Debug, Clone, Serialize)]
pub struct OAuthError {
    pub error: &'static str,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error_description: Option<String>,
}

impl OAuthError {
    pub fn status(&self) -> u16 {
        match self.error {
            "invalid_client" | "invalid_token" => 401,
            _ => 400,
        }
    }
}

impl Error {
    pub fn to_oauth_error(&self) -> OAuthError {
        match self {
            Error::InvalidClient => OAuthError {
                error: "invalid_client",
                error_description: Some(self.to_string()),
            },
            Error::UnknownGrant(g) => OAuthError {
                error: "unsupported_grant_type",
                error_description: Some(format!("grant type `{g}` is not supported")),
            },
            Error::UnsupportedResponseType(rt) => OAuthError {
                error: "unsupported_response_type",
                error_description: Some(format!("response type `{rt}` is not supported")),
            },
            Error::InvalidGrant(_)
            | Error::RedirectUriMismatch
            | Error::PkceVerificationFailed
            | Error::UnsupportedPkceMethod(_) => OAuthError {
                error: "invalid_grant",
                error_description: Some(self.to_string()),
            },
            Error::InvalidScope(_) => OAuthError {
                error: "invalid_scope",
                error_description: Some(self.to_string()),
            },
            Error::InvalidRequest(_) => OAuthError {
                error: "invalid_request",
                error_description: Some(self.to_string()),
            },
            Error::Unauthorized(_) | Error::TokenRevoked | Error::TokenExpired => OAuthError {
                error: "invalid_token",
                error_description: Some(self.to_string()),
            },
            Error::AuthorizationPending => OAuthError {
                error: "authorization_pending",
                error_description: Some(self.to_string()),
            },
            Error::SlowDown => OAuthError {
                error: "slow_down",
                error_description: Some(self.to_string()),
            },
            Error::DeviceCodeExpired => OAuthError {
                error: "expired_token",
                error_description: Some(self.to_string()),
            },
            Error::AccessDenied => OAuthError {
                error: "access_denied",
                error_description: Some(self.to_string()),
            },
            _ => OAuthError {
                error: "server_error",
                error_description: Some(self.to_string()),
            },
        }
    }
}