feat: implemented access token storing

This commit is contained in:
antifallobst 2023-09-13 15:55:40 +02:00
parent f965a4ddd9
commit 5f70219eac
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
5 changed files with 106 additions and 7 deletions

83
Cargo.lock generated
View File

@ -230,6 +230,27 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "directories"
version = "5.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a49173b84e034382284f27f1af4dcbbd231ffa358c0fe316541a7337f376a35"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs-sys"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c"
dependencies = [
"libc",
"option-ext",
"redox_users",
"windows-sys",
]
[[package]] [[package]]
name = "encoding_rs" name = "encoding_rs"
version = "0.8.33" version = "0.8.33"
@ -335,6 +356,17 @@ dependencies = [
"pin-utils", "pin-utils",
] ]
[[package]]
name = "getrandom"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]] [[package]]
name = "gimli" name = "gimli"
version = "0.28.0" version = "0.28.0"
@ -582,6 +614,7 @@ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
"crossterm", "crossterm",
"directories",
"reqwest", "reqwest",
"serde", "serde",
"serde_json", "serde_json",
@ -659,6 +692,12 @@ dependencies = [
"vcpkg", "vcpkg",
] ]
[[package]]
name = "option-ext"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
[[package]] [[package]]
name = "parking_lot" name = "parking_lot"
version = "0.12.1" version = "0.12.1"
@ -677,7 +716,7 @@ checksum = "93f00c865fe7cabf650081affecd3871070f26767e7b2070a3ffae14c654b447"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"libc", "libc",
"redox_syscall", "redox_syscall 0.3.5",
"smallvec", "smallvec",
"windows-targets", "windows-targets",
] ]
@ -724,6 +763,15 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "redox_syscall"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
dependencies = [
"bitflags 1.3.2",
]
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.3.5" version = "0.3.5"
@ -733,6 +781,17 @@ dependencies = [
"bitflags 1.3.2", "bitflags 1.3.2",
] ]
[[package]]
name = "redox_users"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b"
dependencies = [
"getrandom",
"redox_syscall 0.2.16",
"thiserror",
]
[[package]] [[package]]
name = "reqwest" name = "reqwest"
version = "0.11.20" version = "0.11.20"
@ -966,11 +1025,31 @@ checksum = "cb94d2f3cc536af71caac6b6fcebf65860b347e7ce0cc9ebe8f70d3e521054ef"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"fastrand", "fastrand",
"redox_syscall", "redox_syscall 0.3.5",
"rustix", "rustix",
"windows-sys", "windows-sys",
] ]
[[package]]
name = "thiserror"
version = "1.0.48"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d6d7a740b8a666a7e828dd00da9c0dc290dff53154ea77ac109281de90589b7"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.48"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49922ecae66cc8a249b77e68d1d0623c1b2c514f0060c27cdc68bd62a1219d35"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "tinyvec" name = "tinyvec"
version = "1.6.0" version = "1.6.0"

View File

@ -15,4 +15,4 @@ reqwest = { version = "0.11.20", features = ["json"] }
clap = { version = "4.4.2", features = ["derive"] } clap = { version = "4.4.2", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
serde = { version = "1.0.183", features = ["derive"] } serde = { version = "1.0.183", features = ["derive"] }
directories = "5.0.1"

View File

@ -1,6 +1,7 @@
use anyhow::{Error, Ok, Result}; use anyhow::{Error, Ok, Result};
use directories::ProjectDirs;
use reqwest::{Client, StatusCode}; use reqwest::{Client, StatusCode};
use serde::Deserialize; use serde::{Deserialize, Serialize};
use serde_json::json; use serde_json::json;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -22,7 +23,7 @@ struct UserInfoResponse {
is_admin: bool, is_admin: bool,
} }
#[derive(Debug)] #[derive(Debug, Deserialize, Serialize)]
pub struct Api { pub struct Api {
token: String, token: String,
base_url: String, base_url: String,
@ -134,4 +135,17 @@ impl Api {
base_url: base_url.to_string(), base_url: base_url.to_string(),
})) }))
} }
pub fn save(&self) -> Result<()> {
let serialized = serde_json::to_string(&self)?;
let project_dirs = match ProjectDirs::from("lol", "NerdCult", "ncadmin") {
Some(pd) => Ok(pd),
None => Err(Error::msg("Failed to get data dir")),
}?;
std::fs::create_dir_all(project_dirs.data_dir())?;
std::fs::write(project_dirs.data_dir().join("data.json"), serialized)?;
Ok(())
}
} }

View File

@ -3,6 +3,7 @@ mod state;
mod ui; mod ui;
use anyhow::Result; use anyhow::Result;
use api::Api;
use clap::Parser; use clap::Parser;
/// A tui for administrating nerdcult.net /// A tui for administrating nerdcult.net
@ -25,7 +26,9 @@ async fn main() -> Result<()> {
let stdout = ui::prepare()?; let stdout = ui::prepare()?;
let base_url = args.base_url.unwrap(); let base_url = args.base_url.unwrap();
let api = { let api = if let Some(token) = args.token {
Api::from_token(&base_url, token).await??
} else {
let mut login = ui::login::UI::new(stdout, base_url)?; let mut login = ui::login::UI::new(stdout, base_url)?;
let api = login.run().await?; let api = login.run().await?;
api api

View File

@ -126,7 +126,10 @@ impl UI<'_> {
let password = self.password_data.lines()[0].clone(); let password = self.password_data.lines()[0].clone();
let api = Api::from_creds(&self.base_url, username, password).await?; let api = Api::from_creds(&self.base_url, username, password).await?;
match api { match api {
Ok(a) => return Ok(a), Ok(a) => {
a.save()?;
return Ok(a);
}
Err(e) => self.status = e.to_string(), Err(e) => self.status = e.to_string(),
} }
} }