about summary refs log tree commit diff
path: root/web/pwcrypt/src/main.rs
blob: 2b9b82abb022a7053421b180d1f156c1d668c00f (plain) (blame)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use argon2::password_hash::{PasswordHasher, SaltString};
use argon2::Argon2;
use gloo::console::log;
use rand_core::OsRng;
use web_sys::HtmlInputElement;
use yew::prelude::*;

fn hash_password(pw: &str) -> String {
    let salt = SaltString::generate(&mut OsRng);
    let argon2 = Argon2::default();
    argon2
        .hash_password(pw.as_bytes(), &salt)
        .expect("failed to hash pw")
        .to_string()
}

enum Msg {
    NoOp,
    SetEmail(String),
    SetPassword(String),
    SetUsername(String),
    UpdateCredentials,
}

#[derive(Default)]
struct App {
    email: Option<String>,
    password: Option<String>,
    username: Option<String>,
    hashed: Option<String>,
}

impl App {
    fn whats_missing(&self) -> Option<String> {
        let mut missing = vec![];

        if self.username.is_none() {
            missing.push("username");
        }

        if self.email.is_none() {
            missing.push("email");
        }

        if self.password.is_none() {
            missing.push("password");
        }

        match missing.len() {
            0 => None,
            1 => Some(missing[0].to_string()),
            2 => Some(format!("{} and {}", missing[0], missing[1])),
            3 => Some(format!("{}, {} and {}", missing[0], missing[1], missing[2])),
            _ => unreachable!(),
        }
    }

    fn update_credentials(&mut self) {
        if self.password.is_none() {
            log!("error: password unset, but credentials requested");
            return;
        }

        let pw = self.password.as_ref().unwrap();
        let hashed = hash_password(pw);
        log!("hashed password to", &hashed);
        self.hashed = Some(hashed);
    }

    fn display_credentials(&self) -> Html {
        if let (Some(username), Some(email), Some(hash)) =
            (&self.username, &self.email, &self.hashed)
        {
            html! {
              <>
                <hr />
                <p>{"Your credentials are as follows: "}</p>
                <pre>
                  {"  {\n"}
                  {"    username = \""}{username}{"\";\n"}
                  {"    email = \""}{email}{"\";\n"}
                  {"    password = \"{ARGON2}"}{hash}{"\";\n"}
                  {"  }"}
                </pre>
                <p>
                  {"Please propose a CL to "}
                  <a href="https://at.tvl.fyi/?q=//ops/users/default.nix">
                    <code>{"//ops/users/default.nix"}</code>
                  </a>
                  {", or submit your patch via email to "}
                  <a href="mailto:depot@tvl.su">{"depot@tvl.su"}</a>
                  {"."}
                </p>
              </>
            }
        } else {
            html! {}
        }
    }
}

fn input_to_message(event: InputEvent, msg: fn(String) -> Msg) -> Msg {
    let input = event.target_unchecked_into::<HtmlInputElement>();
    if input.check_validity() {
        msg(input.value())
    } else {
        Msg::NoOp
    }
}

fn set_if_present(s: String, target: &mut Option<String>) {
    if s.is_empty() {
        *target = None;
    } else {
        *target = Some(s);
    }
}

impl Component for App {
    type Message = Msg;
    type Properties = ();

    fn create(_: &Context<Self>) -> Self {
        Self::default()
    }

    fn update(&mut self, _: &Context<Self>, msg: Self::Message) -> bool {
        log!(
            "handling message ",
            match msg {
                Msg::NoOp => "NoOp",
                Msg::SetEmail(_) => "SetEmail",
                Msg::SetUsername(_) => "SetUsername",
                Msg::SetPassword(_) => "SetPassword",
                Msg::UpdateCredentials => "UpdateCredentials",
            }
        );

        match msg {
            Msg::NoOp => return false,
            Msg::SetEmail(email) => set_if_present(email, &mut self.email),
            Msg::SetUsername(username) => set_if_present(username, &mut self.username),
            Msg::SetPassword(password) => set_if_present(password, &mut self.password),
            Msg::UpdateCredentials => {
                self.update_credentials();
            }
        }

        true
    }

    fn view(&self, ctx: &Context<Self>) -> Html {
        let link = ctx.link();
        include!("main.html")
    }
}

fn main() {
    yew::Renderer::<App>::new().render();
}