@@ -223,7 +223,15 @@ mod tests {
223223 let encode_matches = encode_matcher. subcommand_matches ( "encode" ) . unwrap ( ) ;
224224 let encoded_token = encode_token ( & encode_matches) . unwrap ( ) ;
225225 let decode_matcher = config_options ( )
226- . get_matches_from_safe ( vec ! [ "jwt" , "decode" , "-S" , "1234567890" , & encoded_token] )
226+ . get_matches_from_safe ( vec ! [
227+ "jwt" ,
228+ "decode" ,
229+ "-S" ,
230+ "1234567890" ,
231+ "-A" ,
232+ "HS256" ,
233+ & encoded_token,
234+ ] )
227235 . unwrap ( ) ;
228236 let decode_matches = decode_matcher. subcommand_matches ( "decode" ) . unwrap ( ) ;
229237 let ( decoded_token, _, _) = decode_token ( & decode_matches) ;
@@ -257,7 +265,15 @@ mod tests {
257265 let encode_matches = encode_matcher. subcommand_matches ( "encode" ) . unwrap ( ) ;
258266 let encoded_token = encode_token ( & encode_matches) . unwrap ( ) ;
259267 let decode_matcher = config_options ( )
260- . get_matches_from_safe ( vec ! [ "jwt" , "decode" , "-S" , "1234567890" , & encoded_token] )
268+ . get_matches_from_safe ( vec ! [
269+ "jwt" ,
270+ "decode" ,
271+ "-S" ,
272+ "1234567890" ,
273+ "-A" ,
274+ "HS256" ,
275+ & encoded_token,
276+ ] )
261277 . unwrap ( ) ;
262278 let decode_matches = decode_matcher. subcommand_matches ( "decode" ) . unwrap ( ) ;
263279 let ( decoded_token, _, _) = decode_token ( & decode_matches) ;
@@ -279,7 +295,15 @@ mod tests {
279295 let encode_matches = encode_matcher. subcommand_matches ( "encode" ) . unwrap ( ) ;
280296 let encoded_token = encode_token ( & encode_matches) . unwrap ( ) ;
281297 let decode_matcher = config_options ( )
282- . get_matches_from_safe ( vec ! [ "jwt" , "decode" , "-S" , "1234567890" , & encoded_token] )
298+ . get_matches_from_safe ( vec ! [
299+ "jwt" ,
300+ "decode" ,
301+ "-S" ,
302+ "1234567890" ,
303+ "-A" ,
304+ "HS256" ,
305+ & encoded_token,
306+ ] )
283307 . unwrap ( ) ;
284308 let decode_matches = decode_matcher. subcommand_matches ( "decode" ) . unwrap ( ) ;
285309 let ( decoded_token, token_data, _) = decode_token ( & decode_matches) ;
@@ -299,7 +323,15 @@ mod tests {
299323 let encode_matches = encode_matcher. subcommand_matches ( "encode" ) . unwrap ( ) ;
300324 let encoded_token = encode_token ( & encode_matches) . unwrap ( ) ;
301325 let decode_matcher = config_options ( )
302- . get_matches_from_safe ( vec ! [ "jwt" , "decode" , "-S" , "1234567890" , & encoded_token] )
326+ . get_matches_from_safe ( vec ! [
327+ "jwt" ,
328+ "decode" ,
329+ "-S" ,
330+ "1234567890" ,
331+ "-A" ,
332+ "HS256" ,
333+ & encoded_token,
334+ ] )
303335 . unwrap ( ) ;
304336 let decode_matches = decode_matcher. subcommand_matches ( "decode" ) . unwrap ( ) ;
305337 let ( decoded_token, _, _) = decode_token ( & decode_matches) ;
@@ -328,7 +360,15 @@ mod tests {
328360 let encode_matches = encode_matcher. subcommand_matches ( "encode" ) . unwrap ( ) ;
329361 let encoded_token = encode_token ( & encode_matches) . unwrap ( ) ;
330362 let decode_matcher = config_options ( )
331- . get_matches_from_safe ( vec ! [ "jwt" , "decode" , "-S" , "1234567890" , & encoded_token] )
363+ . get_matches_from_safe ( vec ! [
364+ "jwt" ,
365+ "decode" ,
366+ "-S" ,
367+ "1234567890" ,
368+ "-A" ,
369+ "HS256" ,
370+ & encoded_token,
371+ ] )
332372 . unwrap ( ) ;
333373 let decode_matches = decode_matcher. subcommand_matches ( "decode" ) . unwrap ( ) ;
334374 let ( decoded_token, _, _) = decode_token ( & decode_matches) ;
@@ -356,7 +396,15 @@ mod tests {
356396 let encode_matches = encode_matcher. subcommand_matches ( "encode" ) . unwrap ( ) ;
357397 let encoded_token = encode_token ( & encode_matches) . unwrap ( ) ;
358398 let decode_matcher = config_options ( )
359- . get_matches_from_safe ( vec ! [ "jwt" , "decode" , "-S" , "1234567890" , & encoded_token] )
399+ . get_matches_from_safe ( vec ! [
400+ "jwt" ,
401+ "decode" ,
402+ "-S" ,
403+ "1234567890" ,
404+ "-A" ,
405+ "HS256" ,
406+ & encoded_token,
407+ ] )
360408 . unwrap ( ) ;
361409 let decode_matches = decode_matcher. subcommand_matches ( "decode" ) . unwrap ( ) ;
362410 let ( decoded_token, _, _) = decode_token ( & decode_matches) ;
@@ -378,7 +426,15 @@ mod tests {
378426 let encode_matches = encode_matcher. subcommand_matches ( "encode" ) . unwrap ( ) ;
379427 let encoded_token = encode_token ( & encode_matches) . unwrap ( ) ;
380428 let decode_matcher = config_options ( )
381- . get_matches_from_safe ( vec ! [ "jwt" , "decode" , "-S" , "1234567890" , & encoded_token] )
429+ . get_matches_from_safe ( vec ! [
430+ "jwt" ,
431+ "decode" ,
432+ "-S" ,
433+ "1234567890" ,
434+ "-A" ,
435+ "HS512" ,
436+ & encoded_token,
437+ ] )
382438 . unwrap ( ) ;
383439 let decode_matches = decode_matcher. subcommand_matches ( "decode" ) . unwrap ( ) ;
384440 let ( decoded_token, _, _) = decode_token ( & decode_matches) ;
@@ -399,6 +455,8 @@ mod tests {
399455 "decode" ,
400456 "-S" ,
401457 "1234567890" ,
458+ "-A" ,
459+ "HS256" ,
402460 "--ignore-exp" ,
403461 & encoded_token,
404462 ] )
@@ -424,7 +482,15 @@ mod tests {
424482 let encode_matches = encode_matcher. subcommand_matches ( "encode" ) . unwrap ( ) ;
425483 let encoded_token = encode_token ( & encode_matches) . unwrap ( ) ;
426484 let decode_matcher = config_options ( )
427- . get_matches_from_safe ( vec ! [ "jwt" , "decode" , "-S" , "1234567890" , & encoded_token] )
485+ . get_matches_from_safe ( vec ! [
486+ "jwt" ,
487+ "decode" ,
488+ "-S" ,
489+ "1234567890" ,
490+ "-A" ,
491+ "HS256" ,
492+ & encoded_token,
493+ ] )
428494 . unwrap ( ) ;
429495 let decode_matches = decode_matcher. subcommand_matches ( "decode" ) . unwrap ( ) ;
430496 let ( decoded_token, _, _) = decode_token ( & decode_matches) ;
@@ -460,7 +526,15 @@ mod tests {
460526 let encode_matches = encode_matcher. subcommand_matches ( "encode" ) . unwrap ( ) ;
461527 let encoded_token = encode_token ( & encode_matches) . unwrap ( ) ;
462528 let decode_matcher = config_options ( )
463- . get_matches_from_safe ( vec ! [ "jwt" , "decode" , "-S" , "1234567890" , & encoded_token] )
529+ . get_matches_from_safe ( vec ! [
530+ "jwt" ,
531+ "decode" ,
532+ "-S" ,
533+ "1234567890" ,
534+ "-A" ,
535+ "HS256" ,
536+ & encoded_token,
537+ ] )
464538 . unwrap ( ) ;
465539 let decode_matches = decode_matcher. subcommand_matches ( "decode" ) . unwrap ( ) ;
466540 let ( decoded_token, _, _) = decode_token ( & decode_matches) ;
@@ -597,6 +671,108 @@ mod tests {
597671 assert ! ( result. is_ok( ) ) ;
598672 }
599673
674+ #[ test]
675+ fn encodes_and_decodes_a_token_with_multiple_algorithms ( ) {
676+ let body: String = "{\" field\" :\" value\" }" . to_string ( ) ;
677+ let encode_matcher = config_options ( )
678+ . get_matches_from_safe ( vec ! [
679+ "jwt" ,
680+ "encode" ,
681+ "-A" ,
682+ "HS256" ,
683+ "--exp" ,
684+ "-S" ,
685+ "1234567890" ,
686+ & body,
687+ ] )
688+ . unwrap ( ) ;
689+ let encode_matches = encode_matcher. subcommand_matches ( "encode" ) . unwrap ( ) ;
690+ let encoded_token = encode_token ( & encode_matches) . unwrap ( ) ;
691+ let decode_matcher = config_options ( )
692+ . get_matches_from_safe ( vec ! [
693+ "jwt" ,
694+ "decode" ,
695+ "-S" ,
696+ "1234567890" ,
697+ "-A" ,
698+ "HS256,HS384,HS512" ,
699+ & encoded_token,
700+ ] )
701+ . unwrap ( ) ;
702+ let decode_matches = decode_matcher. subcommand_matches ( "decode" ) . unwrap ( ) ;
703+ let ( result, _, _) = decode_token ( & decode_matches) ;
704+
705+ assert ! ( result. is_ok( ) ) ;
706+ }
707+
708+ #[ test]
709+ fn encodes_and_decodes_a_token_with_invalid_algorithms_family ( ) {
710+ let body: String = "{\" field\" :\" value\" }" . to_string ( ) ;
711+ let encode_matcher = config_options ( )
712+ . get_matches_from_safe ( vec ! [
713+ "jwt" ,
714+ "encode" ,
715+ "-A" ,
716+ "HS256" ,
717+ "--exp" ,
718+ "-S" ,
719+ "1234567890" ,
720+ & body,
721+ ] )
722+ . unwrap ( ) ;
723+ let encode_matches = encode_matcher. subcommand_matches ( "encode" ) . unwrap ( ) ;
724+ let encoded_token = encode_token ( & encode_matches) . unwrap ( ) ;
725+ let decode_matcher = config_options ( )
726+ . get_matches_from_safe ( vec ! [
727+ "jwt" ,
728+ "decode" ,
729+ "-S" ,
730+ "1234567890" ,
731+ "-A" ,
732+ "RS256,RS384,RS512" , // invalid algorithm family
733+ & encoded_token,
734+ ] )
735+ . unwrap ( ) ;
736+ let decode_matches = decode_matcher. subcommand_matches ( "decode" ) . unwrap ( ) ;
737+ let ( result, _, _) = decode_token ( & decode_matches) ;
738+
739+ assert ! ( result. is_err( ) ) ;
740+ }
741+
742+ #[ test]
743+ fn encodes_and_decodes_a_token_with_mixed_algorithms_family ( ) {
744+ let body: String = "{\" field\" :\" value\" }" . to_string ( ) ;
745+ let encode_matcher = config_options ( )
746+ . get_matches_from_safe ( vec ! [
747+ "jwt" ,
748+ "encode" ,
749+ "-A" ,
750+ "HS256" ,
751+ "--exp" ,
752+ "-S" ,
753+ "1234567890" ,
754+ & body,
755+ ] )
756+ . unwrap ( ) ;
757+ let encode_matches = encode_matcher. subcommand_matches ( "encode" ) . unwrap ( ) ;
758+ let encoded_token = encode_token ( & encode_matches) . unwrap ( ) ;
759+ let decode_matcher = config_options ( )
760+ . get_matches_from_safe ( vec ! [
761+ "jwt" ,
762+ "decode" ,
763+ "-S" ,
764+ "1234567890" ,
765+ "-A" ,
766+ "HS256,RS512" , // algorithms from incompatible algorithm families
767+ & encoded_token,
768+ ] )
769+ . unwrap ( ) ;
770+ let decode_matches = decode_matcher. subcommand_matches ( "decode" ) . unwrap ( ) ;
771+ let ( result, _, _) = decode_token ( & decode_matches) ;
772+
773+ assert ! ( result. is_err( ) ) ;
774+ }
775+
600776 #[ test]
601777 fn encodes_and_decodes_an_rsa_token_using_key_from_file ( ) {
602778 let body: String = "{\" field\" :\" value\" }" . to_string ( ) ;
@@ -705,6 +881,8 @@ mod tests {
705881 "decode" ,
706882 "-S" ,
707883 "1234567890" ,
884+ "-A" ,
885+ "HS256" ,
708886 "--iso8601" ,
709887 & encoded_token,
710888 ] )
0 commit comments