1+ /// This module provides middleware for handling JWT authentication in a web application.
2+ /// It supports multiple algorithms for verifying JWTs and integrates with the application's request/response lifecycle.
13use std:: { collections:: HashMap , future:: Future , pin:: Pin , sync:: Arc } ;
24
35use http:: { StatusCode , header:: AUTHORIZATION } ;
@@ -10,6 +12,8 @@ use crate::{
1012 types:: { Request , Response } ,
1113} ;
1214
15+ /// Represents a verification key for various JWT algorithms.
16+ /// This enum supports multiple algorithms, including HMAC, RSA, and EdDSA.
1317pub enum AnyVerifyKey {
1418 HS256 ( Arc < HS256Key > ) ,
1519 HS384 ( Arc < HS384Key > ) ,
@@ -32,6 +36,10 @@ pub enum AnyVerifyKey {
3236}
3337
3438impl AnyVerifyKey {
39+ /// Returns the algorithm identifier for the verification key.
40+ ///
41+ /// # Returns
42+ /// A static string representing the algorithm (e.g., "HS256", "RS256").
3543 pub fn alg_id ( & self ) -> & ' static str {
3644 match self {
3745 Self :: HS256 ( _) => "HS256" ,
@@ -55,6 +63,13 @@ impl AnyVerifyKey {
5563 }
5664 }
5765
66+ /// Verifies a JWT token using the current verification key.
67+ ///
68+ /// # Parameters
69+ /// - `token`: The JWT token to verify.
70+ ///
71+ /// # Returns
72+ /// A `Result` containing the decoded claims if verification succeeds, or an error otherwise.
5873 pub fn verify < C > ( & self , token : & str ) -> Result < JWTClaims < C > , jwt_simple:: Error >
5974 where
6075 C : Serialize + DeserializeOwned ,
@@ -83,6 +98,10 @@ impl AnyVerifyKey {
8398 }
8499}
85100
101+ /// Middleware for handling JWT authentication.
102+ ///
103+ /// This struct allows configuring multiple verification keys for different algorithms
104+ /// and integrates with the application's middleware chain.
86105pub struct JwtAuth < T >
87106where
88107 T : DeserializeOwned + Send + Sync + ' static ,
@@ -95,6 +114,14 @@ impl<T> JwtAuth<T>
95114where
96115 T : DeserializeOwned + Send + Sync + ' static ,
97116{
117+ /// Creates a new instance of `JwtAuth` with the provided verification keys.
118+ ///
119+ /// # Parameters
120+ /// - `keys`: A `HashMap` where the key is the algorithm identifier (e.g., "HS256")
121+ /// and the value is the corresponding verification key.
122+ ///
123+ /// # Returns
124+ /// A new `JwtAuth` instance.
98125 pub fn new ( keys : HashMap < & ' static str , AnyVerifyKey > ) -> Self {
99126 Self {
100127 keys : Arc :: new ( keys) ,
@@ -107,6 +134,13 @@ impl<T> IntoMiddleware for JwtAuth<T>
107134where
108135 T : Clone + Serialize + DeserializeOwned + Send + Sync + ' static ,
109136{
137+ /// Converts the `JwtAuth` instance into a middleware function.
138+ ///
139+ /// The middleware extracts the JWT token from the `Authorization` header,
140+ /// verifies it using the configured keys, and injects the claims into the request's extensions.
141+ ///
142+ /// # Returns
143+ /// A closure that represents the middleware function.
110144 fn into_middleware (
111145 self ,
112146 ) -> impl Fn ( Request , Next ) -> Pin < Box < dyn Future < Output = Response > + Send + ' static > >
0 commit comments