lldap/app/src/components/login.rs

182 lines
6.4 KiB
Rust
Raw Normal View History

2021-10-31 12:20:30 +00:00
use crate::infra::{
api::HostService,
common_component::{CommonComponent, CommonComponentParts},
};
2021-09-19 18:21:37 +00:00
use anyhow::{anyhow, bail, Context, Result};
use lldap_auth::*;
2021-09-19 18:21:37 +00:00
use validator_derive::Validate;
2021-10-31 12:20:30 +00:00
use yew::{prelude::*, services::ConsoleService};
2021-09-19 18:21:37 +00:00
use yew_form::Form;
use yew_form_derive::Model;
2021-05-13 17:33:57 +00:00
pub struct LoginForm {
2021-10-31 12:20:30 +00:00
common: CommonComponentParts<Self>,
2021-09-19 18:21:37 +00:00
form: Form<FormModel>,
2021-05-13 17:33:57 +00:00
}
2021-09-19 18:21:37 +00:00
/// The fields of the form, with the constraints.
#[derive(Model, Validate, PartialEq, Clone, Default)]
pub struct FormModel {
#[validate(length(min = 1, message = "Missing username"))]
username: String,
#[validate(length(min = 8, message = "Invalid password. Min length: 8"))]
password: String,
}
2021-05-13 17:33:57 +00:00
#[derive(Clone, PartialEq, Properties)]
pub struct Props {
pub on_logged_in: Callback<(String, bool)>,
2021-05-13 17:33:57 +00:00
}
pub enum Msg {
2021-09-19 18:21:37 +00:00
Update,
2021-05-13 17:33:57 +00:00
Submit,
2021-09-19 18:21:37 +00:00
AuthenticationStartResponse(
(
opaque::client::login::ClientLogin,
Result<Box<login::ServerLoginStartResponse>>,
),
),
AuthenticationFinishResponse(Result<(String, bool)>),
}
2021-10-31 12:20:30 +00:00
impl CommonComponent<LoginForm> for LoginForm {
fn handle_msg(&mut self, msg: <Self as Component>::Message) -> Result<bool> {
match msg {
2021-09-19 18:21:37 +00:00
Msg::Update => Ok(true),
Msg::Submit => {
2021-09-19 18:21:37 +00:00
if !self.form.validate() {
bail!("Check the form for errors");
2021-09-19 18:21:37 +00:00
}
let FormModel { username, password } = self.form.model();
let mut rng = rand::rngs::OsRng;
2021-09-19 18:21:37 +00:00
let opaque::client::login::ClientLoginStartResult { state, message } =
opaque::client::login::start_login(&password, &mut rng)
.context("Could not initialize login")?;
let req = login::ClientLoginStartRequest {
username,
2021-09-19 18:21:37 +00:00
login_start_request: message,
};
2021-10-31 12:20:30 +00:00
self.common
.call_backend(HostService::login_start, req, move |r| {
Msg::AuthenticationStartResponse((state, r))
})?;
Ok(true)
}
2021-09-19 18:21:37 +00:00
Msg::AuthenticationStartResponse((login_start, res)) => {
let res = res.context("Could not log in (invalid response to login start)")?;
let login_finish =
match opaque::client::login::finish_login(login_start, res.credential_response)
{
Err(e) => {
// Common error, we want to print a full error to the console but only a
// simple one to the user.
ConsoleService::error(&format!("Invalid username or password: {}", e));
2021-10-31 12:20:30 +00:00
self.common.error = Some(anyhow!("Invalid username or password"));
self.common.cancel_task();
2021-09-19 18:21:37 +00:00
return Ok(true);
}
Ok(l) => l,
};
let req = login::ClientLoginFinishRequest {
server_data: res.server_data,
credential_finalization: login_finish.message,
};
2021-10-31 12:20:30 +00:00
self.common.call_backend(
HostService::login_finish,
req,
2021-10-31 12:20:30 +00:00
Msg::AuthenticationFinishResponse,
)?;
2021-09-19 18:21:37 +00:00
Ok(false)
}
2021-09-19 18:21:37 +00:00
Msg::AuthenticationFinishResponse(user_info) => {
2021-10-31 12:20:30 +00:00
self.common.cancel_task();
self.common
.on_logged_in
2021-09-19 18:21:37 +00:00
.emit(user_info.context("Could not log in")?);
Ok(true)
}
}
}
2021-10-31 12:20:30 +00:00
fn mut_common(&mut self) -> &mut CommonComponentParts<Self> {
&mut self.common
}
}
2021-05-13 17:33:57 +00:00
impl Component for LoginForm {
type Message = Msg;
type Properties = Props;
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
LoginForm {
2021-10-31 12:20:30 +00:00
common: CommonComponentParts::<Self>::create(props, link),
2021-09-19 18:21:37 +00:00
form: Form::<FormModel>::new(FormModel::default()),
2021-05-13 17:33:57 +00:00
}
}
fn update(&mut self, msg: Self::Message) -> ShouldRender {
2021-10-31 12:20:30 +00:00
CommonComponentParts::<Self>::update(self, msg)
2021-05-13 17:33:57 +00:00
}
fn change(&mut self, _: Self::Properties) -> ShouldRender {
false
}
fn view(&self) -> Html {
2021-09-19 18:21:37 +00:00
type Field = yew_form::Field<FormModel>;
2021-05-13 17:33:57 +00:00
html! {
2021-09-19 17:44:53 +00:00
<form
2021-09-19 18:21:37 +00:00
class="form center-block col-sm-4 col-offset-4">
2021-09-19 17:44:53 +00:00
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">
<i class="bi-person-fill"/>
</span>
</div>
2021-09-19 18:21:37 +00:00
<Field
2021-09-19 17:44:53 +00:00
class="form-control"
2021-09-19 18:21:37 +00:00
class_invalid="is-invalid has-error"
class_valid="has-success"
form=&self.form
field_name="username"
2021-09-19 17:44:53 +00:00
placeholder="Username"
2021-09-19 18:21:37 +00:00
autocomplete="username"
2021-10-31 12:20:30 +00:00
oninput=self.common.callback(|_| Msg::Update) />
2021-05-13 17:33:57 +00:00
</div>
2021-09-19 17:44:53 +00:00
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">
<i class="bi-lock-fill"/>
</span>
</div>
2021-09-19 18:21:37 +00:00
<Field
2021-09-19 17:44:53 +00:00
class="form-control"
2021-09-19 18:21:37 +00:00
class_invalid="is-invalid has-error"
class_valid="has-success"
form=&self.form
field_name="password"
input_type="password"
2021-10-06 15:44:49 +00:00
placeholder="Password"
2021-09-19 17:44:53 +00:00
autocomplete="current-password" />
2021-05-13 17:33:57 +00:00
</div>
2021-09-19 17:44:53 +00:00
<div class="form-group">
<button
type="submit"
2021-09-19 18:21:37 +00:00
class="btn btn-primary"
2021-10-31 12:20:30 +00:00
disabled=self.common.is_task_running()
onclick=self.common.callback(|e: MouseEvent| {e.prevent_default(); Msg::Submit})>
2021-09-19 17:44:53 +00:00
{"Login"}
</button>
</div>
<div class="form-group">
2021-10-31 12:20:30 +00:00
{ if let Some(e) = &self.common.error {
2021-05-13 17:33:57 +00:00
html! { e.to_string() }
} else { html! {} }
}
</div>
</form>
}
}
}