@@ -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/// ```
272272pub 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.
336338fn 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 }
0 commit comments