mwoauth 0.1.0-rc.1

MediaWiki OAuth 2.0 client with optional Rocket, Axum, and Actix Web adapters
Documentation
// SPDX-License-Identifier: GPL-3.0-or-later

use crate::rocket_framework as rocket;
use crate::{Error, ErrorKind};
use rocket::http::Status;

/// HTML error responder used by the built-in Rocket OAuth routes.
#[derive(rocket::Responder)]
#[response(content_type = "html")]
pub(crate) struct OAuthError((Status, String));

impl From<Error> for OAuthError {
    fn from(error: Error) -> Self {
        let status = match error.kind() {
            ErrorKind::Callback => Status::BadRequest,
            ErrorKind::Network | ErrorKind::Provider => Status::BadGateway,
            ErrorKind::Unauthenticated => Status::Unauthorized,
            ErrorKind::Configuration | ErrorKind::Session => Status::InternalServerError,
        };
        let message = match error.kind() {
            ErrorKind::Callback => "OAuth callback validation failed",
            ErrorKind::Network | ErrorKind::Provider => "OAuth provider request failed",
            ErrorKind::Configuration | ErrorKind::Session => "Internal OAuth error",
            ErrorKind::Unauthenticated => "Authentication required",
        };
        Self((
            status,
            format!(
                r#"
                <!DOCTYPE html>
                <html lang="en">
                <head>
                    <meta charset="utf-8">
                    <meta name="color-scheme" content="light dark">
                    <title>Internal Server Error</title>
                </head>
                <body align="center">
                    <div role="main" align="center">
                        <h1>{}: {}</h1>
                        <p>{}</p>
                        <hr />
                    </div>
                    <div role="contentinfo" align="center">
                        <small>Rocket</small>
                    </div>
                </body>
                </html>
                "#,
                status.code,
                status.reason_lossy(),
                message
            ),
        ))
    }
}