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

Commit 569c620

Browse files
authored
Merge pull request #359 from deedee/jwttoken_update
FIX ME: HS256 use oldjwt to refresh v3jwt
2 parents 6137dcc + 9c7cc72 commit 569c620

File tree

5 files changed

+17
-3
lines changed

5 files changed

+17
-3
lines changed

conf/ApplicationServer.properties

+1
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ SSO_HASH_SECRET = @ApplicationServer.SSO_HASH_SECRET@
4545
SSO_DOMAIN = @ApplicationServer.SSO_DOMAIN@
4646

4747
JWT_V3_COOKIE_KEY = @ApplicationServer.JWT_V3_COOKIE_KEY@
48+
JWT_COOKIE_KEY = @ApplicationServer.JWT_COOKIE_KEY@

src/java/main/com/topcoder/direct/services/configs/ServerConfiguration.java

+2
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,6 @@ public class ServerConfiguration extends ApplicationServer {
5656
* @since 1.1
5757
*/
5858
public static String JWT_V3_COOKIE_KEY = bundle.getProperty("JWT_V3_COOKIE_KEY", "v3jwt");
59+
60+
public static String JWT_COOKIE_KEY = bundle.getProperty("JWT_COOKIE_KEY", "tcjwt");
5961
}

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,13 @@ public String intercept(ActionInvocation invocation) throws Exception {
300300
} catch (TokenExpiredException e) {
301301
logger.error("Token is expired. Try to refresh");
302302
try {
303-
jwtToken = jwtToken.refresh();
303+
//TODO .refresh() should use v3jwt for all algo
304+
if ("HS256".equals(jwtToken.getAlgorithm())) {
305+
jwtToken = jwtToken.refresh(DirectUtils.getCookieFromRequest(ServletActionContext.getRequest(),
306+
ServerConfiguration.JWT_COOKIE_KEY).getValue());
307+
} else {
308+
jwtToken = jwtToken.refresh();
309+
}
304310
DirectUtils.addDirectCookie(ServletActionContext.getResponse(), ServerConfiguration.JWT_V3_COOKIE_KEY,
305311
jwtToken.getToken(), -1);
306312
} catch (Exception ex) {

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,17 @@ protected Integer calcExpirySeconds(Integer exp, Integer iat) {
291291
return exp - issuedAt;
292292
}
293293

294+
public JWTToken refresh() throws Exception {
295+
return refresh(this.token);
296+
}
297+
294298
/**
295299
* Refresh jwt from authorizationUrl
296300
*
297301
* @return this instance
298302
* @throws Exception if any error occurs
299303
*/
300-
public JWTToken refresh() throws Exception {
304+
public JWTToken refresh(String oldToken) throws Exception {
301305
if (authorizationURL == null || "".equals(authorizationURL))
302306
throw new JWTException("Please set authorizationUrl");
303307

@@ -309,7 +313,7 @@ public JWTToken refresh() throws Exception {
309313
HttpPost httpPost = new HttpPost(authorizationUri);
310314
httpPost.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");
311315

312-
StringEntity body = new StringEntity(String.format(AUTHORIZATION_PARAMS, token));
316+
StringEntity body = new StringEntity(String.format(AUTHORIZATION_PARAMS, oldToken));
313317
httpPost.setEntity(body);
314318
HttpResponse response = httpClient.execute(httpPost);
315319
HttpEntity entity = response.getEntity();

token.properties.docker

+1
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@
336336
#####################################
337337
# Direct API #
338338
#####################################
339+
@ApplicationServer.JWT_COOKIE_KEY@=tcjwt_vm
339340
@ApplicationServer.JWT_V3_COOKIE_KEY@=v3jwt
340341

341342
@memberSearchApiUrl@=https://tc-api.cloud.topcoder.com:8443/v3/members/_suggest/

0 commit comments

Comments
 (0)