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

Commit 5ee31b2

Browse files
committed
fix JwtTokeUpdater thread safety
1 parent ac34bca commit 5ee31b2

File tree

7 files changed

+21
-62
lines changed

7 files changed

+21
-62
lines changed

src/java/main/com/topcoder/direct/services/view/action/ServiceBackendDataTablesAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ protected JsonNode getJsonResultFromAPI(URI apiEndPoint) throws Exception {
323323
// specify the get request
324324
HttpGet getRequest = new HttpGet(apiEndPoint);
325325

326-
String token = jwtTokenUpdater.check().getToken();
326+
String token = jwtTokenUpdater.getV3Token();
327327

328328
getRequest.setHeader(HttpHeaders.AUTHORIZATION,
329329
"Bearer " + token);

src/java/main/com/topcoder/direct/services/view/action/contest/launch/GetGroupMemberAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ private RestResult<GroupMember> getGroupMemberByGid(Long gid) throws Exception {
190190
HttpGet request = new HttpGet(groupApiEndpointUri);
191191
String jwtToken;
192192
try{
193-
jwtToken = jwtTokenUpdater.check().getToken();
193+
jwtToken = jwtTokenUpdater.getV3Token();
194194
} catch (Exception e) {
195195
logger.error("Can't get jwt token");
196196
throw e;

src/java/main/com/topcoder/direct/services/view/action/my/MyChallengesAction.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
*/
44
package com.topcoder.direct.services.view.action.my;
55

6+
import com.topcoder.direct.services.configs.ServerConfiguration;
67
import com.topcoder.direct.services.view.action.ServiceBackendDataTablesAction;
78
import com.topcoder.direct.services.view.dto.my.Challenge;
89
import com.topcoder.direct.services.view.dto.my.RestResult;
9-
import com.topcoder.direct.services.view.exception.JwtAuthenticationException;
10+
import com.topcoder.direct.services.view.util.DirectUtils;
1011
import org.codehaus.jackson.JsonNode;
1112

13+
import org.apache.struts2.ServletActionContext;
14+
1215
import java.text.DateFormat;
1316
import java.text.NumberFormat;
1417
import java.text.SimpleDateFormat;
@@ -46,11 +49,9 @@ public class MyChallengesAction extends ServiceBackendDataTablesAction {
4649
*/
4750
@Override
4851
public String execute() throws Exception {
49-
try {
50-
getJwtTokenUpdater().check();
51-
} catch (JwtAuthenticationException e) {
52+
if (DirectUtils.getCookieFromRequest(ServletActionContext.getRequest(),
53+
ServerConfiguration.JWT_COOOKIE_KEY) == null)
5254
return "forward";
53-
}
5455

5556
// populate filter data
5657
this.setupFilterPanel();

src/java/main/com/topcoder/direct/services/view/action/my/MyCreatedChallengesAction.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@
77
import com.topcoder.direct.services.view.action.ServiceBackendDataTablesAction;
88
import com.topcoder.direct.services.view.dto.my.Challenge;
99
import com.topcoder.direct.services.view.dto.my.RestResult;
10-
import com.topcoder.direct.services.view.exception.JwtAuthenticationException;
1110
import com.topcoder.direct.services.view.util.DirectUtils;
12-
import com.topcoder.direct.services.view.util.JwtTokenUpdater;
1311
import com.topcoder.service.user.UserService;
1412
import org.apache.struts2.ServletActionContext;
1513
import org.codehaus.jackson.JsonNode;
1614

17-
import javax.servlet.http.Cookie;
1815
import java.text.DateFormat;
1916
import java.text.NumberFormat;
2017
import java.text.SimpleDateFormat;
@@ -63,11 +60,9 @@ public class MyCreatedChallengesAction extends ServiceBackendDataTablesAction {
6360
*/
6461
@Override
6562
public String execute() throws Exception {
66-
try {
67-
getJwtTokenUpdater().check();
68-
} catch (JwtAuthenticationException e) {
63+
if (DirectUtils.getCookieFromRequest(ServletActionContext.getRequest(),
64+
ServerConfiguration.JWT_COOOKIE_KEY) == null)
6965
return "forward";
70-
}
7166

7267
// populate filter data
7368
this.setupFilterPanel();

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@
77

88
import java.util.Arrays;
99
import java.util.Set;
10-
import java.util.Map;
1110

1211
import javax.servlet.http.Cookie;
1312
import javax.servlet.http.HttpServletRequest;
1413
import javax.servlet.http.HttpServletResponse;
1514
import javax.servlet.http.HttpSession;
1615

1716
import com.topcoder.direct.services.view.util.jwt.JWTToken;
18-
import com.topcoder.direct.services.view.util.jwt.MyTest;
1917
import com.topcoder.direct.services.view.util.jwt.TokenExpiredException;
2018
import org.apache.struts2.ServletActionContext;
2119

@@ -226,8 +224,6 @@ public class AuthenticationInterceptor extends AbstractInterceptor {
226224
*/
227225
private String redirectBackUrlIdentityKey;
228226

229-
private MyTest myTest;
230-
231227
/**
232228
* Default constructor, constructs an instance of this class.
233229
*/
@@ -294,15 +290,15 @@ public String intercept(ActionInvocation invocation) throws Exception {
294290
return loginPageName;
295291
}
296292

297-
JWTToken jwtToken;
293+
JWTToken jwtToken = null;
298294
try {
299295
String[] knownIssuers = new String[]{ "https://" + DirectProperties.DOMAIN_AUTH0 };
300296
jwtToken = new JWTToken(jwtCookie.getValue(),DirectProperties.CLIENT_SECRET_AUTH0, Arrays.asList(knownIssuers));
301297
} catch (TokenExpiredException e) {
302298
//refresh token here
303299
//redirect to loginpage for now
304-
logger.error("Token is expired. redirect to login page");
305-
return loginPageName;
300+
logger.error("Token is expired. Should do refresh token here");
301+
//return loginPageName;
306302
} catch (Exception e) {
307303
return loginPageName;
308304
}
@@ -441,12 +437,4 @@ public void setRedirectBackUrlIdentityKey(String redirectBackUrlIdentityKey) {
441437
Helper.checkNotNullOrEmpty(redirectBackUrlIdentityKey, "redirectBackUrlIdentityKey");
442438
this.redirectBackUrlIdentityKey = redirectBackUrlIdentityKey;
443439
}
444-
445-
public MyTest getMyTest() {
446-
return myTest;
447-
}
448-
449-
public void setMyTest(MyTest myTest) {
450-
this.myTest = myTest;
451-
}
452440
}

src/java/main/com/topcoder/direct/services/view/util/DirectUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3787,7 +3787,7 @@ public static Set<ProjectGroup> getGroupsFromApi(TCSubject tcSubject, JwtTokenUp
37873787
HttpGet getRequest = new HttpGet(uri.build());
37883788
logger.info("Getting Group with thi uri: " + uri.build().toString());
37893789

3790-
String v3Token = jwtTokenUpdater.check().getToken();
3790+
String v3Token = jwtTokenUpdater.getV3Token();
37913791

37923792
getRequest.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + v3Token);
37933793

src/java/main/com/topcoder/direct/services/view/util/JwtTokenUpdater.java

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,6 @@ public class JwtTokenUpdater {
4040
*/
4141
private String authorizationURL;
4242

43-
/**
44-
* v3 token
45-
*/
46-
private String token;
47-
48-
private String v2Token = null;
49-
5043
/**
5144
* ssoLogin Url
5245
*/
@@ -69,12 +62,12 @@ public JwtTokenUpdater() {
6962
}
7063

7164
/**
72-
* Check token from cookie
65+
* Validate and get v3 token from cookies
7366
*
74-
* @return this class instance
67+
* @return v3 token
7568
* @throws Exception
7669
*/
77-
public JwtTokenUpdater check() throws Exception {
70+
public String getV3Token() throws Exception {
7871
Cookie jwtCookieV3 = DirectUtils.getCookieFromRequest(ServletActionContext.getRequest(),
7972
ServerConfiguration.JWT_V3_COOKIE_KEY);
8073
Cookie jwtCookieV2 = DirectUtils.getCookieFromRequest(ServletActionContext.getRequest(),
@@ -84,9 +77,7 @@ public JwtTokenUpdater check() throws Exception {
8477
throw new JwtAuthenticationException("Please re-login");
8578
}
8679

87-
validateCookieV2V3(jwtCookieV2,jwtCookieV3);
88-
v2Token = jwtCookieV2.getValue();
89-
return this;
80+
return validateCookieV2V3(jwtCookieV2,jwtCookieV3);
9081
}
9182

9283

@@ -163,9 +154,10 @@ private String getValidJwtToken(String v3token, String v2token) throws JwtAuthen
163154
*
164155
* @param v2 cookie v2
165156
* @param v3 cookie v3
157+
* @return valid v3 token
166158
* @throws Exception
167159
*/
168-
private void validateCookieV2V3(Cookie v2, Cookie v3) throws Exception {
160+
private String validateCookieV2V3(Cookie v2, Cookie v3) throws Exception {
169161
String validToken;
170162
String v3Token = null;
171163
if (v3 == null) {
@@ -179,17 +171,9 @@ private void validateCookieV2V3(Cookie v2, Cookie v3) throws Exception {
179171
DirectUtils.addDirectCookie(ServletActionContext.getResponse(), ServerConfiguration.JWT_V3_COOKIE_KEY, validToken, -1);
180172
}
181173

182-
token = validToken;
174+
return validToken;
183175
}
184176

185-
/**
186-
* True if user has logge-in and has v2token
187-
* Must be called after {@link #check()}
188-
* @return
189-
*/
190-
public boolean isLoggedIn() {
191-
return v2Token != null && !v2Token.isEmpty();
192-
}
193177

194178
public String getAuthorizationURL() {
195179
return authorizationURL;
@@ -206,13 +190,4 @@ public String getSsoLoginUrl() {
206190
public void setSsoLoginUrl(String ssoLoginUrl) {
207191
this.ssoLoginUrl = ssoLoginUrl;
208192
}
209-
210-
/**
211-
* Get v3 token
212-
* Must be called after {@link #check()}
213-
* @return
214-
*/
215-
public String getToken() {
216-
return token;
217-
}
218193
}

0 commit comments

Comments
 (0)