From 9dacfecac7909988539cfebfbbf46aa5b1667b92 Mon Sep 17 00:00:00 2001 From: Septias <39526136+Septias@users.noreply.github.com> Date: Wed, 24 Mar 2021 11:40:26 +0100 Subject: [PATCH] add and configure credentials endpoint --- Backend/src/main.rs | 2 +- Backend/src/server.rs | 34 ++++++++++++++++++++---- Frontend/src/components/Ilias.vue | 44 ++++++++++++++++++++++++++----- 3 files changed, 67 insertions(+), 13 deletions(-) diff --git a/Backend/src/main.rs b/Backend/src/main.rs index 188df97..76e8dd1 100644 --- a/Backend/src/main.rs +++ b/Backend/src/main.rs @@ -37,7 +37,7 @@ async fn main() -> Result<(), Box> { "/assets/", StaticFiles::from("C:/dev/repositories/BettIlias/Frontend/dist/assets"), ) - .mount("/", routes![server::get_node, server::index, server::open_file, server::update]) + .mount("/", routes![server::get_node, server::index, server::open_file, server::update, server::set_credentials]) .manage(ilias) .launch().await?; diff --git a/Backend/src/server.rs b/Backend/src/server.rs index 7091068..1bc8ede 100644 --- a/Backend/src/server.rs +++ b/Backend/src/server.rs @@ -2,9 +2,10 @@ use std::{ path::PathBuf, sync::Arc, }; -use rocket::{response::NamedFile, State}; +use rocket::{State, response::NamedFile}; use rocket_contrib::json::{json, Json, JsonValue}; -use tokio::fs::{read_to_string}; +use serde::Deserialize; +use tokio::fs; use crate::tree::{ILiasTree, IlNode}; use crate::client::ClientError; @@ -21,9 +22,6 @@ pub async fn update(node: State<'_, Arc>) -> JsonValue { match err { ClientError::NoToken => { - if let Ok(token) = read_to_string("token.txt").await { - node.set_client_token(&token); - } return json!({"status": "set_token"}) } _ => {return json!({"status": format!("{:?}",err)})} @@ -44,3 +42,29 @@ pub fn open_file(file: PathBuf) -> std::result::Result<(), std::io::Error> { open::that(file)?; Ok(()) } + +#[derive(Deserialize)] +pub struct Credentials { + username: String, + password: String, + persistent: bool +} + +#[post("/api/credentials", data= "")] +pub async fn set_credentials(credentials: Json, node: State<'_, Arc>) -> JsonValue { + let creds = [credentials.username.to_owned(), credentials.password.to_owned()]; + let err = node.client.acquire_token(&creds).await; + + + if let Err(err) = err{ + json!({"status": format!("{}",err)}) + } else { + if credentials.persistent { + if fs::write("credentials.txt", creds.join("\n")).await.is_err() { + error!("couldn't write credentials to file"); + }; + } + + json!({"status": "ok"}) + } +} \ No newline at end of file diff --git a/Frontend/src/components/Ilias.vue b/Frontend/src/components/Ilias.vue index a7f3380..0e9a439 100644 --- a/Frontend/src/components/Ilias.vue +++ b/Frontend/src/components/Ilias.vue @@ -52,7 +52,7 @@
+

{{ wrong }}

@@ -69,9 +70,14 @@ class="block w-full" type="password" /> - +

Angemeldet bleiben

-
@@ -147,19 +153,40 @@ export default defineComponent({ const username = ref(""); const password = ref(""); - const persistent = ref(""); + const persistent = ref(false); + const wrong = ref(""); + const requesting = ref(false); const send_credentials = () => { + requesting.value = true; + wrong.value = ""; axios .post("api/credentials", { username: username.value, password: password.value, - persistent, + persistent: persistent.value, }) .then((resp) => { - console.log(resp); + requesting.value = false; + console.log(resp.data.status); + if (resp.data.status == "ok") { + wrong.value = ""; + login.value = false; + } else { + wrong.value = resp.data.status; + } + }) + .catch((err) => { + console.err(err); + requesting.value = false; }); - login.value = false; }; + + const disable_login = () => { + if (!requesting.value) { + login.value = !login.value; + } + }; + return { root_node, handle_set_inivisible, @@ -172,6 +199,9 @@ export default defineComponent({ password, send_credentials, persistent, + wrong, + requesting, + disable_login, }; }, });