@@ -5,6 +5,7 @@ use jsonwebtoken::{
55 decode, decode_header, encode, Algorithm , DecodingKey , EncodingKey , Header , Validation ,
66} ;
77use serde:: { Deserialize , Serialize } ;
8+ use std:: collections:: HashMap ;
89use time:: OffsetDateTime ;
910use wasm_bindgen_test:: wasm_bindgen_test;
1011
@@ -51,6 +52,56 @@ fn encode_with_custom_header() {
5152 . unwrap ( ) ;
5253 assert_eq ! ( my_claims, token_data. claims) ;
5354 assert_eq ! ( "kid" , token_data. header. kid. unwrap( ) ) ;
55+ assert ! ( token_data. header. extras. is_empty( ) ) ;
56+ }
57+
58+ #[ test]
59+ #[ wasm_bindgen_test]
60+ fn encode_with_extra_custom_header ( ) {
61+ let my_claims = Claims {
62+ sub : "[email protected] " . to_string ( ) , 63+ company : "ACME" . to_string ( ) ,
64+ exp : OffsetDateTime :: now_utc ( ) . unix_timestamp ( ) + 10000 ,
65+ } ;
66+ let mut extras = HashMap :: with_capacity ( 1 ) ;
67+ extras. insert ( "custom" . to_string ( ) , "header" . to_string ( ) ) ;
68+ let header = Header { kid : Some ( "kid" . to_string ( ) ) , extras, ..Default :: default ( ) } ;
69+ let token = encode ( & header, & my_claims, & EncodingKey :: from_secret ( b"secret" ) ) . unwrap ( ) ;
70+ let token_data = decode :: < Claims > (
71+ & token,
72+ & DecodingKey :: from_secret ( b"secret" ) ,
73+ & Validation :: new ( Algorithm :: HS256 ) ,
74+ )
75+ . unwrap ( ) ;
76+ assert_eq ! ( my_claims, token_data. claims) ;
77+ assert_eq ! ( "kid" , token_data. header. kid. unwrap( ) ) ;
78+ assert_eq ! ( "header" , token_data. header. extras. get( "custom" ) . unwrap( ) . as_str( ) ) ;
79+ }
80+
81+ #[ test]
82+ #[ wasm_bindgen_test]
83+ fn encode_with_multiple_extra_custom_headers ( ) {
84+ let my_claims = Claims {
85+ sub : "[email protected] " . to_string ( ) , 86+ company : "ACME" . to_string ( ) ,
87+ exp : OffsetDateTime :: now_utc ( ) . unix_timestamp ( ) + 10000 ,
88+ } ;
89+ let mut extras = HashMap :: with_capacity ( 2 ) ;
90+ extras. insert ( "custom1" . to_string ( ) , "header1" . to_string ( ) ) ;
91+ extras. insert ( "custom2" . to_string ( ) , "header2" . to_string ( ) ) ;
92+ let header = Header { kid : Some ( "kid" . to_string ( ) ) , extras, ..Default :: default ( ) } ;
93+ let token = encode ( & header, & my_claims, & EncodingKey :: from_secret ( b"secret" ) ) . unwrap ( ) ;
94+ let token_data = decode :: < Claims > (
95+ & token,
96+ & DecodingKey :: from_secret ( b"secret" ) ,
97+ & Validation :: new ( Algorithm :: HS256 ) ,
98+ )
99+ . unwrap ( ) ;
100+ assert_eq ! ( my_claims, token_data. claims) ;
101+ assert_eq ! ( "kid" , token_data. header. kid. unwrap( ) ) ;
102+ let extras = token_data. header . extras ;
103+ assert_eq ! ( "header1" , extras. get( "custom1" ) . unwrap( ) . as_str( ) ) ;
104+ assert_eq ! ( "header2" , extras. get( "custom2" ) . unwrap( ) . as_str( ) ) ;
54105}
55106
56107#[ test]
@@ -86,6 +137,25 @@ fn decode_token() {
86137 claims. unwrap ( ) ;
87138}
88139
140+ #[ test]
141+ #[ wasm_bindgen_test]
142+ fn decode_token_custom_headers ( ) {
143+ let token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6ImtpZCIsImN1c3RvbTEiOiJoZWFkZXIxIiwiY3VzdG9tMiI6ImhlYWRlcjIifQ.eyJzdWIiOiJiQGIuY29tIiwiY29tcGFueSI6IkFDTUUiLCJleHAiOjI1MzI1MjQ4OTF9.FtOHsoKcNH3SriK3tnR-uWJg4UV4FkOzvq_JCfLngfU" ;
144+ let claims = decode :: < Claims > (
145+ token,
146+ & DecodingKey :: from_secret ( b"secret" ) ,
147+ & Validation :: new ( Algorithm :: HS256 ) ,
148+ )
149+ . unwrap ( ) ;
150+ let my_claims =
151+ Claims { sub : "[email protected] " . to_string ( ) , company : "ACME" . to_string ( ) , exp : 2532524891 } ; 152+ assert_eq ! ( my_claims, claims. claims) ;
153+ assert_eq ! ( "kid" , claims. header. kid. unwrap( ) ) ;
154+ let extras = claims. header . extras ;
155+ assert_eq ! ( "header1" , extras. get( "custom1" ) . unwrap( ) . as_str( ) ) ;
156+ assert_eq ! ( "header2" , extras. get( "custom2" ) . unwrap( ) . as_str( ) ) ;
157+ }
158+
89159#[ test]
90160#[ wasm_bindgen_test]
91161#[ should_panic( expected = "InvalidToken" ) ]
0 commit comments