diff --git a/virtweb_backend/src/api_tokens.rs b/virtweb_backend/src/api_tokens.rs index 6e91d0c..3222c9a 100644 --- a/virtweb_backend/src/api_tokens.rs +++ b/virtweb_backend/src/api_tokens.rs @@ -17,6 +17,7 @@ pub struct Token { pub description: String, created: u64, updated: u64, + #[serde(skip_serializing_if = "TokenPubKey::is_invalid")] pub pub_key: TokenPubKey, pub rights: Vec, pub last_used: Option, @@ -121,7 +122,7 @@ pub async fn create(t: &NewToken) -> anyhow::Result<(Token, TokenPrivKey)> { Ok((token, priv_key)) } -/// Get the entire list of api toksn +/// Get the entire list of api tokens pub async fn full_list() -> anyhow::Result> { let mut list = Vec::new(); for f in std::fs::read_dir(AppConfig::get().api_tokens_path())? { @@ -129,3 +130,8 @@ pub async fn full_list() -> anyhow::Result> { } Ok(list) } + +/// Get the information about a single token +pub async fn get_single(id: TokenID) -> anyhow::Result { + Token::load_from_file(&AppConfig::get().api_token_definition_path(id)) +} diff --git a/virtweb_backend/src/controllers/api_tokens_controller.rs b/virtweb_backend/src/controllers/api_tokens_controller.rs index 12da9f6..4f80eae 100644 --- a/virtweb_backend/src/controllers/api_tokens_controller.rs +++ b/virtweb_backend/src/controllers/api_tokens_controller.rs @@ -1,7 +1,7 @@ //! # API tokens management use crate::api_tokens; -use crate::api_tokens::NewToken; +use crate::api_tokens::{NewToken, TokenID}; use crate::controllers::api_tokens_controller::rest_token::RestToken; use crate::controllers::HttpResult; use crate::utils::jwt_utils::TokenPrivKey; @@ -58,3 +58,15 @@ pub async fn list() -> HttpResult { Ok(HttpResponse::Ok().json(list)) } + +#[derive(serde::Deserialize)] +pub struct TokenIDInPath { + uid: TokenID, +} + +/// Get the information about a single token +pub async fn get_single(path: web::Path) -> HttpResult { + let token = api_tokens::get_single(path.uid).await?; + + Ok(HttpResponse::Ok().json(RestToken::new(token))) +} diff --git a/virtweb_backend/src/main.rs b/virtweb_backend/src/main.rs index 7b84199..4176739 100644 --- a/virtweb_backend/src/main.rs +++ b/virtweb_backend/src/main.rs @@ -286,11 +286,11 @@ async fn main() -> std::io::Result<()> { "/api/tokens/list", web::get().to(api_tokens_controller::list), ) - /* TODO .route( + .route( "/api/tokens/{uid}", web::get().to(api_tokens_controller::get_single), ) - .route( + /* TODO .route( "/api/tokens/{uid}", web::put().to(api_tokens_controller::update), ) diff --git a/virtweb_backend/src/utils/jwt_utils.rs b/virtweb_backend/src/utils/jwt_utils.rs index aa379bc..d8beb25 100644 --- a/virtweb_backend/src/utils/jwt_utils.rs +++ b/virtweb_backend/src/utils/jwt_utils.rs @@ -6,10 +6,10 @@ use rand::rngs::OsRng; use serde::de::DeserializeOwned; use serde::Serialize; -#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] +#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Eq, PartialEq)] #[serde(tag = "alg")] pub enum TokenPubKey { - /// This variant DOES make crash the program. It MUST NOT be serialized. + /// This variant DOES make crash the program. It MUST NOT used to validate JWT. /// /// It is a hack to hide public key when getting the list of tokens None, @@ -18,6 +18,12 @@ pub enum TokenPubKey { ES384 { r#pub: String }, } +impl TokenPubKey { + pub fn is_invalid(&self) -> bool { + self == &TokenPubKey::None + } +} + #[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] #[serde(tag = "alg")] pub enum TokenPrivKey {