From e1da57a407cca50ec8391dc7889345b520d892a2 Mon Sep 17 00:00:00 2001 From: shimun Date: Sat, 24 Dec 2022 16:25:56 +0100 Subject: [PATCH] refactor: move path types into module --- src/api.rs | 32 ++------------------------------ src/api/extract.rs | 4 ++-- src/client.rs | 6 +++--- src/main.rs | 1 + src/routes.rs | 28 ++++++++++++++++++++++++++++ 5 files changed, 36 insertions(+), 35 deletions(-) create mode 100644 src/routes.rs diff --git a/src/api.rs b/src/api.rs index 517ac3a..e146c2a 100644 --- a/src/api.rs +++ b/src/api.rs @@ -8,15 +8,13 @@ use std::time::{Duration, SystemTime}; use crate::certs::{load_cert_by_id, read_certs, read_pubkey, store_cert}; use crate::env_key; +use crate::routes::*; use anyhow::Context; use axum::body; use axum::extract::{Query, State}; use axum::{http::StatusCode, response::IntoResponse, Json, Router}; -use axum_extra::routing::{ - RouterExt, // for `Router::typed_*` - TypedPath, -}; +use axum_extra::routing::RouterExt; use clap::{Args, Parser}; use jwt_compact::alg::{Hs256, Hs256Key}; use jwt_compact::{AlgorithmExt, Token, UntrustedToken}; @@ -202,10 +200,6 @@ async fn fallback_404() -> ApiResult<()> { Err(ApiError::CertificateNotFound) } -#[derive(TypedPath, Deserialize)] -#[typed_path("/certs")] -pub struct CertList; - async fn list_certs( _: CertList, State(ApiState { certs, .. }): State, @@ -227,12 +221,6 @@ struct AuthClaims { identifier: String, } -#[derive(TypedPath, Deserialize)] -#[typed_path("/certs/:identifier")] -pub struct GetCert { - pub identifier: String, -} - /// Retrieve an certificate for identifier /// TODO: add option to require auth /// return Unauthorized with an challenge @@ -264,12 +252,6 @@ async fn get_certs_identifier( Ok(cert.to_openssh().context("to openssh")?) } -#[derive(TypedPath, Deserialize)] -#[typed_path("/certs/:identifier/info")] -pub struct GetCertInfo { - pub identifier: String, -} - #[cfg(feature = "info")] #[derive(Debug, Serialize)] struct CertInfo { @@ -312,12 +294,6 @@ async fn get_cert_info( unimplemented!() } -#[derive(TypedPath, Deserialize)] -#[typed_path("/certs/:identifier")] -pub struct PostCertInfo { - pub identifier: String, -} - #[derive(Debug, Deserialize)] struct PostCertsQuery { challenge: String, @@ -353,10 +329,6 @@ async fn post_certs_identifier( Ok(cert.to_openssh().context("to openssh")?) } -#[derive(TypedPath)] -#[typed_path("/cert")] -pub struct PutCert; - /// Upload an cert with an higher serial than the previous async fn put_cert_update( _: PutCert, diff --git a/src/api/extract.rs b/src/api/extract.rs index 4b9ef79..4db8d7a 100644 --- a/src/api/extract.rs +++ b/src/api/extract.rs @@ -1,10 +1,10 @@ +use super::ApiError; use anyhow::Context; use axum::{ - async_trait, body::BoxBody, extract::FromRequest, http::Request, response::IntoResponse, + async_trait, body::BoxBody, extract::FromRequest, http::Request, }; use ssh_key::{Certificate, SshSig}; use tracing::trace; -use super::ApiError; #[derive(Debug, Clone)] pub struct CertificateBody(pub Certificate); diff --git a/src/client.rs b/src/client.rs index 16b9ff3..26064ef 100644 --- a/src/client.rs +++ b/src/client.rs @@ -3,7 +3,7 @@ use axum_extra::routing::TypedPath; use clap::{Args, Parser, Subcommand}; use reqwest::{Client, StatusCode}; use ssh_key::Certificate; -use std::io::{stdin, stdout}; +use std::io::{stdin}; use std::path::PathBuf; use std::time::{Duration, SystemTime}; use tokio::fs; @@ -11,10 +11,10 @@ use tracing::{debug, error, info, instrument, trace}; use url::Url; -use crate::api::PutCert; use crate::certs::load_cert; +use crate::certs::read_dir; use crate::env_key; -use crate::{api::GetCert, certs::read_dir}; +use crate::routes::*; #[derive(Parser)] pub struct ClientArgs { diff --git a/src/main.rs b/src/main.rs index e3185a6..f54af37 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ mod api; mod certs; #[cfg(feature = "client")] mod client; +mod routes; #[macro_export] macro_rules! env_key { diff --git a/src/routes.rs b/src/routes.rs new file mode 100644 index 0000000..3b4599e --- /dev/null +++ b/src/routes.rs @@ -0,0 +1,28 @@ +use axum_extra::routing::TypedPath; +use serde::Deserialize; + +#[derive(TypedPath, Deserialize)] +#[typed_path("/certs")] +pub struct CertList; + +#[derive(TypedPath, Deserialize)] +#[typed_path("/certs/:identifier")] +pub struct GetCert { + pub identifier: String, +} + +#[derive(TypedPath, Deserialize)] +#[typed_path("/certs/:identifier/info")] +pub struct GetCertInfo { + pub identifier: String, +} + +#[derive(TypedPath, Deserialize)] +#[typed_path("/certs/:identifier")] +pub struct PostCertInfo { + pub identifier: String, +} + +#[derive(TypedPath)] +#[typed_path("/cert")] +pub struct PutCert;