Skip to content

Commit

Permalink
Enable get and query strings for check_token endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
fhanik committed Apr 22, 2017
1 parent 8d94c5b commit 80d9398
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
Expand Up @@ -39,10 +39,13 @@


import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;


import static java.util.Collections.emptyList;
import static org.springframework.http.HttpStatus.NOT_ACCEPTABLE; import static org.springframework.http.HttpStatus.NOT_ACCEPTABLE;
import static org.springframework.util.StringUtils.commaDelimitedListToSet;
import static org.springframework.util.StringUtils.hasText; import static org.springframework.util.StringUtils.hasText;
import static org.springframework.web.bind.annotation.RequestMethod.POST; import static org.springframework.web.bind.annotation.RequestMethod.POST;


Expand All @@ -60,10 +63,10 @@ public void setTokenServices(ResourceServerTokenServices resourceServerTokenServ
this.resourceServerTokenServices = resourceServerTokenServices; this.resourceServerTokenServices = resourceServerTokenServices;
} }


private boolean allowQueryString = false; private Boolean allowQueryString = null;


public boolean isAllowQueryString() { public boolean isAllowQueryString() {
return allowQueryString; return (allowQueryString == null) ? true : allowQueryString;
} }


public void setAllowQueryString(boolean allowQueryString) { public void setAllowQueryString(boolean allowQueryString) {
Expand All @@ -81,7 +84,7 @@ public Claims checkToken(@RequestParam("token") String value,
@RequestParam(name = "scopes", required = false, defaultValue = "") List<String> scopes, @RequestParam(name = "scopes", required = false, defaultValue = "") List<String> scopes,
HttpServletRequest request) throws HttpRequestMethodNotSupportedException { HttpServletRequest request) throws HttpRequestMethodNotSupportedException {


if (hasText(request.getQueryString())) { if (hasText(request.getQueryString()) && !isAllowQueryString()) {
logger.debug("Call to /oauth/token contains a query string. Aborting."); logger.debug("Call to /oauth/token contains a query string. Aborting.");
throw new HttpRequestMethodNotSupportedException("POST"); throw new HttpRequestMethodNotSupportedException("POST");
} }
Expand Down Expand Up @@ -121,7 +124,15 @@ public Claims checkToken(@RequestParam("token") String value,


@RequestMapping(value = "/check_token") @RequestMapping(value = "/check_token")
public void checkToken(HttpServletRequest request) throws HttpRequestMethodNotSupportedException { public void checkToken(HttpServletRequest request) throws HttpRequestMethodNotSupportedException {
throw new HttpRequestMethodNotSupportedException(request.getMethod()); if (isAllowQueryString()) {
String token = request.getParameter("token");
String scope = request.getParameter("scope");
checkToken(
token, hasText(scope) ? new LinkedList<>(commaDelimitedListToSet(scope)) : emptyList(),
request);
} else {
throw new HttpRequestMethodNotSupportedException(request.getMethod());
}
} }




Expand Down
Expand Up @@ -44,8 +44,8 @@ public class UaaTokenEndpoint extends TokenEndpoint {


private Boolean allowQueryString = null; private Boolean allowQueryString = null;


public Boolean isAllowQueryString() { public boolean isAllowQueryString() {
return allowQueryString; return allowQueryString == null ? true : allowQueryString;
} }


public void setAllowQueryString(boolean allowQueryString) { public void setAllowQueryString(boolean allowQueryString) {
Expand Down
Expand Up @@ -52,6 +52,7 @@
import org.springframework.security.oauth2.provider.OAuth2Authentication; import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.client.BaseClientDetails; import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.security.oauth2.provider.client.InMemoryClientDetailsService; import org.springframework.security.oauth2.provider.client.InMemoryClientDetailsService;
import org.springframework.web.HttpRequestMethodNotSupportedException;


import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.ArrayList; import java.util.ArrayList;
Expand Down Expand Up @@ -779,6 +780,33 @@ public void testValidateAudParameter() throws Exception {
assertTrue(aud.contains("client")); assertTrue(aud.contains("client"));
} }


@Test
public void by_default_query_string_is_allowed() throws Exception {
setAccessToken(tokenServices.createAccessToken(authentication));
request.setQueryString("token="+getAccessToken());
endpoint.checkToken(getAccessToken(), Collections.emptyList(), request);
}

@Test
public void by_default_get_is_allowed() throws Exception {
setAccessToken(tokenServices.createAccessToken(authentication));
request.setQueryString("token="+getAccessToken());
request.setParameter("token", getAccessToken());
endpoint.checkToken(request);
}

@Test(expected = HttpRequestMethodNotSupportedException.class)
public void disable_query_string() throws Exception {
endpoint.setAllowQueryString(false);
by_default_query_string_is_allowed();
}

@Test(expected = HttpRequestMethodNotSupportedException.class)
public void disable_get_method() throws Exception {
endpoint.setAllowQueryString(false);
by_default_get_is_allowed();
}

@Test @Test
public void testClientId() throws Exception { public void testClientId() throws Exception {
setAccessToken(tokenServices.createAccessToken(authentication)); setAccessToken(tokenServices.createAccessToken(authentication));
Expand Down
Expand Up @@ -20,6 +20,7 @@
import org.cloudfoundry.identity.uaa.mock.token.AbstractTokenMockMvcTests; import org.cloudfoundry.identity.uaa.mock.token.AbstractTokenMockMvcTests;
import org.cloudfoundry.identity.uaa.oauth.token.TokenConstants; import org.cloudfoundry.identity.uaa.oauth.token.TokenConstants;
import org.cloudfoundry.identity.uaa.util.JsonUtils; import org.cloudfoundry.identity.uaa.util.JsonUtils;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.security.oauth2.common.util.OAuth2Utils; import org.springframework.security.oauth2.common.util.OAuth2Utils;
Expand Down Expand Up @@ -50,6 +51,7 @@ public class CheckTokenEndpointMockMvcTest extends AbstractTokenMockMvcTests {
public static final String CLIENTSECRET = "secret"; public static final String CLIENTSECRET = "secret";
private String token; private String token;
private String basic; private String basic;
private boolean allowQueryString;


@Before @Before
public void get_token_to_check() throws Exception { public void get_token_to_check() throws Exception {
Expand All @@ -70,8 +72,16 @@ public void get_token_to_check() throws Exception {
Map<String,Object> tokenMap = JsonUtils.readValue(content, new TypeReference<Map<String, Object>>() {}); Map<String,Object> tokenMap = JsonUtils.readValue(content, new TypeReference<Map<String, Object>>() {});
token = (String) tokenMap.get("access_token"); token = (String) tokenMap.get("access_token");
basic = new String(Base64.encodeBase64((CLIENTID+":"+CLIENTSECRET).getBytes())); basic = new String(Base64.encodeBase64((CLIENTID+":"+CLIENTSECRET).getBytes()));
allowQueryString = getWebApplicationContext().getBean(CheckTokenEndpoint.class).isAllowQueryString();
getWebApplicationContext().getBean(CheckTokenEndpoint.class).setAllowQueryString(false);
} }


@After
public void resetAllowQueryString() throws Exception {
getWebApplicationContext().getBean(CheckTokenEndpoint.class).setAllowQueryString(allowQueryString);
}


@Test @Test
public void check_token_get() throws Exception { public void check_token_get() throws Exception {
check_token(get("/check_token"), status().isMethodNotAllowed()) check_token(get("/check_token"), status().isMethodNotAllowed())
Expand All @@ -93,6 +103,12 @@ public void check_token_post() throws Exception {
check_token(post("/check_token"), status().isOk()); check_token(post("/check_token"), status().isOk());
} }


@Test
public void check_token_get_when_allowed() throws Exception {
getWebApplicationContext().getBean(CheckTokenEndpoint.class).setAllowQueryString(true);
check_token(get("/check_token"), status().isOk());
}

@Test @Test
public void check_token_delete() throws Exception { public void check_token_delete() throws Exception {
check_token(MockMvcRequestBuilders.delete("/check_token"),status().isMethodNotAllowed()) check_token(MockMvcRequestBuilders.delete("/check_token"),status().isMethodNotAllowed())
Expand Down

0 comments on commit 80d9398

Please sign in to comment.