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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#[cfg(debug_assertions)]
use gloo::console;
use kanidm_proto::v1::{CURegState, CURequest, CUSessionToken, CUStatus};
use uuid::Uuid;
use wasm_bindgen::{JsCast, JsValue, UnwrapThrowExt};
use wasm_bindgen_futures::JsFuture;
use web_sys::{Request, RequestInit, RequestMode, Response};
use yew::prelude::*;
use super::reset::{EventBusMsg, PasskeyRemoveModalProps};
use crate::error::*;
use crate::utils;
pub struct PasskeyRemoveModalApp {
state: State,
target: String,
tag: String,
uuid: Uuid,
}
pub enum State {
Init,
Submitting,
}
pub enum Msg {
Cancel,
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,
}
}
}
impl PasskeyRemoveModalApp {
pub fn render_button(tag: &str, uuid: Uuid) -> Html {
let remove_tgt = format!("#staticPasskeyRemove-{}", uuid);
let tag = tag.to_string();
html! {
<div class="row mb-3">
<div class="col">{ tag.clone() }</div>
<div class="col">
<button type="button" class="btn btn-dark btn-sml" id={tag} data-bs-toggle="modal" data-bs-target={ remove_tgt }>
{ "Remove" }
</button>
</div>
</div>
}
}
fn reset_and_hide(&mut self) {
utils::modal_hide_by_id(&self.target);
self.state = State::Init;
}
async fn submit_passkey_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: status.clone(),
});
Ok(match status.mfaregstate {
CURegState::TotpCheck(_)
| CURegState::TotpTryAgain
| CURegState::TotpInvalidSha1
| CURegState::Passkey(_)
| CURegState::BackupCodes(_) => Msg::Error {
emsg: "Invalid Passkey reg state response".to_string(),
kopid,
},
CURegState::None => Msg::Success,
})
} else {
let text = JsFuture::from(resp.text()?).await?;
let emsg = text.as_string().unwrap_or_default();
Ok(Msg::Error { emsg, kopid })
}
}
}
impl Component for PasskeyRemoveModalApp {
type Message = Msg;
type Properties = PasskeyRemoveModalProps;
fn create(ctx: &Context<Self>) -> Self {
#[cfg(debug_assertions)]
console::debug!("passkey remove modal create");
let tag = ctx.props().tag.clone();
let uuid = ctx.props().uuid;
let target = format!("staticPasskeyRemove-{}", uuid);
PasskeyRemoveModalApp {
state: State::Init,
tag,
uuid,
target,
}
}
fn changed(&mut self, _ctx: &Context<Self>, _props: &Self::Properties) -> bool {
#[cfg(debug_assertions)]
console::debug!("passkey remove modal::change");
false
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
#[cfg(debug_assertions)]
console::debug!("passkey remove modal::update");
let cb = ctx.props().cb.clone();
match msg {
Msg::Submit => {
self.reset_and_hide();
let token_c = ctx.props().token.clone();
let uuid = self.uuid;
ctx.link().send_future(async move {
match Self::submit_passkey_update(token_c, CURequest::PasskeyRemove(uuid), cb)
.await
{
Ok(v) => v,
Err(v) => v.into(),
}
});
self.state = State::Submitting;
}
Msg::Success | Msg::Cancel => {
self.reset_and_hide();
}
Msg::Error { emsg, kopid } => {
cb.emit(EventBusMsg::Error { emsg, kopid });
self.reset_and_hide();
}
}
true
}
fn rendered(&mut self, _ctx: &Context<Self>, _first_render: bool) {
#[cfg(debug_assertions)]
console::debug!("passkey remove modal::rendered");
}
fn destroy(&mut self, _ctx: &Context<Self>) {
#[cfg(debug_assertions)]
console::debug!("passkey remove modal::destroy");
}
fn view(&self, ctx: &Context<Self>) -> Html {
#[cfg(debug_assertions)]
console::debug!("passkey remove modal::view");
let remove_tgt = self.target.clone();
let remove_id = format!("staticPasskeyRemove-{}", self.uuid);
let remove_label = format!("staticPasskeyRemoveLabel-{}", self.uuid);
let msg = format!("Delete the Passkey named '{}'?", self.tag);
let submit_enabled = matches!(self.state, State::Init);
html! {
<div class="modal fade" id={ remove_id } data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby={ remove_tgt } aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id={ remove_label }>{ "Delete Passkey" }</h5>
<button type="button" class="btn-close" aria-label="Close"
onclick={
ctx.link()
.callback(move |_| {
Msg::Cancel
})
}
></button>
</div>
<div class="modal-body">
<p>{ msg }</p>
</div>
<div class="modal-footer">
<button id="delete-cancel" type="button" class="btn btn-secondary"
onclick={
ctx.link()
.callback(move |_| {
Msg::Cancel
})
}
>{ "Cancel" }</button>
<button id="delete-submit" type="button" class="btn btn-danger"
disabled={ !submit_enabled }
onclick={
ctx.link()
.callback(move |_| {
Msg::Submit
})
}
>{ "Submit" }</button>
</div>
</div>
</div>
</div>
}
}
}