@@ -222,7 +222,7 @@ fn verify_signature_bytes<'a>(
222
222
}
223
223
224
224
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'.' ) ) ;
226
226
let header = Header :: from_encoded ( header) ?;
227
227
228
228
if validation. validate_signature && !validation. algorithms . contains ( & header. alg ) {
@@ -250,46 +250,16 @@ fn verify_signature_bytes<'a>(
250
250
/// company: String
251
251
/// }
252
252
///
253
- /// let token = "a.jwt.token".to_string() ;
253
+ /// let token = "a.jwt.token";
254
254
/// // 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));
256
256
/// ```
257
257
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 ] > ,
290
259
key : & DecodingKey ,
291
260
validation : & Validation ,
292
261
) -> Result < TokenData < T > > {
262
+ let token = token. as_ref ( ) ;
293
263
match verify_signature_bytes ( token, key, validation) {
294
264
Err ( e) => Err ( e) ,
295
265
Ok ( ( header, claims) ) => {
@@ -309,26 +279,11 @@ pub fn decode_bytes<T: DeserializeOwned>(
309
279
/// ```rust
310
280
/// use jsonwebtoken::decode_header;
311
281
///
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);
330
284
/// ```
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 ( ) ;
332
287
let ( _, message) = expect_two ! ( token. rsplitn( 2 , |b| * b == b'.' ) ) ;
333
288
let ( _, header) = expect_two ! ( message. rsplitn( 2 , |b| * b == b'.' ) ) ;
334
289
Header :: from_encoded ( header)
0 commit comments