Skip to content

Commit

Permalink
add and configure credentials endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Septias committed Mar 24, 2021
1 parent dead835 commit 9dacfec
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
"/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?;

Expand Down
34 changes: 29 additions & 5 deletions Backend/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,9 +22,6 @@ pub async fn update(node: State<'_, Arc<ILiasTree>>) -> 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)})}
Expand All @@ -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= "<credentials>")]
pub async fn set_credentials(credentials: Json<Credentials>, node: State<'_, Arc<ILiasTree>>) -> 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"})
}
}
44 changes: 37 additions & 7 deletions Frontend/src/components/Ilias.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@
</div>
<div
class="fixed h-full w-full flex justify-center items-center top-0 bg-main bg-opacity-50"
@click="login = !login"
@click="disable_login"
v-show="login"
>
<form
class="bg-main rounded-xl border-2 border-accent p-4 custom_form text-xl"
@submit.prevent="send_credentials"
@click.stop=""
>
<p v-if="wrong" class="text-sm text-accent">{{ wrong }}</p>
<label>Benutzername</label>
<input v-model="username" autocomplete="username" class="block w-full" />
<label>Passwort</label>
Expand All @@ -69,9 +70,14 @@
class="block w-full"
type="password"
/>
<input class="mr-1" type="checkbox" />
<input class="mr-1" type="checkbox" v-model="persistent"/>
<p class="inline-block text-sm">Angemeldet bleiben</p>
<button type="submit" class="button px-2 bg-accent rounded float-right">
<button
type="submit"
class="button px-2 rounded float-right"
:class="requesting ? 'bg-gray-600' : 'bg-accent'"
:disabled="requesting"
>
Ok!
</button>
</form>
Expand Down Expand Up @@ -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,
Expand All @@ -172,6 +199,9 @@ export default defineComponent({
password,
send_credentials,
persistent,
wrong,
requesting,
disable_login,
};
},
});
Expand Down

0 comments on commit 9dacfec

Please sign in to comment.