1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//! 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 Extension as AxumExtension;
use post;
use Router;
use AppContext;
use Routes;
use Result as LocoResult;
use crateaxum as passport_axum;
use cratePassportServer;
use crateStores;
/// 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).
/// Sama seperti [`routes`], tetapi dalam bentuk
/// `axum::Router<AppContext>` — bisa langsung di-`merge` ke router bawaan
/// app loco (`Hooks::before_routes` / `after_routes`).
/// 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)])
/// }
/// ```