Skip to content

Commit 0837930

Browse files
PD-6057 PD-6040 fixed 500 for revoke token and the incomplete source for affiliations (#7603)
* PD-6040 Populate the uri and host for source * PD-6057 Fixed 500 error Do we still need Features.OAUTH_TOKEN_VALIDATION.isActive() check or we should remove the legacy code * Removed legacy code as oauth server is always on
1 parent f4d7b04 commit 0837930

2 files changed

Lines changed: 71 additions & 28 deletions

File tree

orcid-core/src/main/java/org/orcid/core/adapter/v3/impl/MapperFacadeFactory.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,10 +405,54 @@ public void mapBtoA(SourceAwareEntity<?> b, SourceAware a, MappingContext contex
405405
}
406406
}
407407

408+
populateSourceUriAndHost(source);
409+
408410
a.setSource(source);
409411
}
410412
}
411413

414+
private void populateSourceUriAndHost(Source source) {
415+
if (source == null) {
416+
return;
417+
}
418+
419+
if (source.getSourceOrcid() != null && StringUtils.isNotBlank(source.getSourceOrcid().getPath())) {
420+
if (StringUtils.isBlank(source.getSourceOrcid().getHost())) {
421+
source.getSourceOrcid().setHost(orcidUrlManager.getBaseHost());
422+
}
423+
if (StringUtils.isBlank(source.getSourceOrcid().getUri())) {
424+
source.getSourceOrcid().setUri(orcidUrlManager.getBaseUrl() + "/" + source.getSourceOrcid().getPath());
425+
}
426+
}
427+
428+
if (source.getSourceClientId() != null && StringUtils.isNotBlank(source.getSourceClientId().getPath())) {
429+
if (StringUtils.isBlank(source.getSourceClientId().getHost())) {
430+
source.getSourceClientId().setHost(orcidUrlManager.getBaseHost());
431+
}
432+
if (StringUtils.isBlank(source.getSourceClientId().getUri())) {
433+
source.getSourceClientId().setUri(orcidUrlManager.getBaseUrl() + "/client/" + source.getSourceClientId().getPath());
434+
}
435+
}
436+
437+
if (source.getAssertionOriginOrcid() != null && StringUtils.isNotBlank(source.getAssertionOriginOrcid().getPath())) {
438+
if (StringUtils.isBlank(source.getAssertionOriginOrcid().getHost())) {
439+
source.getAssertionOriginOrcid().setHost(orcidUrlManager.getBaseHost());
440+
}
441+
if (StringUtils.isBlank(source.getAssertionOriginOrcid().getUri())) {
442+
source.getAssertionOriginOrcid().setUri(orcidUrlManager.getBaseUrl() + "/" + source.getAssertionOriginOrcid().getPath());
443+
}
444+
}
445+
446+
if (source.getAssertionOriginClientId() != null && StringUtils.isNotBlank(source.getAssertionOriginClientId().getPath())) {
447+
if (StringUtils.isBlank(source.getAssertionOriginClientId().getHost())) {
448+
source.getAssertionOriginClientId().setHost(orcidUrlManager.getBaseHost());
449+
}
450+
if (StringUtils.isBlank(source.getAssertionOriginClientId().getUri())) {
451+
source.getAssertionOriginClientId().setUri(orcidUrlManager.getBaseUrl() + "/client/" + source.getAssertionOriginClientId().getPath());
452+
}
453+
}
454+
}
455+
412456
private Source extractSourceFromEntityWithoutUtils(SourceAwareEntity<?> entity) {
413457
Source source = new Source();
414458

orcid-web/src/main/java/org/orcid/frontend/oauth2/RevokeController.java

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
import org.orcid.core.oauth.authorizationServer.AuthorizationServerUtil;
1111
import org.orcid.core.togglz.Features;
1212
import org.orcid.pojo.ajaxForm.PojoUtil;
13-
import org.slf4j.Logger;
14-
import org.slf4j.LoggerFactory;
1513
import org.springframework.http.HttpHeaders;
1614
import org.springframework.http.ResponseEntity;
15+
import org.springframework.security.core.Authentication;
1716
import org.springframework.security.core.context.SecurityContextHolder;
1817
import org.springframework.stereotype.Controller;
1918
import org.springframework.web.bind.annotation.RequestMapping;
@@ -25,45 +24,45 @@
2524
@RequestMapping(value = { T2OrcidApiService.OAUTH_REVOKE }, consumes = MediaType.APPLICATION_FORM_URLENCODED, produces = MediaType.APPLICATION_JSON)
2625
public class RevokeController {
2726

28-
private static final Logger LOGGER = LoggerFactory.getLogger(RevokeController.class);
29-
3027
@Resource
3128
private AuthorizationServerUtil authorizationServerUtil;
3229

3330
@RequestMapping
3431
public ResponseEntity<?> revoke(HttpServletRequest request) throws IOException, URISyntaxException, InterruptedException {
3532
String tokenToRevoke = request.getParameter("token");
33+
String authorization = request.getHeader("Authorization");
3634
Response r = null;
37-
38-
if(Features.OAUTH_TOKEN_VALIDATION.isActive()) {
39-
// Forward the request to the authorization server
40-
if (PojoUtil.isEmpty(tokenToRevoke)) {
41-
throw new IllegalArgumentException("Please provide the token to be param");
42-
}
43-
if(StringUtils.isNotBlank(request.getHeader("Authorization"))) {
44-
String authorization = request.getHeader("Authorization");
45-
r = authorizationServerUtil.forwardTokenRevocationRequest(authorization, tokenToRevoke);
46-
} else {
47-
String clientId = SecurityContextHolder.getContext().getAuthentication().getName();
48-
String clientSecret = request.getParameter("client_secret");
49-
r = authorizationServerUtil.forwardTokenRevocationRequest(clientId, clientSecret, tokenToRevoke);
50-
}
51-
HttpHeaders responseHeaders = new HttpHeaders();
52-
responseHeaders.set(Features.OAUTH_TOKEN_VALIDATION.name(),
53-
"ON");
54-
return ResponseEntity.status(r.getStatus()).headers(responseHeaders).body(r.getEntity());
35+
36+
if (PojoUtil.isEmpty(tokenToRevoke)) {
37+
throw new IllegalArgumentException("Please provide the token to be param");
38+
}
39+
40+
// Forward the request to the authorization server
41+
if (StringUtils.isNotBlank(authorization)) {
42+
r = authorizationServerUtil.forwardTokenRevocationRequest(authorization, tokenToRevoke);
5543
} else {
56-
String clientId = SecurityContextHolder.getContext().getAuthentication().getName();
44+
String clientId = resolveClientId(request);
5745
String clientSecret = request.getParameter("client_secret");
58-
if (PojoUtil.isEmpty(tokenToRevoke)) {
59-
throw new IllegalArgumentException("Please provide the token to be param");
60-
}
6146
r = authorizationServerUtil.forwardTokenRevocationRequest(clientId, clientSecret, tokenToRevoke);
6247
}
6348
HttpHeaders responseHeaders = new HttpHeaders();
64-
responseHeaders.set(Features.OAUTH_TOKEN_VALIDATION.name(),
65-
"ON");
49+
responseHeaders.set(Features.OAUTH_TOKEN_VALIDATION.name(), "ON");
6650
return ResponseEntity.status(r.getStatus()).headers(responseHeaders).body(r.getEntity());
51+
52+
}
53+
54+
private String resolveClientId(HttpServletRequest request) {
55+
String clientId = request.getParameter("client_id");
56+
if (StringUtils.isNotBlank(clientId)) {
57+
return clientId;
58+
}
59+
60+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
61+
if (authentication != null && StringUtils.isNotBlank(authentication.getName())) {
62+
return authentication.getName();
63+
}
64+
65+
throw new IllegalArgumentException("Please provide client_id or Authorization header");
6766
}
6867

6968
}

0 commit comments

Comments
 (0)