|
@@ -0,0 +1,78 @@
|
|
|
+use tide::http::mime;
|
|
|
+use microrm::prelude::*;
|
|
|
+
|
|
|
+use crate::{schema, UIDCError};
|
|
|
+
|
|
|
+type Request = tide::Request<super::ServerStateWrapper>;
|
|
|
+
|
|
|
+fn generate_template_data(qi: µrm::QueryInterface, realm: schema::RealmID, user: schema::UserID) -> Result<serde_json::Value, UIDCError> {
|
|
|
+ let user = qi.get().by_id(&user).one()?.ok_or(UIDCError::Abort("couldn't find user"))?;
|
|
|
+
|
|
|
+ let has_totp = qi.get().by(schema::AuthChallenge::User, &user.id()).by(schema::AuthChallenge::ChallengeType, &schema::AuthChallengeType::TOTP).one()?.is_some();
|
|
|
+
|
|
|
+ let template_data = serde_json::json!({
|
|
|
+ "username": user.username,
|
|
|
+ "totp_control": if has_totp {
|
|
|
+ serde_json::json!([{ "value": "keep", "text": "Keep as-is"}, { "value": "remove", "text": "Remove" }, { "value": "reset", "text": "Reset" }])
|
|
|
+ } else {
|
|
|
+ serde_json::json!([{ "value": "keep", "text": "Leave disabled" }, { "value": "reset", "text": "Start setup" }])
|
|
|
+ },
|
|
|
+ });
|
|
|
+
|
|
|
+ Ok(template_data)
|
|
|
+}
|
|
|
+
|
|
|
+async fn um_index(mut req: Request) -> tide::Result<tide::Response> {
|
|
|
+ let shelper = super::session::SessionHelper::new(&req);
|
|
|
+
|
|
|
+ let (realm, user) = match shelper.verify_session(&req) {
|
|
|
+ Some(v) => v,
|
|
|
+ None => {
|
|
|
+ return Ok(tide::Redirect::temporary(format!("../v1/session/login?redirect={}", req.url())).into())
|
|
|
+ },
|
|
|
+ };
|
|
|
+ let qi = req.state().core.pool.query_interface();
|
|
|
+
|
|
|
+ // template_data.as_object_mut().and_then(|o| o.append
|
|
|
+ let template_data = generate_template_data(qi, realm, user)?;
|
|
|
+
|
|
|
+ Ok(tide::Response::builder(200)
|
|
|
+ .content_type(mime::HTML)
|
|
|
+ .body(req.state().core.templates.render("um_index", &template_data).map_err(|_| tide::Error::from_str(500, "error rendering template"))?)
|
|
|
+ .build())
|
|
|
+}
|
|
|
+
|
|
|
+async fn um_update(mut req: Request) -> tide::Result<tide::Response> {
|
|
|
+ let shelper = super::session::SessionHelper::new(&req);
|
|
|
+
|
|
|
+ let (realm, user) = match shelper.verify_session(&req) {
|
|
|
+ Some(v) => v,
|
|
|
+ None => {
|
|
|
+ return Ok(tide::Redirect::temporary("../v1/session/login?redirect=../../um/").into())
|
|
|
+ },
|
|
|
+ };
|
|
|
+ let qi = req.state().core.pool.query_interface();
|
|
|
+
|
|
|
+ let progress : Result<Vec<String>, UIDCError> = {
|
|
|
+ let mut info_msgs = vec![];
|
|
|
+
|
|
|
+ let password_challenge = qi.get().by(schema::AuthChallenge::User, &user).by(schema::AuthChallenge::ChallengeType, &schema::AuthChallengeType::Password).one()?;
|
|
|
+
|
|
|
+ Ok(info_msgs)
|
|
|
+ };
|
|
|
+
|
|
|
+ let mut template_data = generate_template_data(qi, realm, user)?;
|
|
|
+
|
|
|
+ template_data.as_object_mut().and_then(|o| o.insert("info_msg".into(), serde_json::json!(["Update request received"])));
|
|
|
+
|
|
|
+ Ok(tide::Response::builder(200)
|
|
|
+ .content_type(mime::HTML)
|
|
|
+ .body(req.state().core.templates.render("um_index", &template_data).map_err(|_| tide::Error::from_str(500, "error rendering template"))?)
|
|
|
+ .build())
|
|
|
+}
|
|
|
+
|
|
|
+pub(super) fn um_server(mut route: tide::Route<super::ServerStateWrapper>) {
|
|
|
+ route.at("/").get(um_index);
|
|
|
+ route.at("update").post(um_update);
|
|
|
+ // route.at("/change_password").get(um_change_password).post(um_change_password_post);
|
|
|
+}
|