kanidm/server/web_ui/src/credential/totpremove.rs
James Hodgkinson cc1cc691f3
Started chasing noise, found some code to delete... ()
logging changes:

* Offering auth mechanisms -> debug
* 404's aren't really warnings
* double tombstone message, one goes to debug

other changes:

* CSP changes to allow the bootstrap images to load
* more testing javascriptfile things, I R 
* it's nice to know where things are
* putting non-rust web things in static/ instead of src/
* RequestCredentials::SameOrigin is the default, also adding a utility function to save dupe code. Wow this saved... kilobytes.
* removing commented code, fixing up codespell config
* clippyisms
* wtf, gha
* dee-gloo-ing some things
* adding some ubuntu build test things
* sigh rustwasm/wasm-pack/issues/1138
* more do_request things
* packaging things
* hilarious dev env setup script
* updated script works, all the UI works, including the experimental UI for naughty crabs
* deb package fixes
* fixed some notes
* setup experimental UI tweaks
2023-06-27 11:38:22 +10:00

129 lines
3.5 KiB
Rust

use super::reset::{EventBusMsg, TotpRemoveProps};
#[cfg(debug_assertions)]
use gloo::console;
use kanidm_proto::v1::{CURequest, CUSessionToken, CUStatus};
use wasm_bindgen::{JsValue, UnwrapThrowExt};
use crate::do_request;
use crate::error::*;
use crate::RequestMethod;
use yew::prelude::*;
pub enum Msg {
Submit,
Success,
Error { emsg: String, kopid: Option<String> },
}
impl From<FetchError> for Msg {
fn from(fe: FetchError) -> Self {
Msg::Error {
emsg: fe.as_string(),
kopid: None,
}
}
}
pub struct TotpRemoveComp {
enabled: bool,
}
impl Component for TotpRemoveComp {
type Message = Msg;
type Properties = TotpRemoveProps;
fn create(_ctx: &Context<Self>) -> Self {
#[cfg(debug_assertions)]
console::debug!("totp remove::create");
TotpRemoveComp { enabled: true }
}
fn changed(&mut self, _ctx: &Context<Self>, _old_props: &Self::Properties) -> bool {
#[cfg(debug_assertions)]
console::debug!("totp remove::change");
false
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
#[cfg(debug_assertions)]
console::debug!("totp remove::update");
let cb = ctx.props().cb.clone();
match msg {
Msg::Submit => {
let token_c = ctx.props().token.clone();
let label = ctx.props().label.clone();
ctx.link().send_future(async {
match Self::submit_totp_update(token_c, CURequest::TotpRemove(label), cb).await
{
Ok(v) => v,
Err(v) => v.into(),
}
});
self.enabled = false;
}
Msg::Success => {
// Do nothing, very well.
}
Msg::Error { emsg, kopid } => {
// Submit the error to the parent.
cb.emit(EventBusMsg::Error { emsg, kopid });
}
}
true
}
fn view(&self, ctx: &Context<Self>) -> Html {
let label = ctx.props().label.clone();
let submit_enabled = self.enabled;
html! {
<div class="row mb-3">
<div class="col">{ label }</div>
<div class="col">
<button type="button" class="btn btn-dark btn-sml"
disabled={ !submit_enabled }
onclick={
ctx.link()
.callback(move |_| Msg::Submit)
}
>
{ "Remove TOTP" }
</button>
</div>
</div>
}
}
}
impl TotpRemoveComp {
async fn submit_totp_update(
token: CUSessionToken,
req: CURequest,
cb: Callback<EventBusMsg>,
) -> Result<Msg, FetchError> {
let req_jsvalue = serde_json::to_string(&(req, token))
.map(|s| JsValue::from(&s))
.expect_throw("Failed to serialise pw curequest");
let (kopid, status, value, _) = do_request(
"/v1/credential/_update",
RequestMethod::POST,
Some(req_jsvalue),
)
.await?;
if status == 200 {
let status: CUStatus =
serde_wasm_bindgen::from_value(value).expect_throw("Invalid response type");
cb.emit(EventBusMsg::UpdateStatus { status });
Ok(Msg::Success)
} else {
let emsg = value.as_string().unwrap_or_default();
Ok(Msg::Error { emsg, kopid })
}
}
}