@@ -790,4 +790,145 @@ public void shouldSkipClaimValidationsIfNoClaimsRequired() throws Exception {
790
790
791
791
assertThat (jwt , is (notNullValue ()));
792
792
}
793
+
794
+ @ Test
795
+ public void shouldThrowWhenVerifyingClaimPresenceButClaimNotPresent () {
796
+ exception .expect (InvalidClaimException .class );
797
+ exception .expectMessage ("The Claim 'missing' is not present in the JWT." );
798
+
799
+ String jwt = JWTCreator .init ()
800
+ .withClaim ("custom" , "" )
801
+ .sign (Algorithm .HMAC256 ("secret" ));
802
+
803
+ JWTVerifier verifier = JWTVerifier .init (Algorithm .HMAC256 ("secret" ))
804
+ .withClaimPresence ("missing" )
805
+ .build ();
806
+
807
+ verifier .verify (jwt );
808
+ }
809
+
810
+ @ Test
811
+ public void shouldThrowWhenVerifyingClaimPresenceWhenClaimNameIsNull () {
812
+ exception .expect (IllegalArgumentException .class );
813
+ exception .expectMessage ("The Custom Claim's name can't be null." );
814
+
815
+ String jwt = JWTCreator .init ()
816
+ .withClaim ("custom" , "value" )
817
+ .sign (Algorithm .HMAC256 ("secret" ));
818
+
819
+ JWTVerifier .init (Algorithm .HMAC256 ("secret" ))
820
+ .withClaimPresence (null );
821
+ }
822
+
823
+ @ Test
824
+ public void shouldVerifyStringClaimPresence () {
825
+ String jwt = JWTCreator .init ()
826
+ .withClaim ("custom" , "" )
827
+ .sign (Algorithm .HMAC256 ("secret" ));
828
+
829
+ JWTVerifier verifier = JWTVerifier .init (Algorithm .HMAC256 ("secret" ))
830
+ .withClaimPresence ("custom" )
831
+ .build ();
832
+
833
+ DecodedJWT decodedJWT = verifier .verify (jwt );
834
+ assertThat (decodedJWT , is (notNullValue ()));
835
+ }
836
+
837
+ @ Test
838
+ public void shouldVerifyBooleanClaimPresence () {
839
+ String jwt = JWTCreator .init ()
840
+ .withClaim ("custom" , true )
841
+ .sign (Algorithm .HMAC256 ("secret" ));
842
+
843
+ JWTVerifier verifier = JWTVerifier .init (Algorithm .HMAC256 ("secret" ))
844
+ .withClaimPresence ("custom" )
845
+ .build ();
846
+
847
+ DecodedJWT decodedJWT = verifier .verify (jwt );
848
+ assertThat (decodedJWT , is (notNullValue ()));
849
+ }
850
+
851
+ @ Test
852
+ public void shouldVerifyIntegerClaimPresence () {
853
+ String jwt = JWTCreator .init ()
854
+ .withClaim ("custom" , 123 )
855
+ .sign (Algorithm .HMAC256 ("secret" ));
856
+
857
+ JWTVerifier verifier = JWTVerifier .init (Algorithm .HMAC256 ("secret" ))
858
+ .withClaimPresence ("custom" )
859
+ .build ();
860
+
861
+ DecodedJWT decodedJWT = verifier .verify (jwt );
862
+ assertThat (decodedJWT , is (notNullValue ()));
863
+ }
864
+
865
+ @ Test
866
+ public void shouldVerifyLongClaimPresence () {
867
+ String jwt = JWTCreator .init ()
868
+ .withClaim ("custom" , 922337203685477600L )
869
+ .sign (Algorithm .HMAC256 ("secret" ));
870
+
871
+ JWTVerifier verifier = JWTVerifier .init (Algorithm .HMAC256 ("secret" ))
872
+ .withClaimPresence ("custom" )
873
+ .build ();
874
+
875
+ DecodedJWT decodedJWT = verifier .verify (jwt );
876
+ assertThat (decodedJWT , is (notNullValue ()));
877
+ }
878
+
879
+ @ Test
880
+ public void shouldVerifyDoubleClaimPresence () {
881
+ String jwt = JWTCreator .init ()
882
+ .withClaim ("custom" , 12.34 )
883
+ .sign (Algorithm .HMAC256 ("secret" ));
884
+
885
+ JWTVerifier verifier = JWTVerifier .init (Algorithm .HMAC256 ("secret" ))
886
+ .withClaimPresence ("custom" )
887
+ .build ();
888
+
889
+ DecodedJWT decodedJWT = verifier .verify (jwt );
890
+ assertThat (decodedJWT , is (notNullValue ()));
891
+ }
892
+
893
+ @ Test
894
+ public void shouldVerifyListClaimPresence () {
895
+ String jwt = JWTCreator .init ()
896
+ .withClaim ("custom" , Collections .singletonList ("item" ))
897
+ .sign (Algorithm .HMAC256 ("secret" ));
898
+
899
+ JWTVerifier verifier = JWTVerifier .init (Algorithm .HMAC256 ("secret" ))
900
+ .withClaimPresence ("custom" )
901
+ .build ();
902
+
903
+ DecodedJWT decodedJWT = verifier .verify (jwt );
904
+ assertThat (decodedJWT , is (notNullValue ()));
905
+ }
906
+
907
+ @ Test
908
+ public void shouldVerifyMapClaimPresence () {
909
+ String jwt = JWTCreator .init ()
910
+ .withClaim ("custom" , Collections .singletonMap ("key" , "value" ))
911
+ .sign (Algorithm .HMAC256 ("secret" ));
912
+
913
+ JWTVerifier verifier = JWTVerifier .init (Algorithm .HMAC256 ("secret" ))
914
+ .withClaimPresence ("custom" )
915
+ .build ();
916
+
917
+ DecodedJWT decodedJWT = verifier .verify (jwt );
918
+ assertThat (decodedJWT , is (notNullValue ()));
919
+ }
920
+
921
+ @ Test
922
+ public void shouldVerifyStandardClaimPresence () {
923
+ String jwt = JWTCreator .init ()
924
+ .withClaim ("aud" , "any value" )
925
+ .sign (Algorithm .HMAC256 ("secret" ));
926
+
927
+ JWTVerifier verifier = JWTVerifier .init (Algorithm .HMAC256 ("secret" ))
928
+ .withClaimPresence ("aud" )
929
+ .build ();
930
+
931
+ DecodedJWT decodedJWT = verifier .verify (jwt );
932
+ assertThat (decodedJWT , is (notNullValue ()));
933
+ }
793
934
}
0 commit comments