Skip to content

Commit 62643a7

Browse files
♻️ Refactored code
1 parent 6c515d0 commit 62643a7

File tree

2 files changed

+28
-37
lines changed

2 files changed

+28
-37
lines changed

src/main/java/org/rrajesh1979/utils/JWTUtil.java

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Map;
2222
import java.util.Objects;
2323

24+
import lombok.extern.slf4j.Slf4j;
2425
import org.javatuples.Pair;
2526
import org.json.JSONObject;
2627

@@ -29,7 +30,26 @@
2930
import io.jsonwebtoken.io.Encoders;
3031
import io.jsonwebtoken.security.Keys;
3132

33+
@Slf4j
3234
public class JWTUtil {
35+
private static Map<String, Object> algMap;
36+
static {
37+
algMap = new HashMap<>();
38+
algMap.put("HS256", SignatureAlgorithm.HS256);
39+
algMap.put("HS384", SignatureAlgorithm.HS384);
40+
algMap.put("HS512", SignatureAlgorithm.HS512);
41+
algMap.put("RS256", SignatureAlgorithm.RS256);
42+
algMap.put("RS384", SignatureAlgorithm.RS384);
43+
algMap.put("RS512", SignatureAlgorithm.RS512);
44+
algMap.put("ES256", SignatureAlgorithm.ES256);
45+
algMap.put("ES384", SignatureAlgorithm.ES384);
46+
algMap.put("ES512", SignatureAlgorithm.ES512);
47+
algMap.put("PS256", SignatureAlgorithm.PS256);
48+
algMap.put("PS384", SignatureAlgorithm.PS384);
49+
algMap.put("PS512", SignatureAlgorithm.PS512);
50+
algMap.put("", SignatureAlgorithm.HS256);
51+
}
52+
3353
public static Pair<String, String> createJWT(String typ, String alg, String userInput,
3454
String iss, String sub, String aud, boolean iat, long exp) {
3555

@@ -72,34 +92,8 @@ public static Pair<String, String> createJWT(String typ, String alg, String user
7292
}
7393

7494
private static Key signingKey(String algorithm) {
75-
switch (algorithm) {
76-
case "HS256":
77-
return Keys.secretKeyFor(SignatureAlgorithm.HS256);
78-
case "HS384":
79-
return Keys.secretKeyFor(SignatureAlgorithm.HS384);
80-
case "HS512":
81-
return Keys.secretKeyFor(SignatureAlgorithm.HS512);
82-
case "RS256":
83-
return Keys.secretKeyFor(SignatureAlgorithm.RS256);
84-
case "RS384":
85-
return Keys.secretKeyFor(SignatureAlgorithm.RS384);
86-
case "RS512":
87-
return Keys.secretKeyFor(SignatureAlgorithm.RS512);
88-
case "ES256":
89-
return Keys.secretKeyFor(SignatureAlgorithm.ES256);
90-
case "ES384":
91-
return Keys.secretKeyFor(SignatureAlgorithm.ES384);
92-
case "ES512":
93-
return Keys.secretKeyFor(SignatureAlgorithm.ES512);
94-
case "PS256":
95-
return Keys.secretKeyFor(SignatureAlgorithm.PS256);
96-
case "PS384":
97-
return Keys.secretKeyFor(SignatureAlgorithm.PS384);
98-
case "PS512":
99-
return Keys.secretKeyFor(SignatureAlgorithm.PS512);
100-
default:
101-
throw new IllegalArgumentException("Unsupported algorithm: " + algorithm);
102-
}
95+
SignatureAlgorithm signatureAlgorithm = (SignatureAlgorithm) algMap.get(algorithm);
96+
return Keys.secretKeyFor(signatureAlgorithm);
10397
}
10498

10599
public static Pair<String, String> decodeJWT(String jws, String key) {
@@ -115,12 +109,12 @@ public static void main(String[] args) {
115109
Pair<String, String> jwtAndKey = createJWT("JWT", "HS512", userInput,
116110
"rrajesh1979", "JWT Encoder", "Hello JWT", false, 0);
117111

118-
System.out.println("JWT :" + jwtAndKey.getValue0());
119-
System.out.println("Secret Key :" + jwtAndKey.getValue1());
112+
log.info("JWT :{}", jwtAndKey.getValue0());
113+
log.info("Secret Key :{}", jwtAndKey.getValue1());
120114

121115
Pair<String, String> decodedJwtAndKey = decodeJWT(jwtAndKey.getValue0(), jwtAndKey.getValue1());
122-
System.out.println("Decoded JWT Header:" + decodedJwtAndKey.getValue0());
123-
System.out.println("Decoded JWT Key :" + decodedJwtAndKey.getValue1());
116+
log.info("Decoded JWT Header :{}", decodedJwtAndKey.getValue0());
117+
log.info("Decoded JWT Key :{}", decodedJwtAndKey.getValue1());
124118
}
125119

126120
}

src/test/java/org/rrajesh1979/utils/JWTUtilTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616

1717
package org.rrajesh1979.utils;
1818

19+
import lombok.extern.slf4j.Slf4j;
1920
import org.javatuples.Pair;
2021
import org.junit.jupiter.api.DisplayName;
2122
import org.junit.jupiter.api.Test;
2223

2324
import static org.junit.jupiter.api.Assertions.*;
2425

26+
@Slf4j
2527
class JWTUtilTest {
2628

2729
@Test
@@ -35,12 +37,7 @@ void encodeDecodeJWTTest() {
3537
String jwt = jwtAndKey.getValue0();
3638
String key = jwtAndKey.getValue1();
3739

38-
System.out.println(jwt);
39-
System.out.println(key);
40-
4140
Pair<String, String> decodedJwtAndKey = JWTUtil.decodeJWT(jwt, key);
42-
System.out.println(decodedJwtAndKey.getValue0());
43-
System.out.println(decodedJwtAndKey.getValue1());
4441
assertEquals(decodedJwtAndKey.getValue0(), "{typ=JWT, alg=HS512}");
4542
assertEquals(decodedJwtAndKey.getValue1(), "{sub=JWT Encoder, aud=Hello JWT, name=Joe, iss=rrajesh1979, picture=https://example.com/image.png}");
4643
}

0 commit comments

Comments
 (0)