Skip to content

Commit f112c08

Browse files
authored
Add the ability to decode a JWT token without specifying an audience. (#336)
Adding this allows us to continue using this library to decode a JWT token with a secret (the way it used to be possible pre v9). Without this we cannot update to v9 and we are stuck in v8.3. Co-authored-by: sagunb <sagunb@users.noreply.github.com>
1 parent d4f3300 commit f112c08

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/validation.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@ pub struct Validation {
5151
///
5252
/// Defaults to `false`.
5353
pub validate_nbf: bool,
54-
/// If it contains a value, the validation will check that the `aud` field is a member of the
54+
/// Whether to validate the `aud` field.
55+
///
56+
/// It will return an error if the `aud` field is not a member of the audience provided.
57+
///
58+
/// Defaults to `true`. Very insecure to turn this off. Only do this if you know what you are doing.
59+
pub validate_aud: bool,
60+
/// Validation will check that the `aud` field is a member of the
5561
/// audience provided and will error otherwise.
5662
/// Use `set_audience` to set it
5763
///
@@ -91,6 +97,7 @@ impl Validation {
9197

9298
validate_exp: true,
9399
validate_nbf: false,
100+
validate_aud: true,
94101

95102
iss: None,
96103
sub: None,
@@ -270,6 +277,9 @@ pub(crate) fn validate(claims: ClaimsForValidation, options: &Validation) -> Res
270277
_ => {}
271278
}
272279

280+
if !options.validate_aud {
281+
return Ok(());
282+
}
273283
match (claims.aud, options.aud.as_ref()) {
274284
// Each principal intended to process the JWT MUST
275285
// identify itself with a value in the audience claim. If the principal
@@ -664,6 +674,18 @@ mod tests {
664674
};
665675
}
666676

677+
#[test]
678+
fn aud_validation_skipped() {
679+
let claims = json!({"aud": ["Everyone"]});
680+
let mut validation = Validation::new(Algorithm::HS256);
681+
validation.validate_exp = false;
682+
validation.validate_aud = false;
683+
validation.required_spec_claims = HashSet::new();
684+
validation.aud = None;
685+
let res = validate(deserialize_claims(&claims), &validation);
686+
assert!(res.is_ok());
687+
}
688+
667689
#[test]
668690
fn aud_missing_fails() {
669691
let claims = json!({});

0 commit comments

Comments
 (0)