Skip to content

Commit abf4e7e

Browse files
committed
back to 1 fn, with impl AsRef<[u8]>
1 parent e340d75 commit abf4e7e

File tree

3 files changed

+14
-75
lines changed

3 files changed

+14
-75
lines changed

benches/jwt.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use criterion::{black_box, criterion_group, criterion_main, Criterion};
22
use jsonwebtoken::{
3-
decode, decode_bytes, decode_header, decode_header_bytes, encode, Algorithm, DecodingKey,
4-
EncodingKey, Header, Validation,
3+
decode, decode_header, encode, Algorithm, DecodingKey, EncodingKey, Header, Validation,
54
};
65
use serde::{Deserialize, Serialize};
76

@@ -21,27 +20,16 @@ fn bench_encode(c: &mut Criterion) {
2120
}
2221

2322
fn bench_decode(c: &mut Criterion) {
24-
let token = b"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ";
23+
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ";
2524
let key = DecodingKey::from_secret("secret".as_ref());
2625

2726
let mut group = c.benchmark_group("decode");
2827
group.throughput(criterion::Throughput::Bytes(token.len() as u64));
2928

30-
group.bench_function("bytes", |b| {
31-
b.iter(|| {
32-
decode_bytes::<Claims>(
33-
black_box(token),
34-
black_box(&key),
35-
black_box(&Validation::new(Algorithm::HS256)),
36-
)
37-
})
38-
});
39-
4029
group.bench_function("str", |b| {
4130
b.iter(|| {
4231
decode::<Claims>(
43-
// Simulate the cost of validating &str before decoding
44-
black_box(std::str::from_utf8(black_box(token)).expect("valid utf8")),
32+
black_box(token),
4533
black_box(&key),
4634
black_box(&Validation::new(Algorithm::HS256)),
4735
)
@@ -56,12 +44,10 @@ fn bench_decode(c: &mut Criterion) {
5644
b.iter(|| {
5745
decode_header(
5846
// Simulate the cost of validating &str before decoding
59-
black_box(std::str::from_utf8(black_box(token)).expect("valid utf8")),
47+
black_box(token),
6048
)
6149
})
6250
});
63-
64-
group.bench_function("bytes", |b| b.iter(|| decode_header_bytes(black_box(token))));
6551
}
6652

6753
criterion_group!(benches, bench_encode, bench_decode);

src/decoding.rs

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ fn verify_signature_bytes<'a>(
222222
}
223223

224224
let (signature, message) = expect_two!(token.rsplitn(2, |b| *b == b'.'));
225-
let (header, payload) = expect_two!(message.splitn(2, |b| *b == b'.'));
225+
let (payload, header) = expect_two!(message.rsplitn(2, |b| *b == b'.'));
226226
let header = Header::from_encoded(header)?;
227227

228228
if validation.validate_signature && !validation.algorithms.contains(&header.alg) {
@@ -250,46 +250,16 @@ fn verify_signature_bytes<'a>(
250250
/// company: String
251251
/// }
252252
///
253-
/// let token = "a.jwt.token".to_string();
253+
/// let token = "a.jwt.token";
254254
/// // Claims is a struct that implements Deserialize
255-
/// let token_message = decode::<Claims>(&token, &DecodingKey::from_secret("secret".as_ref()), &Validation::new(Algorithm::HS256));
255+
/// let token_message = decode::<Claims>(token, &DecodingKey::from_secret("secret".as_ref()), &Validation::new(Algorithm::HS256));
256256
/// ```
257257
pub fn decode<T: DeserializeOwned>(
258-
token: &str,
259-
key: &DecodingKey,
260-
validation: &Validation,
261-
) -> Result<TokenData<T>> {
262-
decode_bytes(token.as_bytes(), key, validation)
263-
}
264-
265-
/// Decode and validate a JWT
266-
///
267-
/// If the token or its signature is invalid or the claims fail validation, it will return an error.
268-
///
269-
/// This differs from decode() in the case that you only have bytes. By decoding as bytes you can
270-
/// avoid taking a pass over your bytes to validate them as a utf-8 string. Since the decoding and
271-
/// validation is all done in terms of bytes, the &str step is unnecessary.
272-
/// If you already have a &str, decode is more convenient. If you have bytes, consider using this.
273-
///
274-
/// ```rust
275-
/// use serde::{Deserialize, Serialize};
276-
/// use jsonwebtoken::{decode_bytes, DecodingKey, Validation, Algorithm};
277-
///
278-
/// #[derive(Debug, Serialize, Deserialize)]
279-
/// struct Claims {
280-
/// sub: String,
281-
/// company: String
282-
/// }
283-
///
284-
/// let token = b"a.jwt.token";
285-
/// // Claims is a struct that implements Deserialize
286-
/// let token_message = decode_bytes::<Claims>(token, &DecodingKey::from_secret("secret".as_ref()), &Validation::new(Algorithm::HS256));
287-
/// ```
288-
pub fn decode_bytes<T: DeserializeOwned>(
289-
token: &[u8],
258+
token: impl AsRef<[u8]>,
290259
key: &DecodingKey,
291260
validation: &Validation,
292261
) -> Result<TokenData<T>> {
262+
let token = token.as_ref();
293263
match verify_signature_bytes(token, key, validation) {
294264
Err(e) => Err(e),
295265
Ok((header, claims)) => {
@@ -309,26 +279,11 @@ pub fn decode_bytes<T: DeserializeOwned>(
309279
/// ```rust
310280
/// use jsonwebtoken::decode_header;
311281
///
312-
/// let token = "a.jwt.token".to_string();
313-
/// let header = decode_header(&token);
314-
/// ```
315-
pub fn decode_header(token: &str) -> Result<Header> {
316-
let (_, message) = expect_two!(token.rsplitn(2, '.'));
317-
let (_, header) = expect_two!(message.rsplitn(2, '.'));
318-
Header::from_encoded(header)
319-
}
320-
321-
/// Decode a JWT without any signature verification/validations and return its [Header](struct.Header.html).
322-
///
323-
/// If the token has an invalid format (ie 3 parts separated by a `.`), it will return an error.
324-
///
325-
/// ```rust
326-
/// use jsonwebtoken::decode_header_bytes;
327-
///
328-
/// let token = b"a.jwt.token";
329-
/// let header = decode_header_bytes(token);
282+
/// let token = "a.jwt.token";
283+
/// let header = decode_header(token);
330284
/// ```
331-
pub fn decode_header_bytes(token: &[u8]) -> Result<Header> {
285+
pub fn decode_header(token: impl AsRef<[u8]>) -> Result<Header> {
286+
let token = token.as_ref();
332287
let (_, message) = expect_two!(token.rsplitn(2, |b| *b == b'.'));
333288
let (_, header) = expect_two!(message.rsplitn(2, |b| *b == b'.'));
334289
Header::from_encoded(header)

src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ mod serialization;
1818
mod validation;
1919

2020
pub use algorithms::Algorithm;
21-
pub use decoding::{
22-
decode, decode_bytes, decode_header, decode_header_bytes, DecodingKey, TokenData,
23-
};
21+
pub use decoding::{decode, decode_header, DecodingKey, TokenData};
2422
pub use encoding::{encode, EncodingKey};
2523
pub use header::Header;
2624
pub use validation::{get_current_timestamp, Validation};

0 commit comments

Comments
 (0)