1
//! Handlers of all OAuth2 functions.
2

            
3
use axum::{http::StatusCode, response::IntoResponse, routing, Extension, Router};
4
use tera::Tera;
5

            
6
use super::State as AppState;
7

            
8
mod api;
9
pub(crate) mod endpoint;
10
pub mod middleware;
11
mod primitive;
12
pub(crate) mod request;
13
pub(crate) mod response;
14
mod template;
15

            
16
974
pub fn new_service(state: &AppState) -> Router {
17
974
    let templates = state.templates.clone();
18
974
    let mut tera = Tera::default();
19
974
    let _ = match templates.get("login") {
20
973
        None => match tera.add_raw_template(api::TMPL_LOGIN, template::LOGIN) {
21
            Err(e) => panic!("login default template error: {}", e),
22
973
            Ok(_) => (),
23
        },
24
1
        Some(template) => match tera.add_template_file(template.as_str(), Some(api::TMPL_LOGIN)) {
25
            Err(e) => panic!("login template file {} error: {}", template.as_str(), e),
26
1
            Ok(_) => (),
27
        },
28
    };
29
974
    let _ = match templates.get("grant") {
30
973
        None => match tera.add_raw_template(api::TMPL_GRANT, template::GRANT) {
31
            Err(e) => panic!("grant default template error: {}", e),
32
973
            Ok(_) => (),
33
        },
34
1
        Some(template) => match tera.add_template_file(template.as_str(), Some(api::TMPL_GRANT)) {
35
            Err(e) => panic!("grant template file {} error: {}", template.as_str(), e),
36
1
            Ok(_) => (),
37
        },
38
    };
39

            
40
974
    Router::new()
41
974
        .route("/auth", routing::get(api::get_auth))
42
974
        .route("/login", routing::get(api::get_login).post(api::post_login))
43
974
        .route(
44
974
            "/authorize",
45
974
            routing::get(api::authorize).post(api::authorize),
46
974
        )
47
974
        .route("/token", routing::post(api::post_token))
48
974
        .route("/refresh", routing::post(api::post_refresh))
49
974
        .route("/redirect", routing::get(redirect))
50
974
        .layer(Extension(tera))
51
974
        .with_state(state.clone())
52
974
}
53

            
54
/// The built-in redirect path for getting authorization codes.
55
async fn redirect() -> impl IntoResponse {
56
    StatusCode::OK
57
}