1
use std::sync::Arc;
2

            
3
use axum::{
4
    Router, middleware,
5
    routing::{get, post},
6
};
7

            
8
use crate::pages::script;
9
use crate::pages::template;
10
use crate::{
11
    AppState,
12
    error_render::full_page,
13
    handler::{get_home_link, get_logout_link, get_version},
14
    jwt_auth::{admin, auth},
15
    login::{login_form_handler, login_user_handler, reshow_login},
16
    pages::{account, commodity, report, tag},
17
    rate_limit::{AuthLimiter, throttle_auth},
18
    register::register_user_handler,
19
    session::{change_password_handler, logout_handler, refresh_access_token_handler},
20
};
21

            
22
/// Pages only an admin may see.
23
///
24
/// Registration is invite-only: `POST /api/auth/register` has always been
25
/// admin-gated, but the page carrying its form was public, so an anonymous
26
/// visitor was handed a signup form whose every submission is refused. The
27
/// page now sits behind the same gate as the endpoint it posts to.
28
///
29
/// Layer order matters: `auth` must run before `admin`, which reads the
30
/// extension `auth` inserts.
31
4
pub fn create_admin_pages_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
32
4
    Router::new()
33
4
        .route("/register", get(crate::pages::register))
34
4
        .route_layer(middleware::from_fn_with_state(app_state.clone(), admin))
35
4
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
36
4
        .route_layer(middleware::from_fn(full_page))
37
4
        .with_state(app_state.clone())
38
4
}
39

            
40
1
pub fn create_pages_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
41
1
    Router::new()
42
1
        .route(
43
1
            "/commodity/create",
44
1
            get(crate::pages::commodity::create::commodity_create_page),
45
1
        )
46
1
        .route(
47
1
            "/commodity/convert",
48
1
            get(crate::pages::commodity::convert::commodity_convert_page),
49
1
        )
50
1
        .route(
51
1
            "/commodity/list",
52
1
            get(crate::pages::commodity::list::commodity_list_page),
53
1
        )
54
1
        .route(
55
1
            "/commodity/{id}/edit",
56
1
            get(crate::pages::commodity::edit::commodity_edit_page),
57
1
        )
58
1
        .route("/config", get(crate::pages::config::config_page))
59
1
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
60
1
        .route_layer(middleware::from_fn(full_page))
61
1
        .with_state(app_state.clone())
62
1
}
63

            
64
1
pub fn create_accounts_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
65
1
    Router::new()
66
1
        .route(
67
1
            "/account/create",
68
1
            get(crate::pages::account::create::account_create_page),
69
1
        )
70
1
        .route(
71
1
            "/account/list",
72
1
            get(crate::pages::account::list::account_list_page),
73
1
        )
74
1
        .route(
75
1
            "/account/manage",
76
1
            get(crate::pages::account::manage::account_manage_page),
77
1
        )
78
1
        .route(
79
1
            "/account/ssh-key",
80
1
            get(crate::pages::account::ssh_key::ssh_key_page),
81
1
        )
82
1
        .route(
83
1
            crate::pages::account::merge::PAGE_PATH,
84
1
            get(crate::pages::account::merge::account_merge_page),
85
1
        )
86
1
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
87
1
        .route_layer(middleware::from_fn(full_page))
88
1
        .with_state(app_state.clone())
89
1
}
90

            
91
2
pub fn create_transactions_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
92
2
    Router::new()
93
2
        .route(
94
2
            "/transaction/new",
95
2
            get(crate::pages::transaction::new::transaction_new_page),
96
2
        )
97
2
        .route(
98
2
            crate::pages::transaction::browse::LIST_PATH,
99
2
            get(crate::pages::transaction::browse::transaction_list_page),
100
2
        )
101
2
        .route(
102
2
            "/transaction/{id}/edit",
103
2
            get(crate::pages::transaction::update::transaction_update_page),
104
2
        )
