1818 */
1919package org .apache .bigtop .manager .server .utils ;
2020
21+ import org .apache .bigtop .manager .server .config .JwtProperties ;
22+
2123import org .junit .jupiter .api .Test ;
2224
2325import com .auth0 .jwt .JWT ;
3537
3638public class JWTUtilsTest {
3739
40+ private static JWTUtils newJwtUtilsWithDevSecretAllowed () {
41+ JwtProperties props = new JwtProperties ();
42+ props .setAllowDefaultSecret (true );
43+ props .setIssuer ("bigtop-manager" );
44+ props .setAudience ("bigtop-manager" );
45+ props .setExpirationDays (7 );
46+ return new JWTUtils (props );
47+ }
48+
3849 @ Test
3950 public void testGenerateTokenNormal () {
51+ JWTUtils jwtUtils = newJwtUtilsWithDevSecretAllowed ();
52+
4053 Long id = 1L ;
4154 String username = "testUser" ;
4255 Integer tokenVersion = 1 ;
43- String token = JWTUtils .generateToken (id , username , tokenVersion );
56+ String token = jwtUtils .generateToken (id , username , tokenVersion );
4457 assertNotNull (token );
4558
46- DecodedJWT decodedJWT = JWTUtils .resolveToken (token );
59+ DecodedJWT decodedJWT = jwtUtils .resolveToken (token );
4760 assertEquals (id , decodedJWT .getClaim (JWTUtils .CLAIM_ID ).asLong ());
4861 assertEquals (username , decodedJWT .getClaim (JWTUtils .CLAIM_USERNAME ).asString ());
4962 assertEquals (
@@ -52,40 +65,76 @@ public void testGenerateTokenNormal() {
5265
5366 @ Test
5467 public void testResolveTokenExpired () {
68+ JWTUtils jwtUtils = newJwtUtilsWithDevSecretAllowed ();
69+
5570 Long id = 2L ;
5671 String username = "expiredUser" ;
5772 Calendar calendar = Calendar .getInstance ();
5873 calendar .add (Calendar .HOUR_OF_DAY , -1 );
5974 Date date = calendar .getTime ();
6075
6176 String token = JWT .create ()
77+ .withIssuer ("bigtop-manager" )
78+ .withAudience ("bigtop-manager" )
79+ .withIssuedAt (new Date ())
6280 .withClaim (JWTUtils .CLAIM_ID , id )
6381 .withClaim (JWTUtils .CLAIM_USERNAME , username )
6482 .withExpiresAt (date )
65- .sign (Algorithm .HMAC256 (JWTUtils .SIGN ));
83+ .sign (Algorithm .HMAC256 (JWTUtils .DEFAULT_DEV_SECRET ));
6684
67- assertThrows (JWTVerificationException .class , () -> JWTUtils .resolveToken (token ));
85+ assertThrows (JWTVerificationException .class , () -> jwtUtils .resolveToken (token ));
6886 }
6987
7088 @ Test
7189 public void testResolveTokenIllegal () {
90+ JWTUtils jwtUtils = newJwtUtilsWithDevSecretAllowed ();
91+
7292 String illegalToken =
7393 "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" ;
74- assertThrows (JWTVerificationException .class , () -> JWTUtils .resolveToken (illegalToken ));
94+ assertThrows (JWTVerificationException .class , () -> jwtUtils .resolveToken (illegalToken ));
7595 }
7696
7797 @ Test
7898 public void testResolveTokenWrongFormat () {
99+ JWTUtils jwtUtils = newJwtUtilsWithDevSecretAllowed ();
100+
79101 String wrongFormatToken = "wrong_format_token" ;
80- assertThrows (JWTDecodeException .class , () -> JWTUtils .resolveToken (wrongFormatToken ));
102+ assertThrows (JWTDecodeException .class , () -> jwtUtils .resolveToken (wrongFormatToken ));
81103 }
82104
83105 @ Test
84106 public void testGenerateTokenUsernameEmpty () {
85- String token = JWTUtils .generateToken (1L , "" , 1 );
107+ JWTUtils jwtUtils = newJwtUtilsWithDevSecretAllowed ();
108+
109+ String token = jwtUtils .generateToken (1L , "" , 1 );
86110 assertNotNull (token );
87111
88- DecodedJWT decodedJWT = JWTUtils .resolveToken (token );
112+ DecodedJWT decodedJWT = jwtUtils .resolveToken (token );
89113 assertEquals ("" , decodedJWT .getClaim (JWTUtils .CLAIM_USERNAME ).asString ());
90114 }
115+
116+ @ Test
117+ public void testResolveTokenMissingIatRejected () {
118+ JWTUtils jwtUtils = newJwtUtilsWithDevSecretAllowed ();
119+
120+ String token = JWT .create ()
121+ .withIssuer ("bigtop-manager" )
122+ .withAudience ("bigtop-manager" )
123+ .withClaim (JWTUtils .CLAIM_ID , 1L )
124+ .withClaim (JWTUtils .CLAIM_TOKEN_VERSION , 1 )
125+ .withExpiresAt (new Date (System .currentTimeMillis () + 60_000 ))
126+ // intentionally no iat
127+ .sign (Algorithm .HMAC256 (JWTUtils .DEFAULT_DEV_SECRET ));
128+
129+ assertThrows (JWTVerificationException .class , () -> jwtUtils .resolveToken (token ));
130+ }
131+
132+ @ Test
133+ public void testSecretRequiredByDefault () {
134+ JwtProperties props = new JwtProperties ();
135+ props .setAllowDefaultSecret (false );
136+ JWTUtils jwtUtils = new JWTUtils (props );
137+
138+ assertThrows (IllegalStateException .class , () -> jwtUtils .generateToken (1L , "u" , 1 ));
139+ }
91140}
0 commit comments