Skip to content

Optional aide feature #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion jwt-authorizer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repository = "https://github.com/cduvray/jwt-authorizer"
keywords = ["jwt", "axum", "authorisation", "jwks"]

[dependencies]
axum = { version = "0.7" }
axum = { version = "0.8" }
chrono = { version = "0.4", optional = true }
futures-util = "0.3"
futures-core = "0.3"
Expand All @@ -30,6 +30,7 @@ tracing = "0.1"
tonic = { version = "0.12", optional = true }
time = { version = "0.3", optional = true }
http-body-util = "0.1.1"
aide = { version = "0.14", optional = true }

[dev-dependencies]
hyper = { version = "1.3.1", features = ["full"] }
Expand All @@ -51,6 +52,7 @@ rustls-tls-webpki-roots = ["reqwest/rustls-tls-webpki-roots"]
rustls-tls-native-roots = ["reqwest/rustls-tls-native-roots"]
time = ["dep:time"]
chrono = ["dep:chrono"]
aide = ["dep:aide"]

[[test]]
name = "tonic"
Expand Down
62 changes: 60 additions & 2 deletions jwt-authorizer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![doc = include_str!("../docs/README.md")]

use axum::{async_trait, extract::FromRequestParts, http::request::Parts};
use axum::{
extract::{FromRequestParts, OptionalFromRequestParts},
http::request::Parts,
};
use jsonwebtoken::TokenData;
use serde::de::DeserializeOwned;

Expand All @@ -24,7 +27,6 @@ pub mod validation;
#[derive(Debug, Clone, Copy, Default)]
pub struct JwtClaims<T>(pub T);

#[async_trait]
impl<T, S> FromRequestParts<S> for JwtClaims<T>
where
T: DeserializeOwned + Send + Sync + Clone + 'static,
Expand All @@ -40,3 +42,59 @@ where
}
}
}

impl<T, S> OptionalFromRequestParts<S> for JwtClaims<T>
where
T: DeserializeOwned + Send + Sync + Clone + 'static,
S: Send + Sync,
{
type Rejection = AuthError;

// Required method
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Option<Self>, Self::Rejection> {
Ok(parts
.extensions
.get::<TokenData<T>>()
.map(|claims| JwtClaims(claims.claims.clone())))
}
}

#[cfg(feature = "aide")]
mod aide {

use super::JwtClaims;
use aide::{
generate::GenContext,
openapi::{HeaderStyle, Operation, Parameter, ParameterData, ParameterSchemaOrContent, SchemaObject},
operation::add_parameters,
OperationInput,
};

impl<T> OperationInput for JwtClaims<T> {
fn operation_input(ctx: &mut GenContext, operation: &mut Operation) {
let s = ctx.schema.subschema_for::<String>();
add_parameters(
ctx,
operation,
[Parameter::Header {
parameter_data: ParameterData {
name: "Authorization".to_string(),
description: Some("Jwt Bearer token".to_string()),
required: true,
format: ParameterSchemaOrContent::Schema(SchemaObject {
json_schema: s,
example: None,
external_docs: None,
}),
extensions: Default::default(),
deprecated: None,
example: None,
examples: Default::default(),
explode: None,
},
style: HeaderStyle::Simple,
}],
);
}
}
}