|
10 | 10 | import org.orcid.core.oauth.authorizationServer.AuthorizationServerUtil; |
11 | 11 | import org.orcid.core.togglz.Features; |
12 | 12 | import org.orcid.pojo.ajaxForm.PojoUtil; |
13 | | -import org.slf4j.Logger; |
14 | | -import org.slf4j.LoggerFactory; |
15 | 13 | import org.springframework.http.HttpHeaders; |
16 | 14 | import org.springframework.http.ResponseEntity; |
| 15 | +import org.springframework.security.core.Authentication; |
17 | 16 | import org.springframework.security.core.context.SecurityContextHolder; |
18 | 17 | import org.springframework.stereotype.Controller; |
19 | 18 | import org.springframework.web.bind.annotation.RequestMapping; |
|
25 | 24 | @RequestMapping(value = { T2OrcidApiService.OAUTH_REVOKE }, consumes = MediaType.APPLICATION_FORM_URLENCODED, produces = MediaType.APPLICATION_JSON) |
26 | 25 | public class RevokeController { |
27 | 26 |
|
28 | | - private static final Logger LOGGER = LoggerFactory.getLogger(RevokeController.class); |
29 | | - |
30 | 27 | @Resource |
31 | 28 | private AuthorizationServerUtil authorizationServerUtil; |
32 | 29 |
|
33 | 30 | @RequestMapping |
34 | 31 | public ResponseEntity<?> revoke(HttpServletRequest request) throws IOException, URISyntaxException, InterruptedException { |
35 | 32 | String tokenToRevoke = request.getParameter("token"); |
| 33 | + String authorization = request.getHeader("Authorization"); |
36 | 34 | 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); |
55 | 43 | } else { |
56 | | - String clientId = SecurityContextHolder.getContext().getAuthentication().getName(); |
| 44 | + String clientId = resolveClientId(request); |
57 | 45 | String clientSecret = request.getParameter("client_secret"); |
58 | | - if (PojoUtil.isEmpty(tokenToRevoke)) { |
59 | | - throw new IllegalArgumentException("Please provide the token to be param"); |
60 | | - } |
61 | 46 | r = authorizationServerUtil.forwardTokenRevocationRequest(clientId, clientSecret, tokenToRevoke); |
62 | 47 | } |
63 | 48 | HttpHeaders responseHeaders = new HttpHeaders(); |
64 | | - responseHeaders.set(Features.OAUTH_TOKEN_VALIDATION.name(), |
65 | | - "ON"); |
| 49 | + responseHeaders.set(Features.OAUTH_TOKEN_VALIDATION.name(), "ON"); |
66 | 50 | 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"); |
67 | 66 | } |
68 | 67 |
|
69 | 68 | } |
0 commit comments