Skip to content

Commit a083324

Browse files
feat: Add insecure_decode_without_signature_validation
1 parent 87bbe49 commit a083324

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

src/decoding.rs

+40
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,43 @@ pub fn decode_header(token: &str) -> Result<Header> {
286286
let (_, header) = expect_two!(message.rsplitn(2, '.'));
287287
Header::from_encoded(header)
288288
}
289+
290+
/// Decode a JWT without any signature verification and return its claims.
291+
/// This means that the token is not verified so use with caution.
292+
/// This is useful when you want to extract the claims without verifying the signature.
293+
///
294+
/// # Arguments
295+
///
296+
/// * `token` - A string slice that holds the JWT token
297+
/// * `validation` - A [Validation](struct.Validation.html) object that holds the validation options
298+
///
299+
/// # Example
300+
///
301+
/// ```rust
302+
/// use jsonwebtoken::{insecure_decode_without_signature_validation, Validation, Algorithm};
303+
/// use serde::{Deserialize, Serialize};
304+
///
305+
/// #[derive(Debug, Serialize, Deserialize)]
306+
/// struct Claims {
307+
/// sub: u32,
308+
/// name: String,
309+
/// iat: u64,
310+
/// exp: u64
311+
/// }
312+
///
313+
/// // Example token from jwt.io
314+
/// let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjEyMzQ1Njc4OTAsIm5hbWUiOiJKb2huIERvZSIsImlhdCI6MTUxNjIzOTAyMiwiZXhwIjoyNTE2MjM5MDYwfQ.Yf3kCk-BdkW3DZNao3lwMoU41ujnt86OgewBA-Q2uBw".to_string();
315+
/// let validation = Validation::new(Algorithm::HS256);
316+
/// let claims = insecure_decode_without_signature_validation::<Claims>(&token, &validation).unwrap();
317+
/// ```
318+
pub fn insecure_decode_without_signature_validation<T: DeserializeOwned>(
319+
token: &str,
320+
validation: &Validation,
321+
) -> Result<T> {
322+
let (_, rest) = expect_two!(token.rsplitn(2, '.'));
323+
let (claims, _) = expect_two!(rest.rsplitn(2, '.'));
324+
let decoded_claims = DecodedJwtPartClaims::from_jwt_part_claims(claims)?;
325+
let claims = decoded_claims.deserialize()?;
326+
validate(decoded_claims.deserialize()?, validation)?;
327+
Ok(claims)
328+
}

src/jwk.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(missing_docs)]
22
//! This crate contains types only for working JWK and JWK Sets
33
//! This is only meant to be used to deal with public JWK, not generate ones.
4-
//! Most of the code in this file is taken from https://github.com/lawliet89/biscuit but
4+
//! Most of the code in this file is taken from <https://github.com/lawliet89/biscuit> but
55
//! tweaked to remove the private bits as it's not the goal for this crate currently.
66
77
use crate::{

src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ mod serialization;
1818
mod validation;
1919

2020
pub use algorithms::Algorithm;
21-
pub use decoding::{decode, decode_header, DecodingKey, TokenData};
21+
pub use decoding::{
22+
decode, decode_header, insecure_decode_without_signature_validation, DecodingKey, TokenData,
23+
};
2224
pub use encoding::{encode, EncodingKey};
2325
pub use header::Header;
2426
pub use validation::{get_current_timestamp, Validation};

0 commit comments

Comments
 (0)