From f56e9c14b2727c3b01ec103c24e0f108f4c1d339 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 9 Apr 2024 18:56:12 +0200 Subject: [PATCH] Can get the list of tokens --- virtweb_backend/src/api_tokens.rs | 15 +++++++++++++++ .../src/controllers/api_tokens_controller.rs | 11 +++++++++++ virtweb_backend/src/main.rs | 4 ++-- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/virtweb_backend/src/api_tokens.rs b/virtweb_backend/src/api_tokens.rs index 45af62e..6e91d0c 100644 --- a/virtweb_backend/src/api_tokens.rs +++ b/virtweb_backend/src/api_tokens.rs @@ -5,6 +5,7 @@ use crate::constants; use crate::utils::jwt_utils; use crate::utils::jwt_utils::{TokenPrivKey, TokenPubKey}; use crate::utils::time_utils::time; +use std::path::Path; #[derive(serde::Serialize, serde::Deserialize, Clone, Copy, Debug)] pub struct TokenID(pub uuid::Uuid); @@ -28,6 +29,11 @@ impl Token { pub fn to_json(&self) -> String { serde_json::to_string(self).expect("Failed to serialize an API token!") } + + /// Load token information from a file + pub fn load_from_file(path: &Path) -> anyhow::Result { + Ok(serde_json::from_str(&std::fs::read_to_string(path)?)?) + } } #[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Copy, Eq, PartialEq)] @@ -114,3 +120,12 @@ pub async fn create(t: &NewToken) -> anyhow::Result<(Token, TokenPrivKey)> { Ok((token, priv_key)) } + +/// Get the entire list of api toksn +pub async fn full_list() -> anyhow::Result> { + let mut list = Vec::new(); + for f in std::fs::read_dir(AppConfig::get().api_tokens_path())? { + list.push(Token::load_from_file(&f?.path())?); + } + Ok(list) +} diff --git a/virtweb_backend/src/controllers/api_tokens_controller.rs b/virtweb_backend/src/controllers/api_tokens_controller.rs index e36a6b7..12da9f6 100644 --- a/virtweb_backend/src/controllers/api_tokens_controller.rs +++ b/virtweb_backend/src/controllers/api_tokens_controller.rs @@ -47,3 +47,14 @@ pub async fn create(new_token: web::Json) -> HttpResult { priv_key, })) } + +/// Get the list of API tokens +pub async fn list() -> HttpResult { + let list = api_tokens::full_list() + .await? + .into_iter() + .map(RestToken::new) + .collect::>(); + + Ok(HttpResponse::Ok().json(list)) +} diff --git a/virtweb_backend/src/main.rs b/virtweb_backend/src/main.rs index c2db550..7b84199 100644 --- a/virtweb_backend/src/main.rs +++ b/virtweb_backend/src/main.rs @@ -282,11 +282,11 @@ async fn main() -> std::io::Result<()> { "/api/tokens/create", web::post().to(api_tokens_controller::create), ) - /* TODO .route( + .route( "/api/tokens/list", web::get().to(api_tokens_controller::list), ) - .route( + /* TODO .route( "/api/tokens/{uid}", web::get().to(api_tokens_controller::get_single), )