fedimint_server_ui/
lib.rs

1pub mod assets;
2pub(crate) mod auth;
3pub mod dashboard;
4pub mod setup;
5
6use axum::response::{Html, IntoResponse, Redirect};
7use axum_extra::extract::cookie::{Cookie, CookieJar, SameSite};
8use fedimint_core::hex::ToHex;
9use fedimint_core::module::ApiAuth;
10use fedimint_core::secp256k1::rand::{Rng, thread_rng};
11use maud::{DOCTYPE, Markup, html};
12use serde::Deserialize;
13
14pub(crate) const LOG_UI: &str = "fm::ui";
15
16// Common route constants
17pub const ROOT_ROUTE: &str = "/";
18pub const LOGIN_ROUTE: &str = "/login";
19
20pub fn common_head(title: &str) -> Markup {
21    html! {
22        meta charset="utf-8";
23        meta name="viewport" content="width=device-width, initial-scale=1.0";
24        title { "Guardian Dashboard"}
25        link rel="stylesheet" href="/assets/bootstrap.min.css" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous";
26        link rel="stylesheet" type="text/css" href="/assets/style.css";
27        link rel="icon" type="image/png" href="/assets/logo.png";
28
29        // Note: this needs to be included in the header, so that web-page does not
30        // get in a state where htmx is not yet loaded. `deref` helps with blocking the load.
31        // Learned the hard way. --dpc
32        script defer src="/assets/htmx.org-2.0.4.min.js" {}
33
34        title { (title) }
35    }
36}
37
38#[derive(Debug, Deserialize)]
39pub(crate) struct LoginInput {
40    pub password: String,
41}
42
43/// Generic state for both setup and dashboard UIs
44#[derive(Clone)]
45pub struct UiState<T> {
46    pub(crate) api: T,
47    pub(crate) auth_cookie_name: String,
48    pub(crate) auth_cookie_value: String,
49}
50
51impl<T> UiState<T> {
52    pub fn new(api: T) -> Self {
53        Self {
54            api,
55            auth_cookie_name: thread_rng().r#gen::<[u8; 4]>().encode_hex(),
56            auth_cookie_value: thread_rng().r#gen::<[u8; 32]>().encode_hex(),
57        }
58    }
59}
60
61pub(crate) fn login_layout(title: &str, content: Markup) -> Markup {
62    html! {
63        (DOCTYPE)
64        html {
65            head {
66                (common_head(title))
67            }
68            body {
69                div class="container" {
70                    div class="row justify-content-center" {
71                        div class="col-md-8 col-lg-5 narrow-container" {
72                            header class="text-center" {
73                                h1 class="header-title" { "Fedimint Guardian UI" }
74                            }
75
76                            div class="card" {
77                                div class="card-body" {
78                                    (content)
79                                }
80                            }
81                        }
82                    }
83                }
84                script src="/assets/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous" {}
85            }
86        }
87    }
88}
89
90pub(crate) fn login_form_response() -> impl IntoResponse {
91    let content = html! {
92        form method="post" action="/login" {
93            div class="form-group mb-4" {
94                input type="password" class="form-control" id="password" name="password" placeholder="Your password" required;
95            }
96            div class="button-container" {
97                button type="submit" class="btn btn-primary setup-btn" { "Log In" }
98            }
99        }
100    };
101
102    Html(login_layout("Fedimint Guardian Login", content).into_string()).into_response()
103}
104
105pub(crate) fn login_submit_response(
106    auth: ApiAuth,
107    auth_cookie_name: String,
108    auth_cookie_value: String,
109    jar: CookieJar,
110    input: LoginInput,
111) -> impl IntoResponse {
112    if auth.0 == input.password {
113        let mut cookie = Cookie::new(auth_cookie_name, auth_cookie_value);
114
115        cookie.set_http_only(true);
116        cookie.set_same_site(Some(SameSite::Lax));
117
118        return (jar.add(cookie), Redirect::to("/")).into_response();
119    }
120
121    let content = html! {
122        div class="alert alert-danger" { "The password is invalid" }
123        div class="button-container" {
124            a href="/login" class="btn btn-primary setup-btn" { "Return to Login" }
125        }
126    };
127
128    Html(login_layout("Login Failed", content).into_string()).into_response()
129}