Polish gh-280

This commit is contained in:
Joe Grandja
2021-06-03 10:07:53 -04:00
parent 683dad1443
commit f3cb8f758c
16 changed files with 404 additions and 388 deletions

View File

@@ -122,7 +122,7 @@ public class OAuth2AuthorizationCodeGrantTests {
private static ProviderSettings providerSettings;
private static HttpMessageConverter<OAuth2AccessTokenResponse> accessTokenHttpResponseConverter =
new OAuth2AccessTokenResponseHttpMessageConverter();
private static String consentPage = "/custom-consent";
private static String consentPage = "/oauth2/consent";
@Rule
public final SpringTestRule spring = new SpringTestRule();
@@ -360,15 +360,13 @@ public class OAuth2AuthorizationCodeGrantTests {
.getResponse()
.getContentAsString();
assertThat(consentPage).contains("Consent required");
assertThat(consentPage).contains(scopeCheckbox("message.read"));
assertThat(consentPage).contains(scopeCheckbox("message.write"));
}
@Test
public void requestWhenConsentRequestReturnAccessTokenResponse() throws Exception {
final String stateParameter = "consent-state";
public void requestWhenConsentRequestThenReturnAccessTokenResponse() throws Exception {
this.spring.register(AuthorizationServerConfiguration.class).autowire();
RegisteredClient registeredClient = TestRegisteredClients.registeredClient()
@@ -381,14 +379,15 @@ public class OAuth2AuthorizationCodeGrantTests {
.build();
when(registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
.thenReturn(registeredClient);
OAuth2Authorization stateTokenAuthorization = TestOAuth2Authorizations.authorization(registeredClient)
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient)
.principalName("user")
.build();
final String stateParameter = "state";
when(authorizationService.findByToken(
eq(stateParameter),
eq(new OAuth2TokenType(OAuth2ParameterNames.STATE))))
.thenReturn(stateTokenAuthorization);
eq(stateParameter), eq(new OAuth2TokenType(OAuth2ParameterNames.STATE))))
.thenReturn(authorization);
MvcResult mvcResult = this.mvc.perform(post(OAuth2AuthorizationEndpointFilter.DEFAULT_AUTHORIZATION_ENDPOINT_URI)
.param(OAuth2ParameterNames.CLIENT_ID, registeredClient.getClientId())
@@ -480,10 +479,10 @@ public class OAuth2AuthorizationCodeGrantTests {
private static String getAuthorizationHeader(RegisteredClient registeredClient) throws Exception {
String clientId = registeredClient.getClientId();
String secret = registeredClient.getClientSecret();
String clientSecret = registeredClient.getClientSecret();
clientId = URLEncoder.encode(clientId, StandardCharsets.UTF_8.name());
secret = URLEncoder.encode(secret, StandardCharsets.UTF_8.name());
String credentialsString = clientId + ":" + secret;
clientSecret = URLEncoder.encode(clientSecret, StandardCharsets.UTF_8.name());
String credentialsString = clientId + ":" + clientSecret;
byte[] encodedBytes = Base64.getEncoder().encode(credentialsString.getBytes(StandardCharsets.UTF_8));
return "Basic " + new String(encodedBytes, StandardCharsets.UTF_8);
}

View File

@@ -1,152 +0,0 @@
/*
* Copyright 2020-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.oauth2.server.authorization;
import org.junit.Before;
import org.junit.Test;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests for {@link InMemoryOAuth2AuthorizationConsentService}.
*
* @author Daniel Garnier-Moiroux
*/
public class InMemoryOAuth2AuthorizationConsentServiceTest {
private InMemoryOAuth2AuthorizationConsentService consentService;
private static final String CLIENT_ID = "client-id";
private static final String PRINCIPAL_NAME = "principal-name";
private static final OAuth2AuthorizationConsent CONSENT = OAuth2AuthorizationConsent
.withId(CLIENT_ID, PRINCIPAL_NAME)
.authority(new SimpleGrantedAuthority("some.authority"))
.build();
@Before
public void setUp() throws Exception {
this.consentService = new InMemoryOAuth2AuthorizationConsentService();
this.consentService.save(CONSENT);
}
@Test
public void constructorVaragsWhenAuthorizationConsentNullThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new InMemoryOAuth2AuthorizationConsentService((OAuth2AuthorizationConsent) null))
.withMessage("authorizationConsent cannot be null");
}
@Test
public void constructorListWhenAuthorizationConsentsNullThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new InMemoryOAuth2AuthorizationConsentService((List<OAuth2AuthorizationConsent>) null))
.withMessage("authorizationConsents cannot be null");
}
@Test
public void constructorWhenDuplicateAuthorizationConsentsThenThrowIllegalArgumentException() {
OAuth2AuthorizationConsent authorizationConsent = OAuth2AuthorizationConsent.withId("client-id", "principal-name")
.scope("thing.write") // must have at least one scope
.build();
assertThatIllegalArgumentException()
.isThrownBy(() -> new InMemoryOAuth2AuthorizationConsentService(authorizationConsent, authorizationConsent))
.withMessage("The authorizationConsent must be unique. Found duplicate, with registered client id: [client-id] and principal name: [principal-name]");
}
@Test
public void saveWhenConsentNullThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.consentService.save(null))
.withMessage("authorizationConsent cannot be null");
}
@Test
public void saveWhenConsentNewThenSaved() {
OAuth2AuthorizationConsent expectedConsent = OAuth2AuthorizationConsent
.withId("new-client", "new-principal")
.authority(new SimpleGrantedAuthority("new.authority"))
.build();
this.consentService.save(expectedConsent);
OAuth2AuthorizationConsent consent =
this.consentService.findById("new-client", "new-principal");
assertThat(consent).isEqualTo(expectedConsent);
}
@Test
public void saveWhenConsentExistsThenUpdated() {
OAuth2AuthorizationConsent expectedConsent = OAuth2AuthorizationConsent
.from(CONSENT)
.authority(new SimpleGrantedAuthority("new.authority"))
.build();
this.consentService.save(expectedConsent);
OAuth2AuthorizationConsent consent =
this.consentService.findById(CLIENT_ID, PRINCIPAL_NAME);
assertThat(consent).isEqualTo(expectedConsent);
assertThat(consent).isNotEqualTo(CONSENT);
}
@Test
public void removeNullThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.consentService.remove(null))
.withMessage("authorizationConsent cannot be null");
}
@Test
public void removeWhenConsentProvidedThenRemoved() {
this.consentService.remove(CONSENT);
assertThat(this.consentService.findById(CLIENT_ID, PRINCIPAL_NAME))
.isNull();
}
@Test
public void findWhenRegisteredClientIdNullThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.consentService.findById(null, "some-user"))
.withMessage("registeredClientId cannot be empty");
}
@Test
public void findWhenPrincipalNameNullThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.consentService.findById("some-client", null))
.withMessage("principalName cannot be empty");
}
@Test
public void findWhenConsentExistsThenFound() {
assertThat(this.consentService.findById(CLIENT_ID, PRINCIPAL_NAME))
.isEqualTo(CONSENT);
}
@Test
public void findWhenConsentDoesNotExistThenNull() {
this.consentService.save(CONSENT);
assertThat(this.consentService.findById("unknown-client", PRINCIPAL_NAME)).isNull();
assertThat(this.consentService.findById(CLIENT_ID, "unkown-user")).isNull();
}
}

