@@ -4,12 +4,33 @@ use serde::Deserialize;
4
4
5
5
use salvo_core:: Depot ;
6
6
7
- /// JwtAuthDecoder is used to decode a token into claims.
7
+ /// Trait for JWT token decoding and validation.
8
+ ///
9
+ /// Implementors of this trait are responsible for decoding JWT tokens into claims objects
10
+ /// and performing any necessary validation. The `JwtAuth` middleware uses the configured
11
+ /// decoder to validate tokens extracted from requests.
12
+ ///
13
+ /// The crate provides built-in implementations:
14
+ /// - `ConstDecoder`: Uses a static key for token validation
15
+ /// - `OidcDecoder`: Uses OpenID Connect for validation (requires the `oidc` feature)
8
16
pub trait JwtAuthDecoder {
9
- /// Error type.
17
+ /// The error type returned if decoding or validation fails .
10
18
type Error : std:: error:: Error + Send + Sync + ' static ;
11
19
12
- ///Decode token.
20
+ /// Decodes and validates a JWT token.
21
+ ///
22
+ /// # Parameters
23
+ ///
24
+ /// * `token` - The JWT token string to decode
25
+ /// * `depot` - The current request's depot, which can be used to store/retrieve additional data
26
+ ///
27
+ /// # Type Parameters
28
+ ///
29
+ /// * `C` - The claims type to deserialize from the token payload
30
+ ///
31
+ /// # Returns
32
+ ///
33
+ /// Returns a `Result` containing either the decoded token data or an error.
13
34
fn decode < C > (
14
35
& self ,
15
36
token : & str ,
@@ -19,41 +40,51 @@ pub trait JwtAuthDecoder {
19
40
C : for < ' de > Deserialize < ' de > ;
20
41
}
21
42
22
- /// ConstDecoder will decode token with a static secret.
43
+ /// A decoder that uses a constant key for JWT token validation.
44
+ ///
45
+ /// This is the simplest decoder implementation, suitable for applications using
46
+ /// symmetric key signing (HMAC) or a single asymmetric key pair (RSA, ECDSA).
23
47
pub struct ConstDecoder {
48
+ /// Key used for validating JWT signatures
24
49
decoding_key : DecodingKey ,
50
+
51
+ /// JWT validation parameters
25
52
validation : Validation ,
26
53
}
27
54
28
55
impl ConstDecoder {
29
- /// Create a new `ConstDecoder` .
56
+ /// Creates a new decoder with the given decoding key and default validation .
30
57
pub fn new ( decoding_key : DecodingKey ) -> Self {
31
58
Self {
32
59
decoding_key,
33
60
validation : Validation :: default ( ) ,
34
61
}
35
62
}
36
- /// Create a new `ConstDecoder` with validation.
63
+
64
+ /// Creates a new decoder with the given decoding key and custom validation parameters.
37
65
pub fn with_validation ( decoding_key : DecodingKey , validation : Validation ) -> Self {
38
66
Self {
39
67
decoding_key,
40
68
validation,
41
69
}
42
70
}
43
71
44
- /// If you're using HMAC, use this.
72
+ /// Creates a decoder from a raw secret byte array for HMAC verification.
73
+ ///
74
+ /// This is the most common method for symmetric key validation.
45
75
pub fn from_secret ( secret : & [ u8 ] ) -> Self {
46
76
Self :: with_validation ( DecodingKey :: from_secret ( secret) , Validation :: default ( ) )
47
77
}
48
78
49
- /// If you're using HMAC with a base64 encoded secret, use this .
79
+ /// Creates a decoder from a base64- encoded secret string for HMAC verification .
50
80
pub fn from_base64_secret ( secret : & str ) -> Result < Self , JwtError > {
51
81
DecodingKey :: from_base64_secret ( secret)
52
82
. map ( |key| Self :: with_validation ( key, Validation :: default ( ) ) )
53
83
}
54
84
55
- /// If you are loading a public RSA key in a PEM format, use this.
56
- /// Only exists if the feature `use_pem` is enabled.
85
+ /// Creates a decoder from an RSA public key in PEM format.
86
+ ///
87
+ /// Only available when the `use_pem` feature is enabled.
57
88
pub fn from_rsa_pem ( key : & [ u8 ] ) -> Result < Self , JwtError > {
58
89
DecodingKey :: from_rsa_pem ( key)
59
90
. map ( |key| Self :: with_validation ( key, Validation :: new ( Algorithm :: RS256 ) ) )
0 commit comments