Skip to content

Commit 14f1ef4

Browse files
committed
Support decoding byte slice
Taken from #387 by @kvc0
1 parent f1cf92c commit 14f1ef4

File tree

8 files changed

+18
-17
lines changed

8 files changed

+18
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- BREAKING: now using traits for crypto backends, you have to choose between `aws_lc_rs` and `rust_crypto`
66
- Add `Clone` bound to `decode`
7+
- Support decoding byte slices
78

89
## 9.3.1 (2024-02-06)
910

examples/custom_header.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::collections::HashMap;
44
use jsonwebtoken::errors::ErrorKind;
55
use jsonwebtoken::{decode, encode, Algorithm, DecodingKey, EncodingKey, Header, Validation};
66

7-
#[derive(Debug, Serialize, Deserialize)]
7+
#[derive(Debug, Serialize, Deserialize, Clone)]
88
struct Claims {
99
sub: String,
1010
company: String,

examples/custom_time.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use jsonwebtoken::{Algorithm, DecodingKey, EncodingKey, Header, Validation};
55

66
const SECRET: &str = "some-secret";
77

8-
#[derive(Debug, PartialEq, Serialize, Deserialize)]
8+
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
99
struct Claims {
1010
sub: String,
1111
#[serde(with = "jwt_numeric_date")]

examples/ed25519.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use jsonwebtoken::{
77
decode, encode, get_current_timestamp, Algorithm, DecodingKey, EncodingKey, Validation,
88
};
99

10-
#[derive(Debug, Serialize, Deserialize)]
10+
#[derive(Debug, Serialize, Deserialize, Clone)]
1111
pub struct Claims {
1212
sub: String,
1313
exp: u64,

examples/validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
33
use jsonwebtoken::errors::ErrorKind;
44
use jsonwebtoken::{decode, encode, Algorithm, DecodingKey, EncodingKey, Header, Validation};
55

6-
#[derive(Debug, Serialize, Deserialize)]
6+
#[derive(Debug, Serialize, Deserialize, Clone)]
77
struct Claims {
88
aud: String,
99
sub: String,

src/algorithms.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ impl AlgorithmFamily {
3131
}
3232
}
3333

34-
35-
3634
/// The algorithms supported for signing/verifying JWTs
3735
#[allow(clippy::upper_case_acronyms)]
3836
#[derive(Debug, Default, PartialEq, Eq, Hash, Copy, Clone, Serialize, Deserialize)]

src/decoding.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl DecodingKey {
9797
pub fn family(&self) -> AlgorithmFamily {
9898
self.family
9999
}
100-
100+
101101
/// If you're using HMAC, use this.
102102
pub fn from_secret(secret: &[u8]) -> Self {
103103
DecodingKey {
@@ -270,10 +270,11 @@ impl DecodingKey {
270270
/// let token_message = decode::<Claims>(&token, &DecodingKey::from_secret("secret".as_ref()), &Validation::new(Algorithm::HS256));
271271
/// ```
272272
pub fn decode<T: DeserializeOwned + Clone>(
273-
token: &str,
273+
token: impl AsRef<[u8]>,
274274
key: &DecodingKey,
275275
validation: &Validation,
276276
) -> Result<TokenData<T>> {
277+
let token = token.as_ref();
277278
let header = decode_header(token)?;
278279

279280
if validation.validate_signature && !validation.algorithms.contains(&header.alg) {
@@ -324,20 +325,21 @@ pub fn jwt_verifier_factory(
324325
/// let token = "a.jwt.token".to_string();
325326
/// let header = decode_header(&token);
326327
/// ```
327-
pub fn decode_header(token: &str) -> Result<Header> {
328-
let (_, message) = expect_two!(token.rsplitn(2, '.'));
329-
let (_, header) = expect_two!(message.rsplitn(2, '.'));
328+
pub fn decode_header(token: impl AsRef<[u8]>) -> Result<Header> {
329+
let token = token.as_ref();
330+
let (_, message) = expect_two!(token.rsplitn(2, |b| *b == b'.'));
331+
let (_, header) = expect_two!(message.rsplitn(2, |b| *b == b'.'));
330332
Header::from_encoded(header)
331333
}
332334

333335
/// Verify the signature of a JWT, and return a header object and raw payload.
334336
///
335337
/// If the token or its signature is invalid, it will return an error.
336338
fn verify_signature<'a>(
337-
token: &'a str,
339+
token: &'a [u8],
338340
validation: &Validation,
339341
verifying_provider: Box<dyn JwtVerifier>,
340-
) -> Result<(Header, &'a str)> {
342+
) -> Result<(Header, &'a [u8])> {
341343
if validation.validate_signature && validation.algorithms.is_empty() {
342344
return Err(new_error(ErrorKind::MissingAlgorithm));
343345
}
@@ -350,16 +352,16 @@ fn verify_signature<'a>(
350352
}
351353
}
352354

353-
let (signature, message) = expect_two!(token.rsplitn(2, '.'));
354-
let (payload, header) = expect_two!(message.rsplitn(2, '.'));
355+
let (signature, message) = expect_two!(token.rsplitn(2, |b| *b == b'.'));
356+
let (payload, header) = expect_two!(message.rsplitn(2, |b| *b == b'.'));
355357
let header = Header::from_encoded(header)?;
356358

357359
if validation.validate_signature && !validation.algorithms.contains(&header.alg) {
358360
return Err(new_error(ErrorKind::InvalidAlgorithm));
359361
}
360362

361363
if validation.validate_signature
362-
&& verifying_provider.verify(message.as_bytes(), &b64_decode(signature)?).is_err()
364+
&& verifying_provider.verify(message, &b64_decode(signature)?).is_err()
363365
{
364366
return Err(new_error(ErrorKind::InvalidSignature));
365367
}

src/encoding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl EncodingKey {
4444
pub fn family(&self) -> AlgorithmFamily {
4545
self.family
4646
}
47-
47+
4848
/// If you're using a HMAC secret that is not base64, use that.
4949
pub fn from_secret(secret: &[u8]) -> Self {
5050
EncodingKey { family: AlgorithmFamily::Hmac, content: secret.to_vec() }

0 commit comments

Comments
 (0)