store token

This commit is contained in:
grimhilt 2023-10-24 15:32:51 +02:00
parent c6cf8a9730
commit f4a905c57f
3 changed files with 68 additions and 4 deletions

View File

@ -35,7 +35,6 @@ impl ApiCall for Login {
}, },
None => "/ocs/v2.php/core/getapppassword".to_owned(), None => "/ocs/v2.php/core/getapppassword".to_owned(),
}; };
dbg!(url.clone());
self.api_builder.set_url(Method::GET, &url); self.api_builder.set_url(Method::GET, &url);
self.api_builder.set_header("OCS-APIRequest", HeaderValue::from_str("true").unwrap()); self.api_builder.set_header("OCS-APIRequest", HeaderValue::from_str("true").unwrap());
self.api_builder.set_basic_auth(self.login.clone(), self.password.clone()); self.api_builder.set_basic_auth(self.login.clone(), self.password.clone());
@ -88,8 +87,8 @@ impl Login {
return text.clone(); return text.clone();
} }
} }
Ok(XmlEvent::EndElement { name, .. }) => { //Ok(XmlEvent::EndElement { name, .. }) => {
} //}
Err(e) => { Err(e) => {
eprintln!("err: parsing xml: {}", e); eprintln!("err: parsing xml: {}", e);
break; break;

View File

@ -3,6 +3,7 @@ use std::sync::Mutex;
use crate::services::login::Login; use crate::services::login::Login;
use crate::commands::config; use crate::commands::config;
use crate::store::gconfig;
use crate::commands::clone::get_url_props; use crate::commands::clone::get_url_props;
use crate::services::api_call::ApiCall; use crate::services::api_call::ApiCall;
@ -55,14 +56,24 @@ impl RequestManager {
pub fn get_token(&mut self) -> String { pub fn get_token(&mut self) -> String {
if self.token.is_none() { if self.token.is_none() {
// todo check in config if let Some(token) = gconfig::read_token() {
if !token.is_empty() {
self.token = Some(token);
return self.token.clone().unwrap();
}
}
let get_token = Login::new() let get_token = Login::new()
.ask_auth() .ask_auth()
.set_host(Some(self.get_host())) .set_host(Some(self.get_host()))
.send_login(); .send_login();
// todo deal with error cases // todo deal with error cases
self.token = Some(get_token.unwrap()); self.token = Some(get_token.unwrap());
if let Err(err) = gconfig::write_token(&self.token.clone().unwrap()) {
eprintln!("err: failed to write token ({})", err);
}
} }
self.token.clone().unwrap() self.token.clone().unwrap()
} }

54
src/store/gconfig.rs Normal file
View File

@ -0,0 +1,54 @@
use std::env;
use std::path::PathBuf;
use std::fs::{self, OpenOptions};
use std::io::{self, Write};
use crate::utils::read;
fn global_path() -> Option<PathBuf> {
if let Some(home_dir) = env::var_os("HOME") {
let mut path = PathBuf::new();
path.push(home_dir);
path.push(".nextsync");
Some(path)
}
else
{
None
}
}
pub fn write_token(token: &str) -> io::Result<()> {
if let Some(mut path_token) = global_path() {
if !path_token.exists() {
fs::create_dir_all(path_token.clone())?;
}
path_token.push("token");
let mut file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(path_token)?;
writeln!(file, "{}", token)?;
}
Ok(())
}
pub fn read_token() -> Option<String> {
if let Some(mut path_token) = global_path() {
if !path_token.exists() {
return None;
}
path_token.push("token");
if let Ok(lines) = read::read_lines(path_token) {
for line in lines {
if let Ok(l) = line {
return Some(l);
}
}
}
}
None
}