11package io .jsonwebtoken .jjwtfun .controller ;
22
33import io .jsonwebtoken .JwtBuilder ;
4+ import io .jsonwebtoken .JwtException ;
45import io .jsonwebtoken .Jwts ;
56import io .jsonwebtoken .SignatureAlgorithm ;
7+ import io .jsonwebtoken .jjwtfun .model .JwtResponse ;
68import org .springframework .beans .factory .annotation .Value ;
79import org .springframework .web .bind .annotation .RequestBody ;
810import org .springframework .web .bind .annotation .RequestMapping ;
1618import static org .springframework .web .bind .annotation .RequestMethod .POST ;
1719
1820@ RestController
19- public class DynamicJWTController {
21+ public class DynamicJWTController extends BaseController {
2022 @ Value ("#{ @environment['jjwtfun.secret'] ?: 'secret' }" )
2123 String secret ;
2224
2325 @ RequestMapping (value = "/dynamic-builder-general" , method = POST )
24- public String dynamicBuilderGeneric (@ RequestBody Map <String , Object > claims ) throws UnsupportedEncodingException {
25- return Jwts .builder ()
26+ public JwtResponse dynamicBuilderGeneric (@ RequestBody Map <String , Object > claims ) throws UnsupportedEncodingException {
27+ String jws = Jwts .builder ()
2628 .setClaims (claims )
2729 .signWith (
2830 SignatureAlgorithm .HS256 ,
2931 secret .getBytes ("UTF-8" )
3032 )
3133 .compact ();
34+ return new JwtResponse (jws );
3235 }
3336
3437 @ RequestMapping (value = "/dynamic-builder-specific" , method = POST )
35- public String dynamicBuilderSpecific (@ RequestBody Map <String , Object > claims ) throws UnsupportedEncodingException {
38+ public JwtResponse dynamicBuilderSpecific (@ RequestBody Map <String , Object > claims ) throws UnsupportedEncodingException {
3639 JwtBuilder builder = Jwts .builder ();
3740
3841 claims .forEach ((key , value ) -> {
3942 switch (key ) {
4043 case "iss" :
41- builder .setIssuer ((String )value );
44+ ensureType (key , value , String .class );
45+ builder .setIssuer ((String ) value );
4246 break ;
4347 case "sub" :
44- builder .setSubject ((String )value );
48+ ensureType (key , value , String .class );
49+ builder .setSubject ((String ) value );
4550 break ;
4651 case "aud" :
47- builder .setAudience ((String )value );
52+ ensureType (key , value , String .class );
53+ builder .setAudience ((String ) value );
4854 break ;
4955 case "exp" :
50- builder .setExpiration (Date .from (Instant .ofEpochSecond (Long .parseLong ((String )value ))));
56+ value = ensureType (key , value , Long .class );
57+ builder .setExpiration (Date .from (Instant .ofEpochSecond ((Long ) value )));
5158 break ;
5259 case "nbf" :
53- builder .setNotBefore (Date .from (Instant .ofEpochSecond (Long .parseLong ((String )value ))));
60+ value = ensureType (key , value , Long .class );
61+ builder .setNotBefore (Date .from (Instant .ofEpochSecond ((Long ) value )));
5462 break ;
5563 case "iat" :
56- builder .setIssuedAt (Date .from (Instant .ofEpochSecond (Long .parseLong ((String )value ))));
64+ value = ensureType (key , value , Long .class );
65+ builder .setIssuedAt (Date .from (Instant .ofEpochSecond ((Long ) value )));
5766 break ;
5867 case "jti" :
59- builder .setId ((String )value );
68+ ensureType (key , value , String .class );
69+ builder .setId ((String ) value );
6070 break ;
6171 default :
6272 builder .claim (key , value );
@@ -65,6 +75,23 @@ public String dynamicBuilderSpecific(@RequestBody Map<String, Object> claims) th
6575
6676 builder .signWith (SignatureAlgorithm .HS256 , secret .getBytes ("UTF-8" ));
6777
68- return builder .compact ();
78+ return new JwtResponse (builder .compact ());
79+ }
80+
81+ private Object ensureType (String registeredClaim , Object value , Class expectedType ) {
82+ // we want to promote Integers to Longs in this case
83+ if (expectedType == Long .class && value instanceof Integer ) {
84+ value = ((Integer ) value ).longValue ();
85+ }
86+
87+ boolean isCorrectType = expectedType .isInstance (value );
88+
89+ if (!isCorrectType ) {
90+ String msg = "Expected type: " + expectedType .getCanonicalName () + " for registered claim: '" +
91+ registeredClaim + "', but got value: " + value + " of type: " + value .getClass ().getCanonicalName ();
92+ throw new JwtException (msg );
93+ }
94+
95+ return value ;
6996 }
7097}
0 commit comments