authr_http 0.1.0

Actix Web integration for authr.
Documentation
use actix_web::{
    middleware::from_fn,
    web::{self, ServiceConfig},
};
use authr_core::database::AuthDatabase;

use crate::{
    handlers,
    middleware::{csrf::csrf_protection, rate_limit::rate_limit, session::require_session},
};

pub fn config<D: AuthDatabase + 'static>(cfg: &mut ServiceConfig) {
    cfg.service(
        web::scope("")
            .wrap(from_fn(csrf_protection::<D, _>))
            .wrap(from_fn(rate_limit))
            .configure(public_routes::<D>)
            .configure(session_routes::<D>)
            .service(
                web::scope("")
                    .wrap(from_fn(require_session::<D, _>))
                    .configure(protected_routes::<D>),
            ),
    );
}

// -------------------- Public Routes --------------------

fn public_routes<D: AuthDatabase + 'static>(cfg: &mut ServiceConfig) {
    cfg
        // ----- Email and password
        .route("/sign-up", web::post().to(handlers::sign_up::<D>))
        .route(
            "/sign-in/email",
            web::post().to(handlers::sign_in_email::<D>),
        )
        // ----- OAuth
        .route(
            "/sign-in/social",
            web::post().to(handlers::sign_in_social::<D>),
        )
        .route(
            "/callback/{provider}",
            web::get().to(handlers::callback::<D>),
        )
        // ----- Password reset
        .route(
            "/request-password-reset",
            web::post().to(handlers::request_password_reset::<D>),
        )
        .route(
            "/reset-password/{token}",
            web::get().to(handlers::reset_password_callback::<D>),
        )
        .route(
            "/reset-password",
            web::post().to(handlers::reset_password::<D>),
        )
        // ----- Email verification
        .route(
            "/send-verification-email",
            web::post().to(handlers::send_verification_email::<D>),
        )
        .route("/verify-email", web::get().to(handlers::verify_email::<D>));
}

// -------------------- Session Routes --------------------

fn session_routes<D: AuthDatabase + 'static>(cfg: &mut ServiceConfig) {
    cfg.route("/sign-out", web::post().to(handlers::sign_out::<D>))
        .route("/get-session", web::get().to(handlers::get_session::<D>));
}

// -------------------- Protected Routes --------------------

fn protected_routes<D: AuthDatabase + 'static>(cfg: &mut ServiceConfig) {
    cfg
        // ----- User
        .route("/update-user", web::post().to(handlers::update_user::<D>))
        .route("/delete-user", web::post().to(handlers::delete_user::<D>))
        // ----- Password
        .route(
            "/verify-password",
            web::post().to(handlers::verify_password::<D>),
        )
        .route(
            "/change-password",
            web::post().to(handlers::change_password::<D>),
        )
        .route("/set-password", web::post().to(handlers::set_password::<D>))
        // ----- Accounts
        .route(
            "/list-accounts",
            web::get().to(handlers::list_accounts::<D>),
        )
        .route(
            "/get-access-token",
            web::post().to(handlers::get_access_token::<D>),
        )
        .route(
            "/refresh-token",
            web::post().to(handlers::refresh_token::<D>),
        )
        .route("/account-info", web::get().to(handlers::account_info::<D>))
        .route(
            "/link-account",
            web::post().to(handlers::link_social_account::<D>),
        )
        .route(
            "/unlink-account",
            web::post().to(handlers::unlink_account::<D>),
        )
        // ----- Sessions
        .route(
            "/list-sessions",
            web::get().to(handlers::list_sessions::<D>),
        )
        .route(
            "/revoke-session",
            web::post().to(handlers::revoke_session::<D>),
        )
        .route(
            "/revoke-sessions",
            web::post().to(handlers::revoke_sessions::<D>),
        )
        .route(
            "/revoke-other-sessions",
            web::post().to(handlers::revoke_other_sessions::<D>),
        );
}