Skip to content

Commit b176d4d

Browse files
committed
cleaned up old endpoints
1 parent 8178af8 commit b176d4d

File tree

2 files changed

+60
-22
lines changed

2 files changed

+60
-22
lines changed

openid-connect-server/src/main/java/org/mitre/oauth2/web/RevocationEndpoint.java

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,24 @@
1616
*******************************************************************************/
1717
package org.mitre.oauth2.web;
1818

19-
import java.security.Principal;
19+
import static org.mitre.oauth2.web.AuthenticationUtilities.ensureOAuthScope;
2020

21+
import java.util.Collection;
22+
23+
import org.mitre.oauth2.model.ClientDetailsEntity;
2124
import org.mitre.oauth2.model.OAuth2AccessTokenEntity;
2225
import org.mitre.oauth2.model.OAuth2RefreshTokenEntity;
26+
import org.mitre.oauth2.service.ClientDetailsEntityService;
2327
import org.mitre.oauth2.service.OAuth2TokenEntityService;
28+
import org.mitre.oauth2.service.SystemScopeService;
2429
import org.mitre.openid.connect.view.HttpCodeView;
30+
import org.mitre.uma.model.ResourceSet;
2531
import org.slf4j.Logger;
2632
import org.slf4j.LoggerFactory;
2733
import org.springframework.beans.factory.annotation.Autowired;
2834
import org.springframework.http.HttpStatus;
2935
import org.springframework.security.access.prepost.PreAuthorize;
36+
import org.springframework.security.core.Authentication;
3037
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
3138
import org.springframework.security.oauth2.provider.OAuth2Authentication;
3239
import org.springframework.security.oauth2.provider.OAuth2Request;
@@ -38,7 +45,10 @@
3845
@Controller
3946
public class RevocationEndpoint {
4047
@Autowired
41-
OAuth2TokenEntityService tokenServices;
48+
private ClientDetailsEntityService clientService;
49+
50+
@Autowired
51+
private OAuth2TokenEntityService tokenServices;
4252

4353
/**
4454
* Logger for this class
@@ -49,32 +59,53 @@ public class RevocationEndpoint {
4959

5060
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_CLIENT')")
5161
@RequestMapping("/" + URL)
52-
public String revoke(@RequestParam("token") String tokenValue, @RequestParam(value = "token_type_hint", required = false) String tokenType, Principal principal, Model model) {
62+
public String revoke(@RequestParam("token") String tokenValue, @RequestParam(value = "token_type_hint", required = false) String tokenType, Authentication auth, Model model) {
5363

5464
// This is the token as passed in from OAuth (in case we need it some day)
5565
//OAuth2AccessTokenEntity tok = tokenServices.getAccessToken((OAuth2Authentication) principal);
5666

57-
OAuth2Request authRequest = null;
58-
if (principal instanceof OAuth2Authentication) {
59-
// if the client is acting on its own behalf (the common case), pull out the client authorization request
60-
authRequest = ((OAuth2Authentication) principal).getOAuth2Request();
67+
ClientDetailsEntity authClient = null;
68+
69+
if (auth instanceof OAuth2Authentication) {
70+
// the client authenticated with OAuth, do our UMA checks
71+
ensureOAuthScope(auth, SystemScopeService.UMA_PROTECTION_SCOPE);
72+
// get out the client that was issued the access token (not the token being revoked)
73+
OAuth2Authentication o2a = (OAuth2Authentication) auth;
74+
75+
String authClientId = o2a.getOAuth2Request().getClientId();
76+
authClient = clientService.loadClientByClientId(authClientId);
77+
78+
// the owner is the user who authorized the token in the first place
79+
String ownerId = o2a.getUserAuthentication().getName();
80+
81+
} else {
82+
// the client authenticated directly, make sure it's got the right access
83+
84+
String authClientId = auth.getName(); // direct authentication puts the client_id into the authentication's name field
85+
authClient = clientService.loadClientByClientId(authClientId);
86+
6187
}
6288

6389
try {
6490
// check and handle access tokens first
6591

6692
OAuth2AccessTokenEntity accessToken = tokenServices.readAccessToken(tokenValue);
67-
if (authRequest != null) {
68-
// client acting on its own, make sure it owns the token
69-
if (!accessToken.getClient().getClientId().equals(authRequest.getClientId())) {
70-
// trying to revoke a token we don't own, throw a 403
71-
model.addAttribute(HttpCodeView.CODE, HttpStatus.FORBIDDEN);
72-
return HttpCodeView.VIEWNAME;
73-
}
93+
94+
// client acting on its own, make sure it owns the token
95+
if (!accessToken.getClient().getClientId().equals(authClient.getClientId())) {
96+
// trying to revoke a token we don't own, throw a 403
97+
98+
logger.info("Client " + authClient.getClientId() + " tried to revoke a token owned by " + accessToken.getClient().getClientId());
99+
100+
model.addAttribute(HttpCodeView.CODE, HttpStatus.FORBIDDEN);
101+
return HttpCodeView.VIEWNAME;
74102
}
75103

76104
// if we got this far, we're allowed to do this
77105
tokenServices.revokeAccessToken(accessToken);
106+
107+
logger.debug("Client " + authClient.getClientId() + " revoked access token " + tokenValue);
108+
78109
model.addAttribute(HttpCodeView.CODE, HttpStatus.OK);
79110
return HttpCodeView.VIEWNAME;
80111

@@ -84,24 +115,30 @@ public String revoke(@RequestParam("token") String tokenValue, @RequestParam(val
84115

85116
try {
86117
OAuth2RefreshTokenEntity refreshToken = tokenServices.getRefreshToken(tokenValue);
87-
if (authRequest != null) {
88-
// client acting on its own, make sure it owns the token
89-
if (!refreshToken.getClient().getClientId().equals(authRequest.getClientId())) {
90-
// trying to revoke a token we don't own, throw a 403
91-
model.addAttribute(HttpCodeView.CODE, HttpStatus.FORBIDDEN);
92-
return HttpCodeView.VIEWNAME;
93-
}
118+
// client acting on its own, make sure it owns the token
119+
if (!refreshToken.getClient().getClientId().equals(authClient.getClientId())) {
120+
// trying to revoke a token we don't own, throw a 403
121+
122+
logger.info("Client " + authClient.getClientId() + " tried to revoke a token owned by " + refreshToken.getClient().getClientId());
123+
124+
model.addAttribute(HttpCodeView.CODE, HttpStatus.FORBIDDEN);
125+
return HttpCodeView.VIEWNAME;
94126
}
95127

96128
// if we got this far, we're allowed to do this
97129
tokenServices.revokeRefreshToken(refreshToken);
130+
131+
logger.debug("Client " + authClient.getClientId() + " revoked access token " + tokenValue);
132+
98133
model.addAttribute(HttpCodeView.CODE, HttpStatus.OK);
99134
return HttpCodeView.VIEWNAME;
100135

101136
} catch (InvalidTokenException e1) {
102137

103138
// neither token type was found, simply say "OK" and be on our way.
104139

140+
logger.debug("Failed to revoke token " + tokenValue);
141+
105142
model.addAttribute(HttpCodeView.CODE, HttpStatus.OK);
106143
return HttpCodeView.VIEWNAME;
107144
}

openid-connect-server/src/main/java/org/mitre/openid/connect/web/DataAPI.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,13 @@ public String importData(Reader in, Model m) throws IOException {
111111
}
112112
break;
113113
case END_OBJECT:
114-
reader.endObject();
115114
break;
116115
case END_DOCUMENT:
117116
break;
118117
}
119118
}
119+
120+
reader.endObject();
120121

121122
return "httpCodeView";
122123
}

0 commit comments

Comments
 (0)