|
|
|
|
@@ -20,6 +20,7 @@ import org.junit.Before;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.mockito.ArgumentCaptor;
|
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
|
import org.springframework.mock.web.MockHttpServletRequest;
|
|
|
|
|
import org.springframework.mock.web.MockHttpServletResponse;
|
|
|
|
|
import org.springframework.security.authentication.TestingAuthenticationToken;
|
|
|
|
|
@@ -34,6 +35,8 @@ import org.springframework.security.oauth2.core.endpoint.PkceParameterNames;
|
|
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
|
|
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationAttributeNames;
|
|
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
|
|
|
|
|
import org.springframework.security.oauth2.server.authorization.TestOAuth2Authorizations;
|
|
|
|
|
import org.springframework.security.oauth2.server.authorization.TokenType;
|
|
|
|
|
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
|
|
|
|
|
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
|
|
|
|
|
import org.springframework.security.oauth2.server.authorization.client.TestRegisteredClients;
|
|
|
|
|
@@ -43,6 +46,7 @@ import org.springframework.util.StringUtils;
|
|
|
|
|
import javax.servlet.FilterChain;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
|
|
|
|
|
@@ -122,19 +126,6 @@ public class OAuth2AuthorizationEndpointFilterTests {
|
|
|
|
|
verify(filterChain).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void doFilterWhenAuthorizationRequestPostThenNotProcessed() throws Exception {
|
|
|
|
|
String requestUri = OAuth2AuthorizationEndpointFilter.DEFAULT_AUTHORIZATION_ENDPOINT_URI;
|
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest("POST", requestUri);
|
|
|
|
|
request.setServletPath(requestUri);
|
|
|
|
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
|
|
|
|
FilterChain filterChain = mock(FilterChain.class);
|
|
|
|
|
|
|
|
|
|
this.filter.doFilter(request, response, filterChain);
|
|
|
|
|
|
|
|
|
|
verify(filterChain).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void doFilterWhenAuthorizationRequestMissingClientIdThenInvalidRequestError() throws Exception {
|
|
|
|
|
doFilterWhenAuthorizationRequestInvalidParameterThenError(
|
|
|
|
|
@@ -222,7 +213,7 @@ public class OAuth2AuthorizationEndpointFilterTests {
|
|
|
|
|
when(this.registeredClientRepository.findByClientId((eq(registeredClient.getClientId()))))
|
|
|
|
|
.thenReturn(registeredClient);
|
|
|
|
|
|
|
|
|
|
doFilterWhenAuthorizationRequestInvalidParameterThenRedirected(
|
|
|
|
|
doFilterWhenAuthorizationRequestInvalidParameterThenRedirect(
|
|
|
|
|
registeredClient,
|
|
|
|
|
OAuth2ParameterNames.RESPONSE_TYPE,
|
|
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST,
|
|
|
|
|
@@ -236,7 +227,7 @@ public class OAuth2AuthorizationEndpointFilterTests {
|
|
|
|
|
when(this.registeredClientRepository.findByClientId((eq(registeredClient.getClientId()))))
|
|
|
|
|
.thenReturn(registeredClient);
|
|
|
|
|
|
|
|
|
|
doFilterWhenAuthorizationRequestInvalidParameterThenRedirected(
|
|
|
|
|
doFilterWhenAuthorizationRequestInvalidParameterThenRedirect(
|
|
|
|
|
registeredClient,
|
|
|
|
|
OAuth2ParameterNames.RESPONSE_TYPE,
|
|
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST,
|
|
|
|
|
@@ -250,7 +241,7 @@ public class OAuth2AuthorizationEndpointFilterTests {
|
|
|
|
|
when(this.registeredClientRepository.findByClientId((eq(registeredClient.getClientId()))))
|
|
|
|
|
.thenReturn(registeredClient);
|
|
|
|
|
|
|
|
|
|
doFilterWhenAuthorizationRequestInvalidParameterThenRedirected(
|
|
|
|
|
doFilterWhenAuthorizationRequestInvalidParameterThenRedirect(
|
|
|
|
|
registeredClient,
|
|
|
|
|
OAuth2ParameterNames.RESPONSE_TYPE,
|
|
|
|
|
OAuth2ErrorCodes.UNSUPPORTED_RESPONSE_TYPE,
|
|
|
|
|
@@ -258,6 +249,23 @@ public class OAuth2AuthorizationEndpointFilterTests {
|
|
|
|
|
request -> request.setParameter(OAuth2ParameterNames.RESPONSE_TYPE, "id_token"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void doFilterWhenAuthorizationRequestInvalidScopeThenInvalidScopeError() throws Exception {
|
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
|
|
when(this.registeredClientRepository.findByClientId((eq(registeredClient.getClientId()))))
|
|
|
|
|
.thenReturn(registeredClient);
|
|
|
|
|
|
|
|
|
|
doFilterWhenAuthorizationRequestInvalidParameterThenRedirect(
|
|
|
|
|
registeredClient,
|
|
|
|
|
OAuth2ParameterNames.SCOPE,
|
|
|
|
|
OAuth2ErrorCodes.INVALID_SCOPE,
|
|
|
|
|
DEFAULT_ERROR_URI,
|
|
|
|
|
request -> {
|
|
|
|
|
String scope = request.getParameter(OAuth2ParameterNames.SCOPE);
|
|
|
|
|
request.setParameter(OAuth2ParameterNames.SCOPE, scope + " invalid-scope");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void doFilterWhenPkceRequiredAndMissingCodeChallengeThenInvalidRequestError() throws Exception {
|
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient()
|
|
|
|
|
@@ -266,7 +274,7 @@ public class OAuth2AuthorizationEndpointFilterTests {
|
|
|
|
|
when(this.registeredClientRepository.findByClientId((eq(registeredClient.getClientId()))))
|
|
|
|
|
.thenReturn(registeredClient);
|
|
|
|
|
|
|
|
|
|
doFilterWhenAuthorizationRequestInvalidParameterThenRedirected(
|
|
|
|
|
doFilterWhenAuthorizationRequestInvalidParameterThenRedirect(
|
|
|
|
|
registeredClient,
|
|
|
|
|
PkceParameterNames.CODE_CHALLENGE,
|
|
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST,
|
|
|
|
|
@@ -285,7 +293,7 @@ public class OAuth2AuthorizationEndpointFilterTests {
|
|
|
|
|
when(this.registeredClientRepository.findByClientId((eq(registeredClient.getClientId()))))
|
|
|
|
|
.thenReturn(registeredClient);
|
|
|
|
|
|
|
|
|
|
doFilterWhenAuthorizationRequestInvalidParameterThenRedirected(
|
|
|
|
|
doFilterWhenAuthorizationRequestInvalidParameterThenRedirect(
|
|
|
|
|
registeredClient,
|
|
|
|
|
PkceParameterNames.CODE_CHALLENGE,
|
|
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST,
|
|
|
|
|
@@ -302,7 +310,7 @@ public class OAuth2AuthorizationEndpointFilterTests {
|
|
|
|
|
when(this.registeredClientRepository.findByClientId((eq(registeredClient.getClientId()))))
|
|
|
|
|
.thenReturn(registeredClient);
|
|
|
|
|
|
|
|
|
|
doFilterWhenAuthorizationRequestInvalidParameterThenRedirected(
|
|
|
|
|
doFilterWhenAuthorizationRequestInvalidParameterThenRedirect(
|
|
|
|
|
registeredClient,
|
|
|
|
|
PkceParameterNames.CODE_CHALLENGE,
|
|
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST,
|
|
|
|
|
@@ -321,7 +329,7 @@ public class OAuth2AuthorizationEndpointFilterTests {
|
|
|
|
|
when(this.registeredClientRepository.findByClientId((eq(registeredClient.getClientId()))))
|
|
|
|
|
.thenReturn(registeredClient);
|
|
|
|
|
|
|
|
|
|
doFilterWhenAuthorizationRequestInvalidParameterThenRedirected(
|
|
|
|
|
doFilterWhenAuthorizationRequestInvalidParameterThenRedirect(
|
|
|
|
|
registeredClient,
|
|
|
|
|
PkceParameterNames.CODE_CHALLENGE_METHOD,
|
|
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST,
|
|
|
|
|
@@ -338,7 +346,7 @@ public class OAuth2AuthorizationEndpointFilterTests {
|
|
|
|
|
when(this.registeredClientRepository.findByClientId((eq(registeredClient.getClientId()))))
|
|
|
|
|
.thenReturn(registeredClient);
|
|
|
|
|
|
|
|
|
|
doFilterWhenAuthorizationRequestInvalidParameterThenRedirected(
|
|
|
|
|
doFilterWhenAuthorizationRequestInvalidParameterThenRedirect(
|
|
|
|
|
registeredClient,
|
|
|
|
|
PkceParameterNames.CODE_CHALLENGE_METHOD,
|
|
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST,
|
|
|
|
|
@@ -357,7 +365,7 @@ public class OAuth2AuthorizationEndpointFilterTests {
|
|
|
|
|
when(this.registeredClientRepository.findByClientId((eq(registeredClient.getClientId()))))
|
|
|
|
|
.thenReturn(registeredClient);
|
|
|
|
|
|
|
|
|
|
doFilterWhenAuthorizationRequestInvalidParameterThenRedirected(
|
|
|
|
|
doFilterWhenAuthorizationRequestInvalidParameterThenRedirect(
|
|
|
|
|
registeredClient,
|
|
|
|
|
PkceParameterNames.CODE_CHALLENGE_METHOD,
|
|
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST,
|
|
|
|
|
@@ -374,7 +382,7 @@ public class OAuth2AuthorizationEndpointFilterTests {
|
|
|
|
|
when(this.registeredClientRepository.findByClientId((eq(registeredClient.getClientId()))))
|
|
|
|
|
.thenReturn(registeredClient);
|
|
|
|
|
|
|
|
|
|
doFilterWhenAuthorizationRequestInvalidParameterThenRedirected(
|
|
|
|
|
doFilterWhenAuthorizationRequestInvalidParameterThenRedirect(
|
|
|
|
|
registeredClient,
|
|
|
|
|
PkceParameterNames.CODE_CHALLENGE_METHOD,
|
|
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST,
|
|
|
|
|
@@ -432,6 +440,10 @@ public class OAuth2AuthorizationEndpointFilterTests {
|
|
|
|
|
|
|
|
|
|
OAuth2AuthorizationRequest authorizationRequest = authorization.getAttribute(OAuth2AuthorizationAttributeNames.AUTHORIZATION_REQUEST);
|
|
|
|
|
assertThat(authorizationRequest).isNotNull();
|
|
|
|
|
|
|
|
|
|
Set<String> authorizedScopes = authorization.getAttribute(OAuth2AuthorizationAttributeNames.AUTHORIZED_SCOPES);
|
|
|
|
|
assertThat(authorizedScopes).isEqualTo(authorizationRequest.getScopes());
|
|
|
|
|
|
|
|
|
|
assertThat(authorizationRequest.getAuthorizationUri()).isEqualTo("http://localhost/oauth2/authorize");
|
|
|
|
|
assertThat(authorizationRequest.getGrantType()).isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE);
|
|
|
|
|
assertThat(authorizationRequest.getResponseType()).isEqualTo(OAuth2AuthorizationResponseType.CODE);
|
|
|
|
|
@@ -467,9 +479,19 @@ public class OAuth2AuthorizationEndpointFilterTests {
|
|
|
|
|
verify(this.authorizationService).save(authorizationCaptor.capture());
|
|
|
|
|
|
|
|
|
|
OAuth2Authorization authorization = authorizationCaptor.getValue();
|
|
|
|
|
OAuth2AuthorizationRequest authorizationRequest = authorization.getAttribute(OAuth2AuthorizationAttributeNames.AUTHORIZATION_REQUEST);
|
|
|
|
|
assertThat(authorizationRequest.getClientId()).isEqualTo(registeredClient.getClientId());
|
|
|
|
|
assertThat(authorization.getRegisteredClientId()).isEqualTo(registeredClient.getId());
|
|
|
|
|
assertThat(authorization.getPrincipalName()).isEqualTo(this.authentication.getPrincipal().toString());
|
|
|
|
|
|
|
|
|
|
String code = authorization.getAttribute(OAuth2AuthorizationAttributeNames.CODE);
|
|
|
|
|
assertThat(code).isNotNull();
|
|
|
|
|
|
|
|
|
|
OAuth2AuthorizationRequest authorizationRequest = authorization.getAttribute(OAuth2AuthorizationAttributeNames.AUTHORIZATION_REQUEST);
|
|
|
|
|
assertThat(authorizationRequest).isNotNull();
|
|
|
|
|
|
|
|
|
|
Set<String> authorizedScopes = authorization.getAttribute(OAuth2AuthorizationAttributeNames.AUTHORIZED_SCOPES);
|
|
|
|
|
assertThat(authorizedScopes).isEqualTo(authorizationRequest.getScopes());
|
|
|
|
|
|
|
|
|
|
assertThat(authorizationRequest.getClientId()).isEqualTo(registeredClient.getClientId());
|
|
|
|
|
assertThat(authorizationRequest.getAdditionalParameters())
|
|
|
|
|
.size()
|
|
|
|
|
.isEqualTo(2)
|
|
|
|
|
@@ -478,6 +500,271 @@ public class OAuth2AuthorizationEndpointFilterTests {
|
|
|
|
|
.containsEntry(PkceParameterNames.CODE_CHALLENGE_METHOD, "S256");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void doFilterWhenUserConsentRequiredAndAuthorizationRequestValidThenUserConsentResponse() throws Exception {
|
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient()
|
|
|
|
|
.clientSettings(new ClientSettings().requireUserConsent(true))
|
|
|
|
|
.build();
|
|
|
|
|
when(this.registeredClientRepository.findByClientId((eq(registeredClient.getClientId()))))
|
|
|
|
|
.thenReturn(registeredClient);
|
|
|
|
|
|
|
|
|
|
MockHttpServletRequest request = createAuthorizationRequest(registeredClient);
|
|
|
|
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
|
|
|
|
FilterChain filterChain = mock(FilterChain.class);
|
|
|
|
|
|
|
|
|
|
this.filter.doFilter(request, response, filterChain);
|
|
|
|
|
|
|
|
|
|
verifyNoInteractions(filterChain);
|
|
|
|
|
|
|
|
|
|
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
|
|
|
|
|
assertThat(response.getContentType().equals(new MediaType("text", "html", StandardCharsets.UTF_8).toString()));
|
|
|
|
|
|
|
|
|
|
ArgumentCaptor<OAuth2Authorization> authorizationCaptor = ArgumentCaptor.forClass(OAuth2Authorization.class);
|
|
|
|
|
|
|
|
|
|
verify(this.authorizationService).save(authorizationCaptor.capture());
|
|
|
|
|
|
|
|
|
|
OAuth2Authorization authorization = authorizationCaptor.getValue();
|
|
|
|
|
assertThat(authorization.getRegisteredClientId()).isEqualTo(registeredClient.getId());
|
|
|
|
|
assertThat(authorization.getPrincipalName()).isEqualTo(this.authentication.getPrincipal().toString());
|
|
|
|
|
|
|
|
|
|
String state = authorization.getAttribute(OAuth2AuthorizationAttributeNames.STATE);
|
|
|
|
|
assertThat(state).isNotNull();
|
|
|
|
|
|
|
|
|
|
Set<String> authorizedScopes = authorization.getAttribute(OAuth2AuthorizationAttributeNames.AUTHORIZED_SCOPES);
|
|
|
|
|
assertThat(authorizedScopes).isNull();
|
|
|
|
|
|
|
|
|
|
OAuth2AuthorizationRequest authorizationRequest = authorization.getAttribute(OAuth2AuthorizationAttributeNames.AUTHORIZATION_REQUEST);
|
|
|
|
|
assertThat(authorizationRequest).isNotNull();
|
|
|
|
|
assertThat(authorizationRequest.getAuthorizationUri()).isEqualTo("http://localhost/oauth2/authorize");
|
|
|
|
|
assertThat(authorizationRequest.getGrantType()).isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE);
|
|
|
|
|
assertThat(authorizationRequest.getResponseType()).isEqualTo(OAuth2AuthorizationResponseType.CODE);
|
|
|
|
|
assertThat(authorizationRequest.getClientId()).isEqualTo(registeredClient.getClientId());
|
|
|
|
|
assertThat(authorizationRequest.getRedirectUri()).isEqualTo(registeredClient.getRedirectUris().iterator().next());
|
|
|
|
|
assertThat(authorizationRequest.getScopes()).containsExactlyInAnyOrderElementsOf(registeredClient.getScopes());
|
|
|
|
|
assertThat(authorizationRequest.getState()).isEqualTo("state");
|
|
|
|
|
assertThat(authorizationRequest.getAdditionalParameters()).isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void doFilterWhenUserConsentRequestMissingStateThenInvalidRequestError() throws Exception {
|
|
|
|
|
doFilterWhenUserConsentRequestInvalidParameterThenError(
|
|
|
|
|
TestRegisteredClients.registeredClient().build(),
|
|
|
|
|
OAuth2ParameterNames.STATE,
|
|
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST,
|
|
|
|
|
request -> request.removeParameter(OAuth2ParameterNames.STATE));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void doFilterWhenUserConsentRequestMultipleStateThenInvalidRequestError() throws Exception {
|
|
|
|
|
doFilterWhenUserConsentRequestInvalidParameterThenError(
|
|
|
|
|
TestRegisteredClients.registeredClient().build(),
|
|
|
|
|
OAuth2ParameterNames.STATE,
|
|
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST,
|
|
|
|
|
request -> request.addParameter(OAuth2ParameterNames.STATE, "state-2"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void doFilterWhenUserConsentRequestInvalidStateThenInvalidRequestError() throws Exception {
|
|
|
|
|
doFilterWhenUserConsentRequestInvalidParameterThenError(
|
|
|
|
|
TestRegisteredClients.registeredClient().build(),
|
|
|
|
|
OAuth2ParameterNames.STATE,
|
|
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST,
|
|
|
|
|
request -> request.setParameter(OAuth2ParameterNames.STATE, "invalid"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void doFilterWhenUserConsentRequestNotAuthenticatedThenInvalidRequestError() throws Exception {
|
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
|
|
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
|
|
.thenReturn(registeredClient);
|
|
|
|
|
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build();
|
|
|
|
|
when(this.authorizationService.findByToken(eq("state"), eq(new TokenType(OAuth2AuthorizationAttributeNames.STATE))))
|
|
|
|
|
.thenReturn(authorization);
|
|
|
|
|
|
|
|
|
|
this.authentication.setAuthenticated(false);
|
|
|
|
|
|
|
|
|
|
doFilterWhenUserConsentRequestInvalidParameterThenError(
|
|
|
|
|
registeredClient,
|
|
|
|
|
OAuth2ParameterNames.STATE,
|
|
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST,
|
|
|
|
|
request -> {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void doFilterWhenUserConsentRequestInvalidPrincipalThenInvalidRequestError() throws Exception {
|
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
|
|
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
|
|
.thenReturn(registeredClient);
|
|
|
|
|
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build();
|
|
|
|
|
when(this.authorizationService.findByToken(eq("state"), eq(new TokenType(OAuth2AuthorizationAttributeNames.STATE))))
|
|
|
|
|
.thenReturn(authorization);
|
|
|
|
|
|
|
|
|
|
this.authentication = new TestingAuthenticationToken("other-principal", "password");
|
|
|
|
|
this.authentication.setAuthenticated(true);
|
|
|
|
|
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
|
|
|
|
|
securityContext.setAuthentication(this.authentication);
|
|
|
|
|
SecurityContextHolder.setContext(securityContext);
|
|
|
|
|
|
|
|
|
|
doFilterWhenUserConsentRequestInvalidParameterThenError(
|
|
|
|
|
registeredClient,
|
|
|
|
|
OAuth2ParameterNames.STATE,
|
|
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST,
|
|
|
|
|
request -> {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void doFilterWhenUserConsentRequestMissingClientIdThenInvalidRequestError() throws Exception {
|
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
|
|
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
|
|
.thenReturn(registeredClient);
|
|
|
|
|
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient)
|
|
|
|
|
.principalName(this.authentication.getName())
|
|
|
|
|
.build();
|
|
|
|
|
when(this.authorizationService.findByToken(eq("state"), eq(new TokenType(OAuth2AuthorizationAttributeNames.STATE))))
|
|
|
|
|
.thenReturn(authorization);
|
|
|
|
|
|
|
|
|
|
doFilterWhenUserConsentRequestInvalidParameterThenError(
|
|
|
|
|
registeredClient,
|
|
|
|
|
OAuth2ParameterNames.CLIENT_ID,
|
|
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST,
|
|
|
|
|
request -> request.removeParameter(OAuth2ParameterNames.CLIENT_ID));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void doFilterWhenUserConsentRequestMultipleClientIdThenInvalidRequestError() throws Exception {
|
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
|
|
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
|
|
.thenReturn(registeredClient);
|
|
|
|
|
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient)
|
|
|
|
|
.principalName(this.authentication.getName())
|
|
|
|
|
.build();
|
|
|
|
|
when(this.authorizationService.findByToken(eq("state"), eq(new TokenType(OAuth2AuthorizationAttributeNames.STATE))))
|
|
|
|
|
.thenReturn(authorization);
|
|
|
|
|
|
|
|
|
|
doFilterWhenUserConsentRequestInvalidParameterThenError(
|
|
|
|
|
TestRegisteredClients.registeredClient().build(),
|
|
|
|
|
OAuth2ParameterNames.CLIENT_ID,
|
|
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST,
|
|
|
|
|
request -> request.addParameter(OAuth2ParameterNames.CLIENT_ID, "client-2"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void doFilterWhenUserConsentRequestInvalidClientIdThenInvalidRequestError() throws Exception {
|
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
|
|
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
|
|
.thenReturn(registeredClient);
|
|
|
|
|
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient)
|
|
|
|
|
.principalName(this.authentication.getName())
|
|
|
|
|
.build();
|
|
|
|
|
when(this.authorizationService.findByToken(eq("state"), eq(new TokenType(OAuth2AuthorizationAttributeNames.STATE))))
|
|
|
|
|
.thenReturn(authorization);
|
|
|
|
|
|
|
|
|
|
doFilterWhenUserConsentRequestInvalidParameterThenError(
|
|
|
|
|
registeredClient,
|
|
|
|
|
OAuth2ParameterNames.CLIENT_ID,
|
|
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST,
|
|
|
|
|
request -> request.setParameter(OAuth2ParameterNames.CLIENT_ID, "invalid"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void doFilterWhenUserConsentRequestDoesNotMatchClientThenInvalidRequestError() throws Exception {
|
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
|
|
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
|
|
.thenReturn(registeredClient);
|
|
|
|
|
RegisteredClient otherRegisteredClient = TestRegisteredClients.registeredClient2().build();
|
|
|
|
|
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(otherRegisteredClient)
|
|
|
|
|
.principalName(this.authentication.getName())
|
|
|
|
|
.build();
|
|
|
|
|
when(this.authorizationService.findByToken(eq("state"), eq(new TokenType(OAuth2AuthorizationAttributeNames.STATE))))
|
|
|
|
|
.thenReturn(authorization);
|
|
|
|
|
|
|
|
|
|
doFilterWhenUserConsentRequestInvalidParameterThenError(
|
|
|
|
|
registeredClient,
|
|
|
|
|
OAuth2ParameterNames.CLIENT_ID,
|
|
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST,
|
|
|
|
|
request -> {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void doFilterWhenUserConsentRequestInvalidScopeThenInvalidScopeError() throws Exception {
|
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
|
|
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
|
|
.thenReturn(registeredClient);
|
|
|
|
|
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient)
|
|
|
|
|
.principalName(this.authentication.getName())
|
|
|
|
|
.build();
|
|
|
|
|
when(this.authorizationService.findByToken(eq("state"), eq(new TokenType(OAuth2AuthorizationAttributeNames.STATE))))
|
|
|
|
|
.thenReturn(authorization);
|
|
|
|
|
|
|
|
|
|
doFilterWhenUserConsentRequestInvalidParameterThenRedirect(
|
|
|
|
|
registeredClient,
|
|
|
|
|
OAuth2ParameterNames.SCOPE,
|
|
|
|
|
OAuth2ErrorCodes.INVALID_SCOPE,
|
|
|
|
|
DEFAULT_ERROR_URI,
|
|
|
|
|
request -> {
|
|
|
|
|
request.addParameter(OAuth2ParameterNames.SCOPE, "invalid-scope");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void doFilterWhenUserConsentRequestNotApprovedThenAccessDeniedError() throws Exception {
|
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
|
|
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
|
|
.thenReturn(registeredClient);
|
|
|
|
|
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient)
|
|
|
|
|
.principalName(this.authentication.getName())
|
|
|
|
|
.build();
|
|
|
|
|
when(this.authorizationService.findByToken(eq("state"), eq(new TokenType(OAuth2AuthorizationAttributeNames.STATE))))
|
|
|
|
|
.thenReturn(authorization);
|
|
|
|
|
|
|
|
|
|
doFilterWhenUserConsentRequestInvalidParameterThenRedirect(
|
|
|
|
|
registeredClient,
|
|
|
|
|
OAuth2ParameterNames.CLIENT_ID,
|
|
|
|
|
OAuth2ErrorCodes.ACCESS_DENIED,
|
|
|
|
|
DEFAULT_ERROR_URI,
|
|
|
|
|
request -> request.removeParameter("consent_action"));
|
|
|
|
|
|
|
|
|
|
verify(this.authorizationService).remove(eq(authorization));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void doFilterWhenUserConsentRequestApprovedThenAuthorizationResponse() throws Exception {
|
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
|
|
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
|
|
.thenReturn(registeredClient);
|
|
|
|
|
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient)
|
|
|
|
|
.principalName(this.authentication.getName())
|
|
|
|
|
.build();
|
|
|
|
|
when(this.authorizationService.findByToken(eq("state"), eq(new TokenType(OAuth2AuthorizationAttributeNames.STATE))))
|
|
|
|
|
.thenReturn(authorization);
|
|
|
|
|
|
|
|
|
|
MockHttpServletRequest request = createUserConsentRequest(registeredClient);
|
|
|
|
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
|
|
|
|
FilterChain filterChain = mock(FilterChain.class);
|
|
|
|
|
|
|
|
|
|
this.filter.doFilter(request, response, filterChain);
|
|
|
|
|
|
|
|
|
|
verifyNoInteractions(filterChain);
|
|
|
|
|
|
|
|
|
|
assertThat(response.getStatus()).isEqualTo(HttpStatus.FOUND.value());
|
|
|
|
|
assertThat(response.getRedirectedUrl()).matches("https://example.com\\?code=.{15,}&state=state");
|
|
|
|
|
|
|
|
|
|
ArgumentCaptor<OAuth2Authorization> authorizationCaptor = ArgumentCaptor.forClass(OAuth2Authorization.class);
|
|
|
|
|
|
|
|
|
|
verify(this.authorizationService).save(authorizationCaptor.capture());
|
|
|
|
|
|
|
|
|
|
OAuth2Authorization updatedAuthorization = authorizationCaptor.getValue();
|
|
|
|
|
assertThat(updatedAuthorization.getRegisteredClientId()).isEqualTo(registeredClient.getId());
|
|
|
|
|
assertThat(updatedAuthorization.getPrincipalName()).isEqualTo(this.authentication.getPrincipal().toString());
|
|
|
|
|
assertThat(updatedAuthorization.getAccessToken()).isNotNull();
|
|
|
|
|
assertThat(updatedAuthorization.<String>getAttribute(OAuth2AuthorizationAttributeNames.STATE)).isNull();
|
|
|
|
|
assertThat(updatedAuthorization.<String>getAttribute(OAuth2AuthorizationAttributeNames.CODE)).isNotNull();
|
|
|
|
|
assertThat(updatedAuthorization.<OAuth2AuthorizationRequest>getAttribute(OAuth2AuthorizationAttributeNames.AUTHORIZATION_REQUEST))
|
|
|
|
|
.isEqualTo(authorization.<OAuth2AuthorizationRequest>getAttribute(OAuth2AuthorizationAttributeNames.AUTHORIZATION_REQUEST));
|
|
|
|
|
assertThat(updatedAuthorization.<Set<String>>getAttribute(OAuth2AuthorizationAttributeNames.AUTHORIZED_SCOPES))
|
|
|
|
|
.isEqualTo(registeredClient.getScopes());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void doFilterWhenAuthorizationRequestInvalidParameterThenError(RegisteredClient registeredClient,
|
|
|
|
|
String parameterName, String errorCode) throws Exception {
|
|
|
|
|
doFilterWhenAuthorizationRequestInvalidParameterThenError(registeredClient, parameterName, errorCode, request -> {});
|
|
|
|
|
@@ -486,7 +773,36 @@ public class OAuth2AuthorizationEndpointFilterTests {
|
|
|
|
|
private void doFilterWhenAuthorizationRequestInvalidParameterThenError(RegisteredClient registeredClient,
|
|
|
|
|
String parameterName, String errorCode, Consumer<MockHttpServletRequest> requestConsumer) throws Exception {
|
|
|
|
|
|
|
|
|
|
MockHttpServletRequest request = createAuthorizationRequest(registeredClient);
|
|
|
|
|
doFilterWhenRequestInvalidParameterThenError(createAuthorizationRequest(registeredClient),
|
|
|
|
|
parameterName, errorCode, requestConsumer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void doFilterWhenAuthorizationRequestInvalidParameterThenRedirect(RegisteredClient registeredClient,
|
|
|
|
|
String parameterName, String errorCode, String errorUri,
|
|
|
|
|
Consumer<MockHttpServletRequest> requestConsumer) throws Exception {
|
|
|
|
|
|
|
|
|
|
doFilterWhenRequestInvalidParameterThenRedirect(createAuthorizationRequest(registeredClient),
|
|
|
|
|
parameterName, errorCode, errorUri, requestConsumer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void doFilterWhenUserConsentRequestInvalidParameterThenError(RegisteredClient registeredClient,
|
|
|
|
|
String parameterName, String errorCode, Consumer<MockHttpServletRequest> requestConsumer) throws Exception {
|
|
|
|
|
|
|
|
|
|
doFilterWhenRequestInvalidParameterThenError(createUserConsentRequest(registeredClient),
|
|
|
|
|
parameterName, errorCode, requestConsumer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void doFilterWhenUserConsentRequestInvalidParameterThenRedirect(RegisteredClient registeredClient,
|
|
|
|
|
String parameterName, String errorCode, String errorUri,
|
|
|
|
|
Consumer<MockHttpServletRequest> requestConsumer) throws Exception {
|
|
|
|
|
|
|
|
|
|
doFilterWhenRequestInvalidParameterThenRedirect(createUserConsentRequest(registeredClient),
|
|
|
|
|
parameterName, errorCode, errorUri, requestConsumer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void doFilterWhenRequestInvalidParameterThenError(MockHttpServletRequest request,
|
|
|
|
|
String parameterName, String errorCode, Consumer<MockHttpServletRequest> requestConsumer) throws Exception {
|
|
|
|
|
|
|
|
|
|
requestConsumer.accept(request);
|
|
|
|
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
|
|
|
|
FilterChain filterChain = mock(FilterChain.class);
|
|
|
|
|
@@ -499,11 +815,10 @@ public class OAuth2AuthorizationEndpointFilterTests {
|
|
|
|
|
assertThat(response.getErrorMessage()).isEqualTo("[" + errorCode + "] OAuth 2.0 Parameter: " + parameterName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void doFilterWhenAuthorizationRequestInvalidParameterThenRedirected(RegisteredClient registeredClient,
|
|
|
|
|
private void doFilterWhenRequestInvalidParameterThenRedirect(MockHttpServletRequest request,
|
|
|
|
|
String parameterName, String errorCode, String errorUri,
|
|
|
|
|
Consumer<MockHttpServletRequest> requestConsumer) throws Exception {
|
|
|
|
|
|
|
|
|
|
MockHttpServletRequest request = createAuthorizationRequest(registeredClient);
|
|
|
|
|
requestConsumer.accept(request);
|
|
|
|
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
|
|
|
|
FilterChain filterChain = mock(FilterChain.class);
|
|
|
|
|
@@ -541,4 +856,19 @@ public class OAuth2AuthorizationEndpointFilterTests {
|
|
|
|
|
request.addParameter(PkceParameterNames.CODE_CHALLENGE, "code-challenge");
|
|
|
|
|
request.addParameter(PkceParameterNames.CODE_CHALLENGE_METHOD, "S256");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static MockHttpServletRequest createUserConsentRequest(RegisteredClient registeredClient) {
|
|
|
|
|
String requestUri = OAuth2AuthorizationEndpointFilter.DEFAULT_AUTHORIZATION_ENDPOINT_URI;
|
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest("POST", requestUri);
|
|
|
|
|
request.setServletPath(requestUri);
|
|
|
|
|
|
|
|
|
|
request.addParameter(OAuth2ParameterNames.CLIENT_ID, registeredClient.getClientId());
|
|
|
|
|
request.addParameter(OAuth2ParameterNames.STATE, "state");
|
|
|
|
|
for (String scope : registeredClient.getScopes()) {
|
|
|
|
|
request.addParameter(OAuth2ParameterNames.SCOPE, scope);
|
|
|
|
|
}
|
|
|
|
|
request.addParameter("consent_action", "approve");
|
|
|
|
|
|
|
|
|
|
return request;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|