105
2
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
106
2
        .route_layer(middleware::from_fn(full_page))
107
2
        .with_state(app_state.clone())
108
2
}
109

            
110
pub fn create_tags_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
111
    Router::new()
112
        .route(
113
            "/tag/create",
114
            get(crate::pages::tag::create::tag_create_page),
115
        )
116
        .route("/tag/list", get(crate::pages::tag::list::tag_list_page))
117
        .route(
118
            "/tag/edit/{id}",
119
            get(crate::pages::tag::edit::tag_edit_page),
120
        )
121
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
122
        .route_layer(middleware::from_fn(full_page))
123
        .with_state(app_state.clone())
124
}
125

            
126
pub fn create_scripts_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
127
    Router::new()
128
        .route("/script/create", get(script::create::script_create_page))
129
        .route("/script/list", get(script::list::script_list_page))
130
        .route("/script/edit/{id}", get(script::edit::script_edit_page))
131
        .route(
132
            "/template/create",
133
            get(template::create::template_create_page),
134
        )
135
        .route("/template/list", get(template::list::template_list_page))
136
        .route(
137
            "/template/edit/{id}",
138
            get(template::edit::template_edit_page),
139
        )
140
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
141
        .route_layer(middleware::from_fn(full_page))
142
        .with_state(app_state.clone())
