2021-09-12 09:54:47 +00:00
|
|
|
use crate::{
|
|
|
|
components::router::{AppRoute, NavButton},
|
2021-10-29 03:32:27 +00:00
|
|
|
infra::{
|
|
|
|
api::HostService,
|
|
|
|
common_component::{CommonComponent, CommonComponentParts},
|
|
|
|
},
|
2021-09-12 09:54:47 +00:00
|
|
|
};
|
|
|
|
use anyhow::{anyhow, bail, Context, Result};
|
|
|
|
use lldap_auth::*;
|
2021-10-12 03:02:20 +00:00
|
|
|
use validator_derive::Validate;
|
2021-10-29 03:32:27 +00:00
|
|
|
use yew::{prelude::*, services::ConsoleService};
|
2021-10-12 03:02:20 +00:00
|
|
|
use yew_form::Form;
|
|
|
|
use yew_form_derive::Model;
|
|
|
|
use yew_router::{
|
|
|
|
agent::{RouteAgentDispatcher, RouteRequest},
|
|
|
|
route::Route,
|
|
|
|
};
|
2021-09-12 09:54:47 +00:00
|
|
|
|
|
|
|
#[derive(PartialEq, Eq)]
|
|
|
|
enum OpaqueData {
|
|
|
|
None,
|
|
|
|
Login(opaque::client::login::ClientLogin),
|
|
|
|
Registration(opaque::client::registration::ClientRegistration),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for OpaqueData {
|
|
|
|
fn default() -> Self {
|
|
|
|
OpaqueData::None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OpaqueData {
|
|
|
|
fn take(&mut self) -> Self {
|
|
|
|
std::mem::take(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-12 03:02:20 +00:00
|
|
|
/// The fields of the form, with the constraints.
|
2022-09-27 04:48:39 +00:00
|
|
|
#[derive(Model, Validate, PartialEq, Eq, Clone, Default)]
|
2021-10-12 03:02:20 +00:00
|
|
|
pub struct FormModel {
|
|
|
|
#[validate(custom(
|
|
|
|
function = "empty_or_long",
|
|
|
|
message = "Password should be longer than 8 characters"
|
|
|
|
))]
|
|
|
|
old_password: String,
|
|
|
|
#[validate(length(min = 8, message = "Invalid password. Min length: 8"))]
|
|
|
|
password: String,
|
|
|
|
#[validate(must_match(other = "password", message = "Passwords must match"))]
|
|
|
|
confirm_password: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn empty_or_long(value: &str) -> Result<(), validator::ValidationError> {
|
|
|
|
if value.is_empty() || value.len() >= 8 {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(validator::ValidationError::new(""))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 09:54:47 +00:00
|
|
|
pub struct ChangePasswordForm {
|
2021-10-29 03:32:27 +00:00
|
|
|
common: CommonComponentParts<Self>,
|
2021-10-12 03:02:20 +00:00
|
|
|
form: Form<FormModel>,
|
2021-09-12 09:54:47 +00:00
|
|
|
opaque_data: OpaqueData,
|
2021-10-12 03:02:20 +00:00
|
|
|
route_dispatcher: RouteAgentDispatcher,
|
2021-09-12 09:54:47 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 04:48:39 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Properties)]
|
2021-09-12 09:54:47 +00:00
|
|
|
pub struct Props {
|
|
|
|
pub username: String,
|
2021-09-14 07:58:04 +00:00
|
|
|
pub is_admin: bool,
|
2021-09-12 09:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Msg {
|
2021-10-12 03:02:20 +00:00
|
|
|
FormUpdate,
|
2021-09-12 09:54:47 +00:00
|
|
|
Submit,
|
|
|
|
AuthenticationStartResponse(Result<Box<login::ServerLoginStartResponse>>),
|
2021-09-14 07:58:04 +00:00
|
|
|
SubmitNewPassword,
|
2021-09-12 09:54:47 +00:00
|
|
|
RegistrationStartResponse(Result<Box<registration::ServerRegistrationStartResponse>>),
|
|
|
|
RegistrationFinishResponse(Result<()>),
|
|
|
|
}
|
|
|
|
|
2021-10-29 03:32:27 +00:00
|
|
|
impl CommonComponent<ChangePasswordForm> for ChangePasswordForm {
|
|
|
|
fn handle_msg(&mut self, msg: <Self as Component>::Message) -> Result<bool> {
|
2021-09-12 09:54:47 +00:00
|
|
|
match msg {
|
2021-10-12 03:02:20 +00:00
|
|
|
Msg::FormUpdate => Ok(true),
|
2021-09-12 09:54:47 +00:00
|
|
|
Msg::Submit => {
|
2021-10-12 03:02:20 +00:00
|
|
|
if !self.form.validate() {
|
|
|
|
bail!("Check the form for errors");
|
2021-09-12 09:54:47 +00:00
|
|
|
}
|
2021-10-29 03:32:27 +00:00
|
|
|
if self.common.is_admin {
|
|
|
|
self.handle_msg(Msg::SubmitNewPassword)
|
2021-09-14 07:58:04 +00:00
|
|
|
} else {
|
2021-10-12 03:02:20 +00:00
|
|
|
let old_password = self.form.model().old_password;
|
|
|
|
if old_password.is_empty() {
|
|
|
|
bail!("Current password should not be empty");
|
|
|
|
}
|
2021-09-14 07:58:04 +00:00
|
|
|
let mut rng = rand::rngs::OsRng;
|
|
|
|
let login_start_request =
|
|
|
|
opaque::client::login::start_login(&old_password, &mut rng)
|
|
|
|
.context("Could not initialize login")?;
|
|
|
|
self.opaque_data = OpaqueData::Login(login_start_request.state);
|
|
|
|
let req = login::ClientLoginStartRequest {
|
2021-10-29 03:32:27 +00:00
|
|
|
username: self.common.username.clone(),
|
2021-09-14 07:58:04 +00:00
|
|
|
login_start_request: login_start_request.message,
|
|
|
|
};
|
2021-10-29 03:32:27 +00:00
|
|
|
self.common.call_backend(
|
2021-09-14 07:58:04 +00:00
|
|
|
HostService::login_start,
|
|
|
|
req,
|
|
|
|
Msg::AuthenticationStartResponse,
|
|
|
|
)?;
|
2021-10-12 03:02:20 +00:00
|
|
|
Ok(true)
|
2021-09-14 07:58:04 +00:00
|
|
|
}
|
2021-09-12 09:54:47 +00:00
|
|
|
}
|
|
|
|
Msg::AuthenticationStartResponse(res) => {
|
|
|
|
let res = res.context("Could not initiate login")?;
|
|
|
|
match self.opaque_data.take() {
|
|
|
|
OpaqueData::Login(l) => {
|
|
|
|
opaque::client::login::finish_login(l, res.credential_response).map_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
|
|
|
|
));
|
|
|
|
anyhow!("Invalid username or password")
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
_ => panic!("Unexpected data in opaque_data field"),
|
|
|
|
};
|
2021-10-29 03:32:27 +00:00
|
|
|
self.handle_msg(Msg::SubmitNewPassword)
|
2021-09-14 07:58:04 +00:00
|
|
|
}
|
|
|
|
Msg::SubmitNewPassword => {
|
2021-09-12 09:54:47 +00:00
|
|
|
let mut rng = rand::rngs::OsRng;
|
2021-10-12 03:02:20 +00:00
|
|
|
let new_password = self.form.model().password;
|
2021-09-12 09:54:47 +00:00
|
|
|
let registration_start_request =
|
|
|
|
opaque::client::registration::start_registration(&new_password, &mut rng)
|
|
|
|
.context("Could not initiate password change")?;
|
|
|
|
let req = registration::ClientRegistrationStartRequest {
|
2021-10-29 03:32:27 +00:00
|
|
|
username: self.common.username.clone(),
|
2021-09-12 09:54:47 +00:00
|
|
|
registration_start_request: registration_start_request.message,
|
|
|
|
};
|
|
|
|
self.opaque_data = OpaqueData::Registration(registration_start_request.state);
|
2021-10-29 03:32:27 +00:00
|
|
|
self.common.call_backend(
|
2021-09-12 09:54:47 +00:00
|
|
|
HostService::register_start,
|
|
|
|
req,
|
|
|
|
Msg::RegistrationStartResponse,
|
|
|
|
)?;
|
2021-10-12 03:02:20 +00:00
|
|
|
Ok(true)
|
2021-09-12 09:54:47 +00:00
|
|
|
}
|
|
|
|
Msg::RegistrationStartResponse(res) => {
|
|
|
|
let res = res.context("Could not initiate password change")?;
|
|
|
|
match self.opaque_data.take() {
|
|
|
|
OpaqueData::Registration(registration) => {
|
|
|
|
let mut rng = rand::rngs::OsRng;
|
|
|
|
let registration_finish =
|
|
|
|
opaque::client::registration::finish_registration(
|
|
|
|
registration,
|
|
|
|
res.registration_response,
|
|
|
|
&mut rng,
|
|
|
|
)
|
|
|
|
.context("Error during password change")?;
|
|
|
|
let req = registration::ClientRegistrationFinishRequest {
|
|
|
|
server_data: res.server_data,
|
|
|
|
registration_upload: registration_finish.message,
|
|
|
|
};
|
2021-10-29 03:32:27 +00:00
|
|
|
self.common.call_backend(
|
2021-09-12 09:54:47 +00:00
|
|
|
HostService::register_finish,
|
|
|
|
req,
|
|
|
|
Msg::RegistrationFinishResponse,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
_ => panic!("Unexpected data in opaque_data field"),
|
2021-10-12 03:02:20 +00:00
|
|
|
}?;
|
|
|
|
Ok(false)
|
2021-09-12 09:54:47 +00:00
|
|
|
}
|
|
|
|
Msg::RegistrationFinishResponse(response) => {
|
2021-10-31 12:19:54 +00:00
|
|
|
self.common.cancel_task();
|
2021-09-12 09:54:47 +00:00
|
|
|
if response.is_ok() {
|
2021-10-12 03:02:20 +00:00
|
|
|
self.route_dispatcher
|
|
|
|
.send(RouteRequest::ChangeRoute(Route::from(
|
2021-10-29 03:32:27 +00:00
|
|
|
AppRoute::UserDetails(self.common.username.clone()),
|
2021-10-12 03:02:20 +00:00
|
|
|
)));
|
2021-09-12 09:54:47 +00:00
|
|
|
}
|
2021-10-12 03:02:20 +00:00
|
|
|
response?;
|
|
|
|
Ok(true)
|
2021-09-12 09:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-29 03:32:27 +00:00
|
|
|
|
|
|
|
fn mut_common(&mut self) -> &mut CommonComponentParts<Self> {
|
|
|
|
&mut self.common
|
|
|
|
}
|
2021-09-12 09:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for ChangePasswordForm {
|
|
|
|
type Message = Msg;
|
|
|
|
type Properties = Props;
|
|
|
|
|
|
|
|
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
|
|
|
|
ChangePasswordForm {
|
2021-10-29 03:32:27 +00:00
|
|
|
common: CommonComponentParts::<Self>::create(props, link),
|
2021-10-12 03:02:20 +00:00
|
|
|
form: yew_form::Form::<FormModel>::new(FormModel::default()),
|
2021-09-12 09:54:47 +00:00
|
|
|
opaque_data: OpaqueData::None,
|
2021-10-12 03:02:20 +00:00
|
|
|
route_dispatcher: RouteAgentDispatcher::new(),
|
2021-09-12 09:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update(&mut self, msg: Self::Message) -> ShouldRender {
|
2021-10-29 03:32:27 +00:00
|
|
|
CommonComponentParts::<Self>::update(self, msg)
|
2021-09-12 09:54:47 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 07:27:51 +00:00
|
|
|
fn change(&mut self, props: Self::Properties) -> ShouldRender {
|
|
|
|
self.common.change(props)
|
2021-09-12 09:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn view(&self) -> Html {
|
2021-10-29 03:32:27 +00:00
|
|
|
let is_admin = self.common.is_admin;
|
2021-10-12 03:02:20 +00:00
|
|
|
type Field = yew_form::Field<FormModel>;
|
2021-09-12 09:54:47 +00:00
|
|
|
html! {
|
2021-10-12 03:02:20 +00:00
|
|
|
<>
|
2022-11-03 14:40:02 +00:00
|
|
|
<div class="mb-2 mt-2">
|
|
|
|
<h5 class="fw-bold">
|
|
|
|
{"Change password"}
|
|
|
|
</h5>
|
|
|
|
</div>
|
|
|
|
{
|
|
|
|
if let Some(e) = &self.common.error {
|
|
|
|
html! {
|
|
|
|
<div class="alert alert-danger mt-3 mb-3">
|
|
|
|
{e.to_string() }
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
} else { html! {} }
|
|
|
|
}
|
2021-10-12 03:02:20 +00:00
|
|
|
<form
|
|
|
|
class="form">
|
|
|
|
{if !is_admin { html! {
|
|
|
|
<div class="form-group row">
|
|
|
|
<label for="old_password"
|
|
|
|
class="form-label col-sm-2 col-form-label">
|
|
|
|
{"Current password*:"}
|
|
|
|
</label>
|
|
|
|
<div class="col-sm-10">
|
|
|
|
<Field
|
|
|
|
form=&self.form
|
|
|
|
field_name="old_password"
|
|
|
|
class="form-control"
|
|
|
|
class_invalid="is-invalid has-error"
|
|
|
|
class_valid="has-success"
|
|
|
|
autocomplete="current-password"
|
2021-10-31 12:19:54 +00:00
|
|
|
oninput=self.common.callback(|_| Msg::FormUpdate) />
|
2021-10-12 03:02:20 +00:00
|
|
|
<div class="invalid-feedback">
|
|
|
|
{&self.form.field_message("old_password")}
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-09-12 09:54:47 +00:00
|
|
|
</div>
|
2021-10-12 03:02:20 +00:00
|
|
|
}} else { html! {} }}
|
2022-11-03 14:40:02 +00:00
|
|
|
<div class="form-group row mb-3">
|
2021-10-12 03:02:20 +00:00
|
|
|
<label for="new_password"
|
|
|
|
class="form-label col-sm-2 col-form-label">
|
2022-11-03 14:40:02 +00:00
|
|
|
{"New Password"}
|
|
|
|
<span class="text-danger">{"*"}</span>
|
|
|
|
{":"}
|
2021-10-12 03:02:20 +00:00
|
|
|
</label>
|
|
|
|
<div class="col-sm-10">
|
|
|
|
<Field
|
|
|
|
form=&self.form
|
|
|
|
field_name="password"
|
2022-07-26 09:07:44 +00:00
|
|
|
input_type="password"
|
2021-10-12 03:02:20 +00:00
|
|
|
class="form-control"
|
|
|
|
class_invalid="is-invalid has-error"
|
|
|
|
class_valid="has-success"
|
|
|
|
autocomplete="new-password"
|
2021-10-31 12:19:54 +00:00
|
|
|
oninput=self.common.callback(|_| Msg::FormUpdate) />
|
2021-10-12 03:02:20 +00:00
|
|
|
<div class="invalid-feedback">
|
|
|
|
{&self.form.field_message("password")}
|
|
|
|
</div>
|
2021-09-12 09:54:47 +00:00
|
|
|
</div>
|
2021-10-12 03:02:20 +00:00
|
|
|
</div>
|
2022-11-03 14:40:02 +00:00
|
|
|
<div class="form-group row mb-3">
|
2021-10-12 03:02:20 +00:00
|
|
|
<label for="confirm_password"
|
|
|
|
class="form-label col-sm-2 col-form-label">
|
2022-11-03 14:40:02 +00:00
|
|
|
{"Confirm Password"}
|
|
|
|
<span class="text-danger">{"*"}</span>
|
|
|
|
{":"}
|
2021-10-12 03:02:20 +00:00
|
|
|
</label>
|
|
|
|
<div class="col-sm-10">
|
|
|
|
<Field
|
|
|
|
form=&self.form
|
|
|
|
field_name="confirm_password"
|
2022-07-26 09:07:44 +00:00
|
|
|
input_type="password"
|
2021-10-12 03:02:20 +00:00
|
|
|
class="form-control"
|
|
|
|
class_invalid="is-invalid has-error"
|
|
|
|
class_valid="has-success"
|
|
|
|
autocomplete="new-password"
|
2021-10-31 12:19:54 +00:00
|
|
|
oninput=self.common.callback(|_| Msg::FormUpdate) />
|
2021-10-12 03:02:20 +00:00
|
|
|
<div class="invalid-feedback">
|
|
|
|
{&self.form.field_message("confirm_password")}
|
|
|
|
</div>
|
2021-09-12 09:54:47 +00:00
|
|
|
</div>
|
2021-10-12 03:02:20 +00:00
|
|
|
</div>
|
2022-11-03 14:40:02 +00:00
|
|
|
<div class="form-group row justify-content-center">
|
2021-10-12 03:02:20 +00:00
|
|
|
<button
|
2022-11-03 14:40:02 +00:00
|
|
|
class="btn btn-primary col-auto col-form-label"
|
2021-10-12 03:02:20 +00:00
|
|
|
type="submit"
|
2021-10-31 12:19:54 +00:00
|
|
|
disabled=self.common.is_task_running()
|
|
|
|
onclick=self.common.callback(|e: MouseEvent| {e.prevent_default(); Msg::Submit})>
|
2022-11-03 14:40:02 +00:00
|
|
|
<i class="bi-save me-2"></i>
|
|
|
|
{"Save changes"}
|
2021-10-12 03:02:20 +00:00
|
|
|
</button>
|
2022-11-03 14:40:02 +00:00
|
|
|
<NavButton
|
|
|
|
classes="btn btn-secondary ms-2 col-auto col-form-label"
|
|
|
|
route=AppRoute::UserDetails(self.common.username.clone())>
|
|
|
|
<i class="bi-arrow-return-left me-2"></i>
|
|
|
|
{"Back"}
|
|
|
|
</NavButton>
|
2021-10-12 03:02:20 +00:00
|
|
|
</div>
|
2021-09-12 09:54:47 +00:00
|
|
|
</form>
|
2021-10-12 03:02:20 +00:00
|
|
|
</>
|
2021-09-12 09:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|