Skip to content

Commit 4b7d088

Browse files
authored
Merge pull request #64 from xxcxy/dev
add token header to v5api
2 parents f2a1d91 + d658d52 commit 4b7d088

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/main/java/com/topcoder/onlinereview/component/project/phase/ManagerHelper.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,19 @@ public class ManagerHelper {
338338
@Value("${managerHelper.V5ChallengeURL:#{null}}")
339339
private String v5ChallengeURL;
340340

341+
@Value("${managerHelper.authClientId:#{null}}")
342+
private String authClientId;
343+
@Value("${managerHelper.authClientSecret:#{null}}")
344+
private String authClientSecret;
345+
@Value("${managerHelper.authAudience:#{null}}")
346+
private String authAudience;
347+
@Value("${managerHelper.authDomain:#{null}}")
348+
private String authDomain;
349+
@Value("${managerHelper.authExpirationTime:#{null}}")
350+
private String authExpirationTime;
351+
@Value("${managerHelper.authProxyURL:#{null}}")
352+
private String authProxyURL;
353+
341354
/**
342355
* <p>A <code>ReviewFeedbackManager</code> providing the interface to review feedback management system.</p>
343356
* @since 1.7.6
@@ -585,4 +598,28 @@ public ProjectPaymentCalculator getProjectPaymentAdjustmentCalculator() {
585598
public String getV5ChallengeURL() {
586599
return v5ChallengeURL;
587600
}
601+
602+
public String getAuthClientId() {
603+
return authClientId;
604+
}
605+
606+
public String getAuthClientSecret() {
607+
return authClientSecret;
608+
}
609+
610+
public String getAuthAudience() {
611+
return authAudience;
612+
}
613+
614+
public String getAuthDomain() {
615+
return authDomain;
616+
}
617+
618+
public String getAuthExpirationTime() {
619+
return authExpirationTime;
620+
}
621+
622+
public String getAuthProxyURL() {
623+
return authProxyURL;
624+
}
588625
}

src/main/java/com/topcoder/onlinereview/component/project/phase/handler/AbstractPhaseHandler.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.topcoder.onlinereview.component.email.TCSEmailMessage;
2323
import com.topcoder.onlinereview.component.external.ExternalUser;
2424
import com.topcoder.onlinereview.component.external.RetrievalException;
25+
import com.topcoder.onlinereview.component.jwt.JWTTokenGenerator;
2526
import com.topcoder.onlinereview.component.project.management.PersistenceException;
2627
import com.topcoder.onlinereview.component.project.management.Project;
2728
import com.topcoder.onlinereview.component.project.management.ProjectCategory;
@@ -38,6 +39,7 @@
3839
import com.topcoder.onlinereview.component.search.SearchBuilderException;
3940
import org.apache.commons.httpclient.HttpClient;
4041
import org.apache.commons.httpclient.methods.GetMethod;
42+
import org.apache.http.HttpHeaders;
4143
import org.slf4j.Logger;
4244
import org.slf4j.LoggerFactory;
4345

@@ -994,6 +996,7 @@ private String getV5Id(Long legacyId, Map<Long, Map<String, String>> challengeId
994996
GetMethod getMethod = new GetMethod(challengeUrl + legacyId);
995997
getMethod.addRequestHeader("accept", "application/json");
996998
try {
999+
setM2AuthToken(getMethod);
9971000
int statusCode = new HttpClient().executeMethod(getMethod);
9981001
if (statusCode == 200) {
9991002
List<Map<String, Object>> res = new ObjectMapper().readValue(getMethod.getResponseBodyAsString(), new TypeReference<List<Map<String, Object>>>(){});
@@ -1007,12 +1010,29 @@ private String getV5Id(Long legacyId, Map<Long, Map<String, String>> challengeId
10071010
} else {
10081011
log.error("Get challenge Id error with statusCode: " + statusCode);
10091012
}
1010-
} catch (IOException e) {
1013+
} catch (Exception e) {
10111014
log.error("parse challenge error: ", e);
10121015
}
10131016
return "";
10141017
}
10151018

1019+
/**
1020+
* Set token header
1021+
*
1022+
* @param getMethod request method
1023+
*/
1024+
public void setM2AuthToken(GetMethod getMethod) throws Exception {
1025+
if (managerHelper.getAuthClientId() != null) {
1026+
String token = JWTTokenGenerator.getInstance(managerHelper.getAuthClientId(),
1027+
managerHelper.getAuthClientSecret(),
1028+
managerHelper.getAuthAudience(),
1029+
managerHelper.getAuthDomain(),
1030+
Integer.parseInt(managerHelper.getAuthExpirationTime()),
1031+
managerHelper.getAuthProxyURL()).getMachineToken();
1032+
getMethod.addRequestHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token);
1033+
}
1034+
}
1035+
10161036
/**
10171037
* Send or delete phase related emails.
10181038
*

0 commit comments

Comments
 (0)