143
}
144

            
145
1
pub fn create_reports_router(app_state: Arc<AppState>) -> Router<Arc<AppState>> {
146
1
    Router::new()
147
1
        .route("/report/balance", get(report::balance::balance_report_page))
148
1
        .route(
149
1
            "/report/activity",
150
1
            get(report::activity::activity_report_page),
151
1
        )
152
1
        .route(
153
1
            "/report/category-breakdown",
154
1
            get(report::category_breakdown::category_breakdown_report_page),
155
1
        )
156
1
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
157
1
        .route_layer(middleware::from_fn(report::retired::reject_retired_filter))
158
1
        .route_layer(middleware::from_fn(full_page))
159
1
        .with_state(app_state.clone())
160
1
}
161

            
162
15
pub fn create_api_router(
163
15
    app_state: Arc<AppState>,
164
15
    limiter: Arc<AuthLimiter>,
165
15
) -> Router<Arc<AppState>> {
166
    // The login routes verify a password against a hash for anyone who asks,
167
    // so they carry a per-address throttle. `/version` is outside it: it reads
168
    // nothing and costs nothing, and throttling it would only make an unrelated
169
    // health check share a budget with the credential path.
170
    // The form is the one POST that answers an error by showing itself again,
171
    // so `reshow_login` sits outside the throttle to cover its refusal too.
172
15
    let login_form_router = Router::new()
173
15
        .route("/auth/login", post(login_form_handler))
174
15
        .route_layer(middleware::from_fn_with_state(
175
15
            limiter.clone(),
176
15
            throttle_auth,
177
15
        ))
178
15
        .route_layer(middleware::from_fn(reshow_login));
179

            
180
15
    let login_json_router = Router::new()
181
15
        .route("/auth/login/json", post(login_user_handler))
182
15
        .route_layer(middleware::from_fn_with_state(
183
15
            limiter.clone(),
184
15
            throttle_auth,
185
15
        ));
186

            
187
15
    let public_router = login_form_router
188
15
        .merge(login_json_router)
189
15
        .route("/version", get(get_version));
190

            
191
    // Both layers: it verifies a password, so it belongs behind the same
192
    // throttle as login, and it acts on the caller, so it needs `auth`.
193
15
    let password_router = Router::new()
194
15
        .route("/auth/password", post(change_password_handler))
195
15
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
196
15
        .route_layer(middleware::from_fn_with_state(limiter, throttle_auth))
197
15
        .with_state(app_state.clone());
198

            
199
15
    let auth_router = Router::new()
200
15
        .route("/auth/refresh", get(refresh_access_token_handler))
201
15
        .route("/auth/logout", get(logout_handler))
202
15
        .route("/htmx/logoutlink", get(get_logout_link))
203
15
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
204
15
        .with_state(app_state.clone());
205

            
206
15
    let account_router = Router::new()
207
15
        .route("/account/list", get(account::list::account_table))
208
15
        .route("/account/info", get(account::list::account_info))
209
15
        .route(
210
15
            "/account/manage/view",
211
15
            get(account::manage::account_manage_view),
212
15
        )
213
15
        .route(
214
15
            "/account/manage/submit",
215
15
            post(account::manage::account_manage_submit),
216
15
        )
217
15
        .route(
218
15
            "/account/create/submit",
219
15
            post(account::create::account_create_submit),
220
15
        )
221
15
        .route("/account/ssh-key/add", post(account::ssh_key::ssh_key_add))
222
15
        .route(
223
15
            "/account/merge/review",
224
15
            post(account::merge::account_merge_review),
225
15
        )
226
15
        .route(
227
15
            "/account/merge/submit",
228
15
            post(account::merge::account_merge_submit),
229
15
        )
230
15
        .route(
231
15
            "/account/ssh-key/remove",
232
15
            post(account::ssh_key::ssh_key_remove),
233
15
        )
234
15
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
235
15
        .with_state(app_state.clone());
236

            
237
15
    let nav_router = Router::new()
238
15
        .route("/nav/home", get(get_home_link))
239
15
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
240
15
        .with_state(app_state.clone());
241

            
242
15
    let commodity_router = Router::new()
243
15
        .route("/commodity/list", get(commodity::list::commodity_table))
244
15
        .route(
245
15
            "/commodity/create/submit",
246
15
            post(commodity::create::commodity_create_submit),
247
15
        )
248
15
        .route(
249
15
            "/commodity/edit",
250
15
            post(commodity::edit::commodity_edit_submit),
251
15
        )
252
15
        .route(
253
15
            "/commodity/convert",
254
15
            get(commodity::convert::commodity_convert),
255
15
        )
256
15
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
257
15
        .with_state(app_state.clone());
258

            
259
15
    let transaction_router = Router::new()
260
15
        .route(
261
15
            "/transaction/detail/{id}",
262
15
            get(crate::pages::transaction::browse::transaction_detail),
263
15
        )
264
15
        .route(
265
15
            "/transaction/new/submit",
266
15
            post(crate::pages::transaction::new::transaction_new_submit),
267
15
        )
268
15
        .route(
269
15
            "/transaction/update",
270
15
            post(crate::pages::transaction::update::transaction_update_submit),
271
15
        )
272
15
        .route(
273
15
            "/transaction/delete/{id}",
274
15
            post(crate::pages::transaction::delete::submit::transaction_delete_submit),
275
15
        )
276
15
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
277
15
        .with_state(app_state.clone());
278

            
279
15
    let tag_router = Router::new()
280
15
        .route("/tag/list", get(tag::list::tag_table))
281
15
        .route("/tag/create/submit", post(tag::create::tag_create_submit))
282
15
        .route("/tag/edit/submit", post(tag::edit::tag_edit_submit))
283
15
        .route("/tag/delete/{id}", post(tag::edit::delete_tag))
284
15
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
285
15
        .with_state(app_state.clone());
286

            
287
15
    let config_router = Router::new()
288
15
        .route("/config/list", get(crate::pages::config::config_table))
289
15
        .route("/config/set", post(crate::pages::config::config_set_submit))
290
15
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
291
15
        .with_state(app_state.clone());
292

            
293
15
    let forms_router = Router::new()
294
15
        .route(
295
15
            "/forms/options/{source}",
296
15
            get(crate::pages::forms::options::form_options),
297
15
        )
298
15
        .route(
299
15
            "/forms/suggestions/{source}",
300
15
            get(crate::pages::forms::suggestions::form_suggestions),
301
15
        )
302
15
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
303
15
        .with_state(app_state.clone());
304

            
305
15
    let script_router = Router::new()
306
15
        .route("/script/list", get(script::list::script_table))
307
15
        .route(
308
15
            "/script/create/submit",
309
15
            post(script::create::script_create_submit),
310
15
        )
311
15
        .route(
312
15
            "/script/edit/submit",
313
15
            post(script::edit::script_edit_submit),
314
15
        )
315
15
        .route("/script/delete/{id}", post(script::edit::delete_script))
316
15
        .route(
317
15
            "/account/{account_id}/script/run/{script_id}",
318
15
            post(account::script::run_account_script),
319
15
        )
320
15
        .route("/template/list", get(template::list::template_table))
321
15
        .route(
322
15
            "/template/create/submit",
323
15
            post(template::create::template_create_submit),
324
15
        )
325
15
        .route("/template/test", post(template::draft::template_test))
326
15
        .route(
327
15
            "/template/edit/submit",
328
15
            post(template::edit::template_edit_submit),
329
15
        )
330
15
        .route(
331
15
            "/template/delete/{id}",
332
15
            post(template::edit::delete_template),
333
15
        )
334
15
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
335
15
        .with_state(app_state.clone());
336

            
337
15
    let report_router = Router::new()
338
15
        .route(
339
15
            "/report/balance",
340
15
            get(report::balance::balance_report_table),
341
15
        )
342
15
        .route(
343
15
            "/report/balance/chart.svg",
344
15
            get(report::balance::balance_report_chart_svg),
345
15
        )
346
15
        .route(
347
15
            "/report/balance/chart.json",
348
15
            get(report::balance::balance_report_chart_json),
349
15
        )
350
15
        .route(
351
15
            "/report/activity",
352
15
            get(report::activity::activity_report_table),
353
15
        )
354
15
        .route(
355
15
            "/report/activity/chart.svg",
356
15
            get(report::activity::activity_report_chart_svg),
357
15
        )
358
15
        .route(
359
15
            "/report/activity/chart.json",
360
15
            get(report::activity::activity_report_chart_json),
361
15
        )
362
15
        .route(
363
15
            "/report/category-breakdown",
364
15
            get(report::category_breakdown::category_breakdown_report_table),
365
15
        )
366
15
        .route(
367
15
            "/report/category-breakdown/chart.svg",
368
15
            get(report::category_breakdown::category_breakdown_report_chart_svg),
369
15
        )
370
15
        .route(
371
15
            "/report/category-breakdown/chart.json",
372
15
            get(report::category_breakdown::category_breakdown_report_chart_json),
373
15
        )
374
15
        .route(
375
15
            "/report/category-breakdown/export.csv",
376
15
            get(report::category_breakdown_csv::category_breakdown_report_csv),
377
15
        )
378
15
        .route_layer(middleware::from_fn_with_state(app_state.clone(), auth))
379
15
        .route_layer(middleware::from_fn(report::bookmark::push_report_url))
380
15
        .route_layer(middleware::from_fn(report::retired::reject_retired_filter))
381
15
        .with_state(app_state.clone());
382

            
383
15
    let admin_router = Router::new()
384
15
        .route("/auth/register", post(register_user_handler))
385
15
        .route_layer(middleware::from_fn_with_state(app_state.clone(), admin))
386
15
        .route_layer(middleware::from_fn_with_state(app_state, auth));
387

            
388
15
    let router = Router::new()
389
15
        .merge(public_router)
390
15
        .merge(password_router)
391
15
        .merge(nav_router)
392
15
        .merge(auth_router)
393
15
        .merge(account_router)
394
15
        .merge(commodity_router)
395
15
        .merge(transaction_router)
396
15
        .merge(tag_router)
397
15
        .merge(config_router)
398
15
        .merge(forms_router)
399
15
        .merge(report_router)
400
15
        .merge(admin_router);
401

            
402
15
    router.merge(script_router)
403
15
}