Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Commit 2429a46

Browse files
committed
HS256 secret use b64 encoding
1 parent cee9a53 commit 2429a46

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

src/java/main/com/topcoder/direct/services/view/interceptors/AuthenticationInterceptor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ public String intercept(ActionInvocation invocation) throws Exception {
292292

293293
JWTToken jwtToken = null;
294294
try {
295-
jwtToken = new JWTToken(jwtCookie.getValue(),DirectProperties.CLIENT_SECRET_AUTH0, DirectProperties.JWT_VALID_ISSUERS);
295+
jwtToken = new JWTToken(jwtCookie.getValue(),DirectProperties.CLIENT_SECRET_AUTH0,
296+
DirectProperties.JWT_VALID_ISSUERS, new JWTToken.Base64SecretEncoder());
296297
} catch (TokenExpiredException e) {
297298
//refresh token here
298299
//redirect to loginpage for now

src/java/main/com/topcoder/direct/services/view/processor/security/LoginProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.topcoder.direct.services.view.util.DirectProperties;
1212
import com.topcoder.direct.services.view.util.DirectUtils;
1313
import com.topcoder.direct.services.view.util.jwt.DirectJWTSigner;
14+
import com.topcoder.direct.services.view.util.jwt.JWTToken;
1415
import com.topcoder.security.TCSubject;
1516
import com.topcoder.security.login.AuthenticationException;
1617
import com.topcoder.security.login.LoginRemote;
@@ -75,7 +76,6 @@ public class LoginProcessor implements RequestProcessor<LoginAction> {
7576

7677
static {
7778
JWT_OPTIONS = new DirectJWTSigner.Options();
78-
JWT_OPTIONS.setAlgorithm(Algorithm.HMAC256(DirectProperties.CLIENT_SECRET_AUTH0.getBytes()));
7979
JWT_OPTIONS.setExpirySeconds(DirectProperties.JWT_EXPIRATION_SECONDS);
8080
JWT_OPTIONS.setIssuedAt(true);
8181
}

src/java/main/com/topcoder/direct/services/view/processor/security/MockLoginProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.topcoder.direct.services.view.util.DirectProperties;
1212
import com.topcoder.direct.services.view.util.DirectUtils;
1313
import com.topcoder.direct.services.view.util.jwt.DirectJWTSigner;
14+
import com.topcoder.direct.services.view.util.jwt.JWTToken;
1415
import com.topcoder.security.RolePrincipal;
1516
import com.topcoder.security.TCPrincipal;
1617
import com.topcoder.security.TCSubject;
@@ -99,7 +100,6 @@ public class MockLoginProcessor implements RequestProcessor<LoginAction> {
99100

100101
static {
101102
JWT_OPTIONS = new DirectJWTSigner.Options();
102-
JWT_OPTIONS.setAlgorithm(Algorithm.HMAC256(DirectProperties.CLIENT_SECRET_AUTH0.getBytes()));
103103
JWT_OPTIONS.setExpirySeconds(DirectProperties.JWT_EXPIRATION_SECONDS);
104104
JWT_OPTIONS.setIssuedAt(true);
105105
}

src/java/main/com/topcoder/direct/services/view/util/jwt/DirectJWTSigner.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,30 @@ public class DirectJWTSigner {
3636
private final String secret;
3737

3838
/**
39-
* Create the JWT signer with the base64 encoded secret.
39+
* Secret encoder
40+
*/
41+
private JWTToken.SecretEncoder secretEncoder = new JWTToken.Base64SecretEncoder();
42+
43+
/**
44+
* Create the JWT signer
4045
*
41-
* @param secret the base64 encoded secret.
46+
* @param secret secret.
4247
*/
4348
public DirectJWTSigner(String secret) {
49+
this(secret, null);
50+
}
51+
52+
/**
53+
* Create the JWT signer with specific encoder
54+
*
55+
* @param secret secret
56+
* @param secretEncoder secret encoder
57+
*/
58+
public DirectJWTSigner(String secret, JWTToken.SecretEncoder secretEncoder) {
4459
this.secret = secret;
60+
if (secretEncoder != null) {
61+
this.secretEncoder = secretEncoder;
62+
}
4563
}
4664

4765
/**
@@ -62,7 +80,7 @@ public DirectJWTSigner(String secret) {
6280
* @param options Allow choosing the signing algorithm, and automatic setting of some registered claims.
6381
*/
6482
public String sign(Map<String, Object> claims, Options options) throws Exception{
65-
Algorithm algorithm = Algorithm.HMAC256(secret);
83+
Algorithm algorithm = Algorithm.HMAC256(secretEncoder.encode(secret));
6684
if (options != null && options.algorithm != null) {
6785
algorithm = options.algorithm;
6886
}

src/java/main/com/topcoder/direct/services/view/util/jwt/JWTToken.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,18 @@ public class JWTToken {
6767

6868
private String algorithmName = "HS256";
6969

70-
protected SecretEncoder encoder = new SecretEncoder();
70+
protected SecretEncoder encoder = new Base64SecretEncoder();
7171

7272
/**
7373
* Constructor
7474
*
7575
* @param token token
7676
* @param secret secret, if algorithm required it
7777
* @param knownIssuers comma separate known issuers
78+
* @param secretEncoder encoder of secret
7879
* @throws JWTException
7980
*/
80-
public JWTToken(String token, String secret, String knownIssuers) throws JWTException{
81+
public JWTToken(String token, String secret, String knownIssuers, SecretEncoder secretEncoder) throws JWTException{
8182
if (token == null) {
8283
logger.error("token can not be null");
8384
throw new IllegalArgumentException("token can not be null");
@@ -87,10 +88,14 @@ public JWTToken(String token, String secret, String knownIssuers) throws JWTExce
8788
throw new IllegalArgumentException("issuers can not be null");
8889
}
8990

91+
if (secretEncoder != null)
92+
this.encoder = secretEncoder;
93+
9094
for (String issuer : knownIssuers.split("\\s*,\\s*")) {
9195
this.knownIssuers.add(issuer.trim());
9296
}
9397

98+
9499
setTokenAndSecret(token, secret);
95100
}
96101

0 commit comments

Comments
 (0)