View File

@@ -0,0 +1,148 @@
/*
* Copyright 2020-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.oauth2.server.authorization;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests for {@link InMemoryOAuth2AuthorizationConsentService}.
*
* @author Daniel Garnier-Moiroux
*/
public class InMemoryOAuth2AuthorizationConsentServiceTests {
private static final String REGISTERED_CLIENT_ID = "registered-client-id";
private static final String PRINCIPAL_NAME = "principal-name";
private static final OAuth2AuthorizationConsent AUTHORIZATION_CONSENT =
OAuth2AuthorizationConsent.withId(REGISTERED_CLIENT_ID, PRINCIPAL_NAME)
.authority(new SimpleGrantedAuthority("some.authority"))
.build();
private InMemoryOAuth2AuthorizationConsentService authorizationConsentService;
@Before
public void setUp() {
this.authorizationConsentService = new InMemoryOAuth2AuthorizationConsentService();
this.authorizationConsentService.save(AUTHORIZATION_CONSENT);
}
@Test
public void constructorVarargsWhenAuthorizationConsentNullThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new InMemoryOAuth2AuthorizationConsentService((OAuth2AuthorizationConsent) null))
.withMessage("authorizationConsent cannot be null");
}
@Test
public void constructorListWhenAuthorizationConsentsNullThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new InMemoryOAuth2AuthorizationConsentService((List<OAuth2AuthorizationConsent>) null))
.withMessage("authorizationConsents cannot be null");
}
@Test
public void constructorWhenDuplicateAuthorizationConsentsThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new InMemoryOAuth2AuthorizationConsentService(AUTHORIZATION_CONSENT, AUTHORIZATION_CONSENT))
.withMessage("The authorizationConsent must be unique. Found duplicate, with registered client id: [registered-client-id] and principal name: [principal-name]");
}
@Test
public void saveWhenAuthorizationConsentNullThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.authorizationConsentService.save(null))
.withMessage("authorizationConsent cannot be null");
}
@Test
public void saveWhenAuthorizationConsentNewThenSaved() {
OAuth2AuthorizationConsent expectedAuthorizationConsent =
OAuth2AuthorizationConsent.withId("new-client", "new-principal")
.authority(new SimpleGrantedAuthority("new.authority"))
.build();
this.authorizationConsentService.save(expectedAuthorizationConsent);
OAuth2AuthorizationConsent authorizationConsent =
this.authorizationConsentService.findById("new-client", "new-principal");
assertThat(authorizationConsent).isEqualTo(expectedAuthorizationConsent);
}
@Test
public void saveWhenAuthorizationConsentExistsThenUpdated() {
OAuth2AuthorizationConsent expectedAuthorizationConsent =
OAuth2AuthorizationConsent.from(AUTHORIZATION_CONSENT)
.authority(new SimpleGrantedAuthority("new.authority"))
.build();
this.authorizationConsentService.save(expectedAuthorizationConsent);
OAuth2AuthorizationConsent authorizationConsent =
this.authorizationConsentService.findById(
AUTHORIZATION_CONSENT.getRegisteredClientId(), AUTHORIZATION_CONSENT.getPrincipalName());
assertThat(authorizationConsent).isEqualTo(expectedAuthorizationConsent);
assertThat(authorizationConsent).isNotEqualTo(AUTHORIZATION_CONSENT);
}
@Test
public void removeWhenAuthorizationConsentNullThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.authorizationConsentService.remove(null))
.withMessage("authorizationConsent cannot be null");
}
@Test
public void removeWhenAuthorizationConsentProvidedThenRemoved() {
this.authorizationConsentService.remove(AUTHORIZATION_CONSENT);
assertThat(this.authorizationConsentService.findById(
AUTHORIZATION_CONSENT.getRegisteredClientId(), AUTHORIZATION_CONSENT.getPrincipalName()))
.isNull();
}
@Test
public void findByIdWhenRegisteredClientIdNullThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.authorizationConsentService.findById(null, "some-user"))
.withMessage("registeredClientId cannot be empty");
}
@Test
public void findByIdWhenPrincipalNameNullThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.authorizationConsentService.findById("some-client", null))
.withMessage("principalName cannot be empty");
}
@Test
public void findByIdWhenAuthorizationConsentExistsThenFound() {
assertThat(this.authorizationConsentService.findById(REGISTERED_CLIENT_ID, PRINCIPAL_NAME))
.isEqualTo(AUTHORIZATION_CONSENT);
}
@Test
public void findByIdWhenAuthorizationConsentDoesNotExistThenNull() {
this.authorizationConsentService.save(AUTHORIZATION_CONSENT);
assertThat(this.authorizationConsentService.findById("unknown-client", PRINCIPAL_NAME)).isNull();
assertThat(this.authorizationConsentService.findById(REGISTERED_CLIENT_ID, "unknown-user")).isNull();
}
}

