Skip to content

Commit 93424d5

Browse files
authored
complete documentation with required claims (#382)
1 parent 5162702 commit 93424d5

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ This library automatically validates the `exp` claim, and `nbf` is validated if
160160
those require setting the expected values in the `Validation` struct. In the case of `aud`, if there is a value set in the token but
161161
not in the `Validation`, the token will be rejected.
162162

163+
Validation is only made on present fields in the claims. It is possible to define the required claims, hence verifying that a JWT has a value for each of these claims before it is considered for validation. The required claims can be set in the `Validation` struct. The default option requires the `exp` claim to be present.
164+
163165
Since validating time fields is always a bit tricky due to clock skew,
164166
you can add some leeway to the `iat`, `exp`, and `nbf` validation by setting the `leeway` field.
165167

examples/validation.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ fn main() {
2626
let mut validation = Validation::new(Algorithm::HS256);
2727
validation.sub = Some("b@b.com".to_string());
2828
validation.set_audience(&["me"]);
29+
validation.set_required_spec_claims(&["exp", "sub", "aud"]);
2930
let token_data = match decode::<Claims>(&token, &DecodingKey::from_secret(key), &validation) {
3031
Ok(c) => c,
3132
Err(err) => match *err.kind() {

src/validation.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ use crate::errors::{new_error, ErrorKind, Result};
2424
/// // or issuer
2525
/// validation.set_issuer(&["Me"]); // a single string
2626
/// validation.set_issuer(&["Me", "You"]); // array of strings
27+
/// // Setting required claims
28+
/// validation.set_required_spec_claims(&["exp", "iss", "aud"]);
2729
/// ```
2830
#[derive(Debug, Clone, PartialEq, Eq)]
2931
pub struct Validation {
@@ -56,29 +58,44 @@ pub struct Validation {
5658
///
5759
/// It will return an error if the current timestamp is before the time in the `nbf` field.
5860
///
61+
/// Validation only happens if `nbf` claim is present in the token.
62+
/// Adding `nbf` to `required_spec_claims` will make it required.
63+
///
5964
/// Defaults to `false`.
6065
pub validate_nbf: bool,
6166
/// Whether to validate the `aud` field.
6267
///
6368
/// It will return an error if the `aud` field is not a member of the audience provided.
6469
///
70+
/// Validation only happens if `aud` claim is present in the token.
71+
/// Adding `aud` to `required_spec_claims` will make it required.
72+
///
6573
/// Defaults to `true`. Very insecure to turn this off. Only do this if you know what you are doing.
6674
pub validate_aud: bool,
6775
/// Validation will check that the `aud` field is a member of the
6876
/// audience provided and will error otherwise.
6977
/// Use `set_audience` to set it
7078
///
79+
/// Validation only happens if `aud` claim is present in the token.
80+
/// Adding `aud` to `required_spec_claims` will make it required.
81+
///
7182
/// Defaults to `None`.
7283
pub aud: Option<HashSet<String>>,
7384
/// If it contains a value, the validation will check that the `iss` field is a member of the
7485
/// iss provided and will error otherwise.
7586
/// Use `set_issuer` to set it
7687
///
88+
/// Validation only happens if `iss` claim is present in the token.
89+
/// Adding `iss` to `required_spec_claims` will make it required.
90+
///
7791
/// Defaults to `None`.
7892
pub iss: Option<HashSet<String>>,
7993
/// If it contains a value, the validation will check that the `sub` field is the same as the
8094
/// one provided and will error otherwise.
8195
///
96+
/// Validation only happens if `sub` claim is present in the token.
97+
/// Adding `sub` to `required_spec_claims` will make it required.
98+
///
8299
/// Defaults to `None`.
83100
pub sub: Option<String>,
84101
/// The validation will check that the `alg` of the header is contained

0 commit comments

Comments
 (0)