From 2c14281ae313158af1b104ed9e7745d92fec48f4 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 27 Jan 2025 21:54:03 +0100 Subject: [PATCH] Display the list of registered clients --- Cargo.lock | 62 ++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 ++- assets/style.css | 2 +- src/constants.rs | 3 +++ src/server/web_ui.rs | 2 ++ src/user.rs | 25 +++++++++++++++--- src/utils.rs | 6 +++++ templates/index.html | 52 ++++++++++++++++++++++++++++++++++--- 8 files changed, 147 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2037862..696cfc0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -290,6 +290,21 @@ dependencies = [ "alloc-no-stdlib", ] +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anstream" version = "0.6.18" @@ -584,6 +599,20 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e36cc9d416881d2e24f9a963be5fb1cd90966419ac844274161d10488b3e825" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-targets", +] + [[package]] name = "cipher" version = "0.4.4" @@ -1314,6 +1343,29 @@ dependencies = [ "tracing", ] +[[package]] +name = "iana-time-zone" +version = "0.1.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "icu_collections" version = "1.5.0" @@ -1609,6 +1661,7 @@ dependencies = [ "actix-web", "anyhow", "askama", + "chrono", "clap", "env_logger", "ipnet", @@ -3051,6 +3104,15 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-registry" version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index 0e3494f..b8e6588 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,4 +22,5 @@ mime_guess = "2.0.5" askama = "0.12.1" urlencoding = "2.1.3" uuid = { version = "1.12.1", features = ["v4", "serde"] } -ipnet = { version = "2.11.0", features = ["serde"] } \ No newline at end of file +ipnet = { version = "2.11.0", features = ["serde"] } +chrono = "0.4.39" \ No newline at end of file diff --git a/assets/style.css b/assets/style.css index 76d5bc7..b893e25 100644 --- a/assets/style.css +++ b/assets/style.css @@ -1,5 +1,5 @@ .body-content { - max-width: 700px; + max-width: 900px; margin: 50px auto; } diff --git a/src/constants.rs b/src/constants.rs index aedb64b..2e1fcce 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -3,3 +3,6 @@ pub const STATE_KEY: &str = "oidc-state"; /// Session key for user information pub const USER_SESSION_KEY: &str = "user"; + +/// Token length +pub const TOKEN_LEN: usize = 20; diff --git a/src/server/web_ui.rs b/src/server/web_ui.rs index c781112..5e91c22 100644 --- a/src/server/web_ui.rs +++ b/src/server/web_ui.rs @@ -34,6 +34,7 @@ pub async fn static_file(path: web::Path) -> HttpResult { struct HomeTemplate { name: String, matrix_token: String, + clients: Vec, success_message: Option, error_message: Option, } @@ -122,6 +123,7 @@ pub async fn home(session: Session, form_req: Option>) -> HomeTemplate { name: user.name, matrix_token: config.obfuscated_matrix_token(), + clients: config.clients, success_message, error_message, } diff --git a/src/user.rs b/src/user.rs index 1e13529..5fc3cac 100644 --- a/src/user.rs +++ b/src/user.rs @@ -4,7 +4,8 @@ use s3::{Bucket, BucketConfiguration}; use thiserror::Error; use crate::app_config::AppConfig; -use crate::utils::{curr_time, rand_str}; +use crate::constants::TOKEN_LEN; +use crate::utils::{curr_time, format_time, rand_str}; #[derive(Error, Debug)] pub enum UserError { @@ -42,16 +43,34 @@ pub struct APIClient { /// Client secret pub secret: String, + + /// Client creation time + pub created: u64, + + /// Client last usage time + pub used: u64, +} + +impl APIClient { + pub fn fmt_created(&self) -> String { + format_time(self.created).unwrap_or_default() + } + + pub fn fmt_used(&self) -> String { + format_time(self.used).unwrap_or_default() + } } impl APIClient { /// Generate a new API client pub fn generate(description: String, network: Option) -> Self { Self { - id: Default::default(), + id: uuid::Uuid::new_v4(), description, network, - secret: rand_str(20), + secret: rand_str(TOKEN_LEN), + created: curr_time().unwrap(), + used: curr_time().unwrap(), } } } diff --git a/src/utils.rs b/src/utils.rs index 15c9799..68a919f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -12,3 +12,9 @@ pub fn curr_time() -> anyhow::Result { .duration_since(UNIX_EPOCH) .map(|t| t.as_secs())?) } + +/// Format time +pub fn format_time(time: u64) -> Option { + let time = chrono::DateTime::from_timestamp(time as i64, 0)?; + Some(time.naive_local().to_string()) +} diff --git a/templates/index.html b/templates/index.html index 3772812..ca240aa 100644 --- a/templates/index.html +++ b/templates/index.html @@ -14,7 +14,7 @@