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
use super::reset::{EventBusMsg, TotpRemoveProps};
#[cfg(debug_assertions)]
use gloo::console;
use kanidm_proto::v1::{CURequest, CUSessionToken, CUStatus};
use wasm_bindgen::{JsCast, JsValue, UnwrapThrowExt};
use wasm_bindgen_futures::JsFuture;
use web_sys::{Request, RequestInit, RequestMode, Response};
use crate::error::*;
use crate::utils;
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 => {
}
Msg::Error { emsg, kopid } => {
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 mut opts = RequestInit::new();
opts.method("POST");
opts.mode(RequestMode::SameOrigin);
opts.body(Some(&req_jsvalue));
let request = Request::new_with_str_and_init("/v1/credential/_update", &opts)?;
request
.headers()
.set("content-type", "application/json")
.expect_throw("failed to set header");
let window = utils::window();
let resp_value = JsFuture::from(window.fetch_with_request(&request)).await?;
let resp: Response = resp_value.dyn_into().expect_throw("Invalid response type");
let status = resp.status();
let headers = resp.headers();
let kopid = headers.get("x-kanidm-opid").ok().flatten();
if status == 200 {
let jsval = JsFuture::from(resp.json()?).await?;
let status: CUStatus =
serde_wasm_bindgen::from_value(jsval).expect_throw("Invalid response type");
cb.emit(EventBusMsg::UpdateStatus { status });
Ok(Msg::Success)
} else {
let text = JsFuture::from(resp.text()?).await?;
let emsg = text.as_string().unwrap_or_default();
Ok(Msg::Error { emsg, kopid })
}
}
}