Skip to content

Commit 6e3d20e

Browse files
committed
trustpub: Implement UnverifiedClaims::decode() fn
This fn can be used to decode a JSON web token without verifying it's signature or claims. Only the `iss` claim will actually be decoded, since we use that to find the correct decoding key for the JWT issuer.
1 parent f23d953 commit 6e3d20e

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use jsonwebtoken::errors::Error;
2+
use jsonwebtoken::{DecodingKey, TokenData, Validation};
3+
use serde::Deserialize;
4+
use std::sync::LazyLock;
5+
6+
/// [`Validation`] configuration for decoding JWTs without any
7+
/// signature validation.
8+
///
9+
/// This must only be used to extract the `iss` claim from the JWT, which
10+
/// is then used to look up the corresponding OIDC key set.
11+
static NO_VALIDATION: LazyLock<Validation> = LazyLock::new(|| {
12+
let mut no_validation = Validation::default();
13+
no_validation.validate_aud = false;
14+
no_validation.validate_exp = false;
15+
no_validation.insecure_disable_signature_validation();
16+
no_validation
17+
});
18+
19+
/// Empty [`DecodingKey`] used for decoding JWTs without any signature
20+
/// validation.
21+
///
22+
/// See [`NO_VALIDATION`] for more details.
23+
static EMPTY_KEY: LazyLock<DecodingKey> = LazyLock::new(|| DecodingKey::from_secret(b""));
24+
25+
#[derive(Debug, Deserialize)]
26+
pub struct UnverifiedClaims {
27+
pub iss: String,
28+
}
29+
30+
impl UnverifiedClaims {
31+
pub fn decode(token: &str) -> Result<TokenData<Self>, Error> {
32+
jsonwebtoken::decode(token, &EMPTY_KEY, &NO_VALIDATION)
33+
}
34+
}

0 commit comments

Comments
 (0)