Can get a single API token
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
Pierre HUBERT 2024-04-09 19:04:49 +02:00
parent f56e9c14b2
commit 0c5a232a25
4 changed files with 30 additions and 6 deletions

View File

@ -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<TokenRights>,
pub last_used: Option<u64>,
@ -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<Vec<Token>> {
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<Vec<Token>> {
}
Ok(list)
}
/// Get the information about a single token
pub async fn get_single(id: TokenID) -> anyhow::Result<Token> {
Token::load_from_file(&AppConfig::get().api_token_definition_path(id))
}

View File

@ -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<TokenIDInPath>) -> HttpResult {
let token = api_tokens::get_single(path.uid).await?;
Ok(HttpResponse::Ok().json(RestToken::new(token)))
}

View File

@ -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),
)

View File

@ -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 {