View File

@@ -16,6 +16,7 @@
package org.springframework.security.oauth2.server.authorization;
import org.junit.Test;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import static org.assertj.core.api.Assertions.assertThat;
@@ -26,7 +27,8 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
*
* @author Daniel Garnier-Moiroux
*/
public class OAuth2AuthorizationConsentTest {
public class OAuth2AuthorizationConsentTests {
@Test
public void fromWhenAuthorizationConsentNullThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException()
@@ -35,14 +37,14 @@ public class OAuth2AuthorizationConsentTest {
}
@Test
public void withClientIdAndPrincipalWhenClientIdNullThenThrowIllegalArgumentException() {
public void withIdWhenRegisteredClientIdNullThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> OAuth2AuthorizationConsent.withId(null, "some-user"))
.withMessage("registeredClientId cannot be empty");
}
@Test
public void withClientIdAndPrincipalWhenPrincipalNullThenThrowIllegalArgumentException() {
public void withIdWhenPrincipalNameNullThenThrowIllegalArgumentException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> OAuth2AuthorizationConsent.withId("some-client", null))
.withMessage("principalName cannot be empty");
@@ -58,21 +60,21 @@ public class OAuth2AuthorizationConsentTest {
@Test
public void buildWhenAllAttributesAreProvidedThenAllAttributesAreSet() {
OAuth2AuthorizationConsent consent = OAuth2AuthorizationConsent
.withId("some-client", "some-user")
.scope("resource.read")
.scope("resource.write")
.authority(new SimpleGrantedAuthority("CLAIM_email"))
.build();
OAuth2AuthorizationConsent authorizationConsent =
OAuth2AuthorizationConsent.withId("some-client", "some-user")
.scope("resource.read")
.scope("resource.write")
.authority(new SimpleGrantedAuthority("CLAIM_email"))
.build();
assertThat(consent.getPrincipalName()).isEqualTo("some-user");
assertThat(consent.getRegisteredClientId()).isEqualTo("some-client");
assertThat(consent.getScopes())
assertThat(authorizationConsent.getRegisteredClientId()).isEqualTo("some-client");
assertThat(authorizationConsent.getPrincipalName()).isEqualTo("some-user");
assertThat(authorizationConsent.getScopes())
.containsExactlyInAnyOrder(
"resource.read",
"resource.write"
);
assertThat(consent.getAuthorities())
assertThat(authorizationConsent.getAuthorities())
.containsExactlyInAnyOrder(
new SimpleGrantedAuthority("SCOPE_resource.read"),
new SimpleGrantedAuthority("SCOPE_resource.write"),
@@ -82,18 +84,20 @@ public class OAuth2AuthorizationConsentTest {
@Test
public void fromWhenAuthorizationConsentProvidedThenCopied() {
OAuth2AuthorizationConsent previousConsent = OAuth2AuthorizationConsent
.withId("some-client", "some-principal")
.scope("first.scope")
.scope("second.scope")
.authority(new SimpleGrantedAuthority("CLAIM_email"))
.build();
OAuth2AuthorizationConsent previousAuthorizationConsent =
OAuth2AuthorizationConsent.withId("some-client", "some-principal")
.scope("first.scope")
.scope("second.scope")
.authority(new SimpleGrantedAuthority("CLAIM_email"))
.build();
OAuth2AuthorizationConsent consent = OAuth2AuthorizationConsent.from(previousConsent).build();
OAuth2AuthorizationConsent authorizationConsent =
OAuth2AuthorizationConsent.from(previousAuthorizationConsent)
.build();
assertThat(consent.getPrincipalName()).isEqualTo("some-principal");
assertThat(consent.getRegisteredClientId()).isEqualTo("some-client");
assertThat(consent.getAuthorities())
assertThat(authorizationConsent.getRegisteredClientId()).isEqualTo("some-client");
assertThat(authorizationConsent.getPrincipalName()).isEqualTo("some-principal");
assertThat(authorizationConsent.getAuthorities())
.containsExactlyInAnyOrder(
new SimpleGrantedAuthority("SCOPE_first.scope"),
new SimpleGrantedAuthority("SCOPE_second.scope"),
@@ -103,15 +107,16 @@ public class OAuth2AuthorizationConsentTest {
@Test
public void authoritiesThenCustomizesAuthorities() {
OAuth2AuthorizationConsent consent = OAuth2AuthorizationConsent
.withId("some-client", "some-user")
.authority(new SimpleGrantedAuthority("some.authority"))
.authorities(authorities -> {
authorities.clear();
authorities.add(new SimpleGrantedAuthority("other.authority"));
})
.build();
OAuth2AuthorizationConsent authorizationConsent =
OAuth2AuthorizationConsent.withId("some-client", "some-user")
.authority(new SimpleGrantedAuthority("some.authority"))
.authorities(authorities -> {
authorities.clear();
authorities.add(new SimpleGrantedAuthority("other.authority"));
})
.build();
assertThat(consent.getAuthorities()).containsExactly(new SimpleGrantedAuthority("other.authority"));
assertThat(authorizationConsent.getAuthorities()).containsExactly(new SimpleGrantedAuthority("other.authority"));
}
}

View File

@@ -33,6 +33,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
@@ -88,16 +89,17 @@ public class OAuth2AuthorizationEndpointFilterTests {
private static final String PKCE_ERROR_URI = "https://tools.ietf.org/html/rfc7636%23section-4.4.1";
private RegisteredClientRepository registeredClientRepository;
private OAuth2AuthorizationService authorizationService;
private OAuth2AuthorizationConsentService authorizationConsentService;
private OAuth2AuthorizationEndpointFilter filter;
private OAuth2AuthorizationConsentService consentService;
private TestingAuthenticationToken authentication;
@Before
public void setUp() {
this.registeredClientRepository = mock(RegisteredClientRepository.class);
this.authorizationService = mock(OAuth2AuthorizationService.class);
this.consentService = mock(OAuth2AuthorizationConsentService.class);
this.filter = new OAuth2AuthorizationEndpointFilter(this.registeredClientRepository, this.authorizationService, this.consentService);
this.authorizationConsentService = mock(OAuth2AuthorizationConsentService.class);
this.filter = new OAuth2AuthorizationEndpointFilter(
this.registeredClientRepository, this.authorizationService, this.authorizationConsentService);
this.authentication = new TestingAuthenticationToken("principalName", "password");
this.authentication.setAuthenticated(true);
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
@@ -112,28 +114,28 @@ public class OAuth2AuthorizationEndpointFilterTests {
@Test
public void constructorWhenRegisteredClientRepositoryNullThenThrowIllegalArgumentException() {
assertThatThrownBy(() -> new OAuth2AuthorizationEndpointFilter(null, this.authorizationService, this.consentService))
assertThatThrownBy(() -> new OAuth2AuthorizationEndpointFilter(null, this.authorizationService, this.authorizationConsentService))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("registeredClientRepository cannot be null");
}
@Test
public void constructorWhenAuthorizationServiceNullThenThrowIllegalArgumentException() {
assertThatThrownBy(() -> new OAuth2AuthorizationEndpointFilter(this.registeredClientRepository, null, this.consentService))
assertThatThrownBy(() -> new OAuth2AuthorizationEndpointFilter(this.registeredClientRepository, null, this.authorizationConsentService))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("authorizationService cannot be null");
}
@Test
public void constructorWhenConsentServiceNullThenThrowIllegalArgumentException() {
public void constructorWhenAuthorizationConsentServiceNullThenThrowIllegalArgumentException() {
assertThatThrownBy(() -> new OAuth2AuthorizationEndpointFilter(this.registeredClientRepository, this.authorizationService, (OAuth2AuthorizationConsentService) null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("consentService cannot be null");
.hasMessage("authorizationConsentService cannot be null");
}
@Test
public void constructorWhenAuthorizationEndpointUriNullThenThrowIllegalArgumentException() {
assertThatThrownBy(() -> new OAuth2AuthorizationEndpointFilter(this.registeredClientRepository, this.authorizationService, this.consentService, null))
assertThatThrownBy(() -> new OAuth2AuthorizationEndpointFilter(this.registeredClientRepository, this.authorizationService, this.authorizationConsentService, null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("authorizationEndpointUri cannot be empty");
}
@@ -592,7 +594,7 @@ public class OAuth2AuthorizationEndpointFilterTests {
RegisteredClient registeredClient = TestRegisteredClients.registeredClient()
.clientSettings(clientSettings -> clientSettings.requireUserConsent(true))
.build();
when(this.registeredClientRepository.findByClientId((eq(registeredClient.getClientId()))))
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
.thenReturn(registeredClient);
MockHttpServletRequest request = createAuthorizationRequest(registeredClient);
@@ -601,6 +603,8 @@ public class OAuth2AuthorizationEndpointFilterTests {
this.filter.doFilter(request, response, filterChain);
verifyNoInteractions(filterChain);
ArgumentCaptor<OAuth2Authorization> authorizationCaptor = ArgumentCaptor.forClass(OAuth2Authorization.class);
verify(this.authorizationService).save(authorizationCaptor.capture());
@@ -640,7 +644,7 @@ public class OAuth2AuthorizationEndpointFilterTests {
})
.clientSettings(clientSettings -> clientSettings.requireUserConsent(true))
.build();
when(this.registeredClientRepository.findByClientId((eq(registeredClient.getClientId()))))
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
.thenReturn(registeredClient);
MockHttpServletRequest request = createAuthorizationRequest(registeredClient);
@@ -656,8 +660,6 @@ public class OAuth2AuthorizationEndpointFilterTests {
assertThat(response.getContentAsString()).contains(scopeCheckbox("message.read"));
assertThat(response.getContentAsString()).contains(scopeCheckbox("message.write"));
verifyNoInteractions(filterChain);
}
@Test
@@ -673,19 +675,17 @@ public class OAuth2AuthorizationEndpointFilterTests {
})
.clientSettings(clientSettings -> clientSettings.requireUserConsent(true))
.build();
when(this.registeredClientRepository.findByClientId((eq(registeredClient.getClientId()))))
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
.thenReturn(registeredClient);
OAuth2AuthorizationConsent previousConsent = createConsent(
OAuth2AuthorizationConsent previousConsent = createAuthorizationConsent(
registeredClient.getClientId(),
this.authentication.getName(),
Arrays.asList(previouslyApprovedScope, unrelatedPreviouslyApprovedScope)
);
when(this.consentService.findById(
eq(registeredClient.getClientId()),
eq(this.authentication.getName())))
when(this.authorizationConsentService.findById(
eq(registeredClient.getId()), eq(this.authentication.getName())))
.thenReturn(previousConsent);
MockHttpServletRequest request = createAuthorizationRequest(registeredClient);
MockHttpServletResponse response = new MockHttpServletResponse();
FilterChain filterChain = mock(FilterChain.class);
@@ -704,7 +704,7 @@ public class OAuth2AuthorizationEndpointFilterTests {
@Test
public void doFilterWhenUserConsentRequiredAndCustomConsentUriAndAuthorizationRequestThenRedirects() throws Exception {
this.filter.setUserConsentUri("/consent");
this.filter.setUserConsentUri("/oauth2/consent");
RegisteredClient registeredClient = TestRegisteredClients.registeredClient()
.scopes(scopes -> {
scopes.clear();
@@ -713,7 +713,7 @@ public class OAuth2AuthorizationEndpointFilterTests {
})
.clientSettings(clientSettings -> clientSettings.requireUserConsent(true))
.build();
when(this.registeredClientRepository.findByClientId((eq(registeredClient.getClientId()))))
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
.thenReturn(registeredClient);
MockHttpServletRequest request = createAuthorizationRequest(registeredClient);
@@ -736,7 +736,7 @@ public class OAuth2AuthorizationEndpointFilterTests {
String[] redirectScopes = consentRedirectUri.getQueryParams().getFirst(OAuth2ParameterNames.SCOPE).split(" ");
String redirectState = consentRedirectUri.getQueryParams().getFirst(OAuth2ParameterNames.STATE);
assertThat(consentRedirectUri.getPath()).isEqualTo("/consent");
assertThat(consentRedirectUri.getPath()).isEqualTo("/oauth2/consent");
assertThat(consentRedirectUri.getQueryParams().getFirst(OAuth2ParameterNames.CLIENT_ID)).isEqualTo(registeredClient.getClientId());
assertThat(redirectScopes).containsExactlyInAnyOrder("message.read", "message.write");
assertThat(redirectState).isEqualTo(authorization.getAttribute(OAuth2ParameterNames.STATE));
@@ -752,15 +752,14 @@ public class OAuth2AuthorizationEndpointFilterTests {
})
.clientSettings(clientSettings -> clientSettings.requireUserConsent(true))
.build();
when(this.registeredClientRepository.findByClientId((eq(registeredClient.getClientId()))))
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
.thenReturn(registeredClient);
OAuth2AuthorizationConsent previousConsent = createConsent(
OAuth2AuthorizationConsent authorizationConsent = createAuthorizationConsent(
registeredClient.getClientId(), this.authentication.getName(), Arrays.asList("message.read", "message.write")
);
when(this.consentService.findById(
eq(registeredClient.getClientId()),
eq(this.authentication.getName())))
.thenReturn(previousConsent);
when(this.authorizationConsentService.findById(
eq(registeredClient.getId()), eq(this.authentication.getName())))
.thenReturn(authorizationConsent);
MockHttpServletRequest request = createAuthorizationRequest(registeredClient);
MockHttpServletResponse response = new MockHttpServletResponse();
@@ -1011,7 +1010,7 @@ public class OAuth2AuthorizationEndpointFilterTests {
.build();
when(this.authorizationService.findByToken(eq("state"), eq(STATE_TOKEN_TYPE)))
.thenReturn(authorization);
when(this.registeredClientRepository.findByClientId((eq(registeredClient.getClientId()))))
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
.thenReturn(registeredClient);
MockHttpServletRequest request = createUserConsentRequest(registeredClient);
@@ -1020,13 +1019,13 @@ public class OAuth2AuthorizationEndpointFilterTests {
this.filter.doFilter(request, response, filterChain);
ArgumentCaptor<OAuth2AuthorizationConsent> consentCaptor = ArgumentCaptor.forClass(OAuth2AuthorizationConsent.class);
ArgumentCaptor<OAuth2AuthorizationConsent> authorizationConsentCaptor = ArgumentCaptor.forClass(OAuth2AuthorizationConsent.class);
verify(this.consentService).save(consentCaptor.capture());
OAuth2AuthorizationConsent consent = consentCaptor.getValue();
assertThat(consent.getPrincipalName()).isEqualTo(this.authentication.getName());
assertThat(consent.getRegisteredClientId()).isEqualTo(registeredClient.getClientId());
assertThat(consent.getScopes()).containsExactlyInAnyOrder("message.read", "message.write");
verify(this.authorizationConsentService).save(authorizationConsentCaptor.capture());
OAuth2AuthorizationConsent authorizationConsent = authorizationConsentCaptor.getValue();
assertThat(authorizationConsent.getPrincipalName()).isEqualTo(this.authentication.getName());
assertThat(authorizationConsent.getRegisteredClientId()).isEqualTo(registeredClient.getId());
assertThat(authorizationConsent.getScopes()).containsExactlyInAnyOrder("message.read", "message.write");
}
@Test
@@ -1041,7 +1040,7 @@ public class OAuth2AuthorizationEndpointFilterTests {
.build();
when(this.authorizationService.findByToken(eq("state"), eq(STATE_TOKEN_TYPE)))
.thenReturn(authorization);
when(this.registeredClientRepository.findByClientId((eq(registeredClient.getClientId()))))
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
.thenReturn(registeredClient);
MockHttpServletRequest request = createUserConsentRequest(registeredClient);
@@ -1050,7 +1049,7 @@ public class OAuth2AuthorizationEndpointFilterTests {
this.filter.doFilter(request, response, filterChain);
verify(this.consentService, never()).save(any());
verify(this.authorizationConsentService, never()).save(any());
}
@Test
@@ -1069,18 +1068,17 @@ public class OAuth2AuthorizationEndpointFilterTests {
.build();
when(this.authorizationService.findByToken(eq("state"), eq(STATE_TOKEN_TYPE)))
.thenReturn(authorization);
when(this.registeredClientRepository.findByClientId((eq(registeredClient.getClientId()))))
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
.thenReturn(registeredClient);
OAuth2AuthorizationConsent previousConsent =
createConsent(
OAuth2AuthorizationConsent previousAuthorizationConsent =
createAuthorizationConsent(
registeredClient.getClientId(),
this.authentication.getName(),
Collections.singleton("message.read")
);
when(this.consentService.findById(
eq(registeredClient.getClientId()),
eq(this.authentication.getName())))
.thenReturn(previousConsent);
when(this.authorizationConsentService.findById(
eq(registeredClient.getId()), eq(this.authentication.getName())))
.thenReturn(previousAuthorizationConsent);
MockHttpServletRequest request = createUserConsentRequest(registeredClient);
MockHttpServletResponse response = new MockHttpServletResponse();
@@ -1088,13 +1086,13 @@ public class OAuth2AuthorizationEndpointFilterTests {
this.filter.doFilter(request, response, filterChain);
ArgumentCaptor<OAuth2AuthorizationConsent> consentCaptor = ArgumentCaptor.forClass(OAuth2AuthorizationConsent.class);
ArgumentCaptor<OAuth2AuthorizationConsent> authorizationConsentCaptor = ArgumentCaptor.forClass(OAuth2AuthorizationConsent.class);
verify(this.consentService).save(consentCaptor.capture());
OAuth2AuthorizationConsent consent = consentCaptor.getValue();
assertThat(consent.getPrincipalName()).isEqualTo(this.authentication.getName());
assertThat(consent.getRegisteredClientId()).isEqualTo(registeredClient.getClientId());
assertThat(consent.getScopes()).containsExactlyInAnyOrder("message.read", "message.write");
verify(this.authorizationConsentService).save(authorizationConsentCaptor.capture());
OAuth2AuthorizationConsent authorizationConsent = authorizationConsentCaptor.getValue();
assertThat(authorizationConsent.getRegisteredClientId()).isEqualTo(registeredClient.getClientId());
assertThat(authorizationConsent.getPrincipalName()).isEqualTo(this.authentication.getName());
assertThat(authorizationConsent.getScopes()).containsExactlyInAnyOrder("message.read", "message.write");
}
@Test
@@ -1116,18 +1114,17 @@ public class OAuth2AuthorizationEndpointFilterTests {
.build();
when(this.authorizationService.findByToken(eq("state"), eq(STATE_TOKEN_TYPE)))
.thenReturn(authorization);
when(this.registeredClientRepository.findByClientId((eq(registeredClient.getClientId()))))
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
.thenReturn(registeredClient);
OAuth2AuthorizationConsent previousConsent =
createConsent(
OAuth2AuthorizationConsent previousAuthorizationConsent =
createAuthorizationConsent(
registeredClient.getClientId(),
this.authentication.getName(),
Arrays.asList(previouslyApprovedScope, unrelatedPreviouslyApprovedScope)
);
when(this.consentService.findById(
eq(registeredClient.getClientId()),
eq(this.authentication.getName())))
.thenReturn(previousConsent);
when(this.authorizationConsentService.findById(
eq(registeredClient.getId()), eq(this.authentication.getName())))
.thenReturn(previousAuthorizationConsent);
MockHttpServletRequest request = createUserConsentRequest(registeredClient);
MockHttpServletResponse response = new MockHttpServletResponse();
@@ -1313,18 +1310,14 @@ public class OAuth2AuthorizationEndpointFilterTests {
request.addParameter(PkceParameterNames.CODE_CHALLENGE_METHOD, "S256");
}
private static OAuth2AuthorizationConsent createConsent(
String registeredClientId,
String prinicpalName,
Collection<String> scopes
) {
OAuth2AuthorizationConsent.Builder consentBuilder = OAuth2AuthorizationConsent
.withId(registeredClientId, prinicpalName);
private static OAuth2AuthorizationConsent createAuthorizationConsent(String registeredClientId,
String principalName, Collection<String> scopes) {
OAuth2AuthorizationConsent.Builder authorizationConsentBuilder =
OAuth2AuthorizationConsent.withId(registeredClientId, principalName);
for (String scope : scopes) {
consentBuilder.scope(scope);
authorizationConsentBuilder.scope(scope);
}
return consentBuilder.build();
return authorizationConsentBuilder.build();
}
private static MockHttpServletRequest createUserConsentRequest(RegisteredClient registeredClient) {
@@ -1357,4 +1350,5 @@ public class OAuth2AuthorizationEndpointFilterTests {
scope
);
}
}