rustbasic-passport 0.1.1

OAuth2 server and personal access tokens for Rust applications, a port of laravel/passport for the RustBasic ecosystem.
Documentation
//! Integrasi [loco.rs](https://loco.rs) (feature `loco`).
//!
//! loco.rs membangun seluruh app di atas `axum::Router<AppContext>` dan
//! menerapkan semua route & middleware sebelum `.with_state(ctx)`. Karena itu
//! handler di sini mengambil [`PassportServer`] via _request extension_
//! (lihat `axum`), bukan router state — sehingga kompatibel penuh dengan
//! loco.
//!
//! ## Cara pakai (di app loco)
//!
//! ```text
//! // src/app.rs — Hooks untuk App
//! async fn initializers(_ctx: &AppContext) -> Result<Vec<Box<dyn Initializer>>> {
//!     Ok(vec![
//!         // server dirimu, sudah berisi StoreBundle yang diimplementasikan
//!         // di atas model sea-orm app
//!         Box::new(server),
//!     ])
//! }
//!
//! fn routes(_ctx: &AppContext) -> AppRoutes {
//!     AppRoutes::with_default_routes()
//!         .add_route(rustbasic_passport::loco::routes::<MyStores>())
//! }
//! ```
//!
//! `PassportServer` mengimplementasi `loco_rs::app::Initializer`, jadi saat
//! didaftarkan di `Hooks::initializers` ia otomatis terpasang sebagai
//! `Extension` pada seluruh route — handler unauth & middleware berikut bisa
//! langsung mengekstraknya: `Extension<PassportServer<S>>`.
//!
//! Proteksi route API:
//!
//! ```text
//! // controller / route layer (sama seperti axum biasa)
//! .route_layer(middleware::from_fn_with_state(
//!     rustbasic_passport::axum::ScopePolicy::any(["read"]),
//!     rustbasic_passport::axum::check_scopes,
//! ))
//! .route_layer(middleware::from_fn_with_state(
//!     server.clone(),
//!     rustbasic_passport::axum::require_auth,
//! ))
//! ```

use axum::extract::Extension as AxumExtension;
use axum::routing::post;
use axum::Router;
use loco_rs::app::AppContext;
use loco_rs::controller::Routes;
use loco_rs::Result as LocoResult;

use crate::axum as passport_axum;
use crate::server::PassportServer;
use crate::store::Stores;

/// Route OAuth `/oauth/*` (token, authorize, revoke, device) siap ditambahkan
/// ke `AppRoutes` lewat `AppRoutes::add_route(..)`.
///
/// Handler mengambil [`PassportServer`] dari `Extension` — pastikan server
/// terpasang (daftarkan via `Hooks::initializers`, lihat modul ini).
pub fn routes<S: Stores>() -> Routes {
    Routes::new()
        .add("/oauth/token", post(passport_axum::token_handler::<S>))
        .add("/oauth/authorize", post(passport_axum::authorize_handler::<S>))
        .add("/oauth/revoke", post(passport_axum::revoke_handler::<S>))
        .add("/oauth/device", post(passport_axum::device_authorization_handler::<S>))
        .add("/oauth/device/verify", post(passport_axum::device_approve_handler::<S>))
}

/// Sama seperti [`routes`], tetapi dalam bentuk
/// `axum::Router<AppContext>` — bisa langsung di-`merge` ke router bawaan
/// app loco (`Hooks::before_routes` / `after_routes`).
pub fn router<S: Stores>() -> Router<AppContext> {
    Router::new()
        .route("/oauth/token", post(passport_axum::token_handler::<S>))
        .route("/oauth/authorize", post(passport_axum::authorize_handler::<S>))
        .route("/oauth/revoke", post(passport_axum::revoke_handler::<S>))
        .route("/oauth/device", post(passport_axum::device_authorization_handler::<S>))
        .route("/oauth/device/verify", post(passport_axum::device_approve_handler::<S>))
}

/// Pasang [`PassportServer`] sebagai `Extension` di seluruh route app — cukup
/// daftarkan server itu sendiri di `Hooks::initializers`.
///
/// ```text
/// async fn initializers(_ctx: &AppContext) -> Result<Vec<Box<dyn Initializer>>> {
///     Ok(vec![Box::new(server)])
/// }
/// ```
#[async_trait::async_trait]
impl<S: Stores> loco_rs::app::Initializer for PassportServer<S> {
    fn name(&self) -> String {
        "rustbasic-passport".to_string()
    }

    async fn after_routes(
        &self,
        router: Router,
        _ctx: &AppContext,
    ) -> LocoResult<Router> {
        Ok(router.layer(AxumExtension(self.clone())))
    }
}