From 83cbe020b49c31415a4ec08ceb3da278ebca0cc8 Mon Sep 17 00:00:00 2001 From: Septias <39526136+Septias@users.noreply.github.com> Date: Wed, 24 Mar 2021 11:40:39 +0100 Subject: [PATCH] implement auto-login --- Backend/src/tree.rs | 56 +++++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/Backend/src/tree.rs b/Backend/src/tree.rs index 8929d74..fc79fc2 100644 --- a/Backend/src/tree.rs +++ b/Backend/src/tree.rs @@ -3,22 +3,16 @@ use crate::{ IdSize, }; use futures::future::join_all; +use itertools::Itertools; use lazy_static::lazy_static; use log::info; use ron::de::from_bytes; use ron::ser::{to_string_pretty, PrettyConfig}; use scraper::{ElementRef, Selector}; use serde::{Deserialize, Serialize}; -use std::{ - path::PathBuf, - sync::{Arc, Mutex}, -}; -use tokio::{ - fs::File, - io::{AsyncReadExt, AsyncWriteExt}, - sync::mpsc::{self, UnboundedReceiver, UnboundedSender}, - task::{self, JoinHandle}, -}; +use std::{convert::TryInto, path::PathBuf, sync::{Arc, Mutex}}; +use tokio::{fs::{File, read_to_string}, io::{AsyncReadExt, AsyncWriteExt}, sync::mpsc::{self, UnboundedReceiver, UnboundedSender}, task::{self, JoinHandle}}; + #[derive(Debug, Deserialize, Serialize, Clone)] pub struct IlNode { @@ -71,7 +65,7 @@ lazy_static! { } pub struct ILiasTree { - client: Arc, + pub client: Arc, tree: Option>>, receiver: Mutex>>>>, sender: UnboundedSender>>, @@ -82,13 +76,44 @@ impl ILiasTree { self.tree.as_ref() } pub async fn update_ilias(&self) -> Result<(), ClientError> { - update_ilias_tree( + + let credentials = if let Err(err) = update_ilias_tree( self.client.clone(), self.get_root_node().unwrap().clone(), self.sender.clone(), ) .await - .unwrap()?; + .unwrap(){ + if let ClientError::NoToken = err { + if let Ok(raw_credenetials) = read_to_string("credentials.txt").await{ + let credentials: [String; 2] = raw_credenetials + .split("\n") + .map(|c| c.trim().to_owned()) + .collect_vec() + .try_into() + .unwrap(); + Ok(Some(credentials)) + } else { + Err(err) + } + } else { + Err(err) + } + } else { + Ok(None) + }?; + + if let Some(credentials) = credentials { + info!("Auto relogin"); + self.client.acquire_token(&credentials).await?; + update_ilias_tree( + self.client.clone(), + self.get_root_node().unwrap().clone(), + self.sender.clone(), + ) + .await + .unwrap()?; + } Ok(()) } @@ -114,6 +139,7 @@ impl ILiasTree { } } + pub async fn download_files(&self) -> Result<(), anyhow::Error> { let client = self.client.clone(); let mut receiver = self.receiver.lock().unwrap().take().unwrap(); @@ -126,10 +152,6 @@ impl ILiasTree { Ok(()) } - pub fn set_client_token(&self, token: &str) { - self.client.set_token(token); - } - pub async fn save(&self) { let pretty = PrettyConfig::new() .with_separate_tuple_members(true)