feat: implemented access token storing
This commit is contained in:
parent
f965a4ddd9
commit
5f70219eac
|
@ -230,6 +230,27 @@ dependencies = [
|
|||
"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]]
|
||||
name = "encoding_rs"
|
||||
version = "0.8.33"
|
||||
|
@ -335,6 +356,17 @@ dependencies = [
|
|||
"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]]
|
||||
name = "gimli"
|
||||
version = "0.28.0"
|
||||
|
@ -582,6 +614,7 @@ dependencies = [
|
|||
"anyhow",
|
||||
"clap",
|
||||
"crossterm",
|
||||
"directories",
|
||||
"reqwest",
|
||||
"serde",
|
||||
"serde_json",
|
||||
|
@ -659,6 +692,12 @@ dependencies = [
|
|||
"vcpkg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "option-ext"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot"
|
||||
version = "0.12.1"
|
||||
|
@ -677,7 +716,7 @@ checksum = "93f00c865fe7cabf650081affecd3871070f26767e7b2070a3ffae14c654b447"
|
|||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"redox_syscall",
|
||||
"redox_syscall 0.3.5",
|
||||
"smallvec",
|
||||
"windows-targets",
|
||||
]
|
||||
|
@ -724,6 +763,15 @@ dependencies = [
|
|||
"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]]
|
||||
name = "redox_syscall"
|
||||
version = "0.3.5"
|
||||
|
@ -733,6 +781,17 @@ dependencies = [
|
|||
"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]]
|
||||
name = "reqwest"
|
||||
version = "0.11.20"
|
||||
|
@ -966,11 +1025,31 @@ checksum = "cb94d2f3cc536af71caac6b6fcebf65860b347e7ce0cc9ebe8f70d3e521054ef"
|
|||
dependencies = [
|
||||
"cfg-if",
|
||||
"fastrand",
|
||||
"redox_syscall",
|
||||
"redox_syscall 0.3.5",
|
||||
"rustix",
|
||||
"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]]
|
||||
name = "tinyvec"
|
||||
version = "1.6.0"
|
||||
|
|
|
@ -15,4 +15,4 @@ reqwest = { version = "0.11.20", features = ["json"] }
|
|||
clap = { version = "4.4.2", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
serde = { version = "1.0.183", features = ["derive"] }
|
||||
|
||||
directories = "5.0.1"
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use anyhow::{Error, Ok, Result};
|
||||
use directories::ProjectDirs;
|
||||
use reqwest::{Client, StatusCode};
|
||||
use serde::Deserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
|
@ -22,7 +23,7 @@ struct UserInfoResponse {
|
|||
is_admin: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct Api {
|
||||
token: String,
|
||||
base_url: String,
|
||||
|
@ -134,4 +135,17 @@ impl Api {
|
|||
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(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ mod state;
|
|||
mod ui;
|
||||
|
||||
use anyhow::Result;
|
||||
use api::Api;
|
||||
use clap::Parser;
|
||||
|
||||
/// A tui for administrating nerdcult.net
|
||||
|
@ -25,7 +26,9 @@ async fn main() -> Result<()> {
|
|||
let stdout = ui::prepare()?;
|
||||
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 api = login.run().await?;
|
||||
api
|
||||
|
|
|
@ -126,7 +126,10 @@ impl UI<'_> {
|
|||
let password = self.password_data.lines()[0].clone();
|
||||
let api = Api::from_creds(&self.base_url, username, password).await?;
|
||||
match api {
|
||||
Ok(a) => return Ok(a),
|
||||
Ok(a) => {
|
||||
a.save()?;
|
||||
return Ok(a);
|
||||
}
|
||||
Err(e) => self.status = e.to_string(),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue