|
@@ -55,9 +55,9 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
import static org.mockito.ArgumentMatchers.eq;
|
|
|
+import static org.mockito.BDDMockito.given;
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
-import static org.mockito.Mockito.when;
|
|
|
|
|
|
/**
|
|
|
* Tests for {@link OAuth2AuthorizationCodeRequestAuthenticationProvider}.
|
|
@@ -155,11 +155,11 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
String redirectUri = registeredClient.getRedirectUris().toArray(new String[0])[1];
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication = new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
|
- AUTHORIZATION_URI, registeredClient.getClientId(), principal, redirectUri, STATE,
|
|
|
+ AUTHORIZATION_URI, registeredClient.getClientId(), this.principal, redirectUri, STATE,
|
|
|
registeredClient.getScopes(), null);
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
|
|
.isInstanceOf(OAuth2AuthorizationCodeRequestAuthenticationException.class)
|
|
|
- .satisfies(ex -> assertAuthenticationException((OAuth2AuthorizationCodeRequestAuthenticationException) ex,
|
|
|
+ .satisfies((ex) -> assertAuthenticationException((OAuth2AuthorizationCodeRequestAuthenticationException) ex,
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST, OAuth2ParameterNames.CLIENT_ID, null));
|
|
|
}
|
|
|
|
|
@@ -167,14 +167,14 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
|
@Test
|
|
|
public void authenticateWhenInvalidRedirectUriHostThenThrowOAuth2AuthorizationCodeRequestAuthenticationException() {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
- when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
- .thenReturn(registeredClient);
|
|
|
+ given(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
+ .willReturn(registeredClient);
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication = new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
|
- AUTHORIZATION_URI, registeredClient.getClientId(), principal, "https:///invalid", STATE,
|
|
|
+ AUTHORIZATION_URI, registeredClient.getClientId(), this.principal, "https:///invalid", STATE,
|
|
|
registeredClient.getScopes(), null);
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
|
|
.isInstanceOf(OAuth2AuthorizationCodeRequestAuthenticationException.class)
|
|
|
- .satisfies(ex -> assertAuthenticationException((OAuth2AuthorizationCodeRequestAuthenticationException) ex,
|
|
|
+ .satisfies((ex) -> assertAuthenticationException((OAuth2AuthorizationCodeRequestAuthenticationException) ex,
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST, OAuth2ParameterNames.REDIRECT_URI, null));
|
|
|
}
|
|
|
|
|
@@ -182,28 +182,28 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
|
@Test
|
|
|
public void authenticateWhenInvalidRedirectUriFragmentThenThrowOAuth2AuthorizationCodeRequestAuthenticationException() {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
- when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
- .thenReturn(registeredClient);
|
|
|
+ given(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
+ .willReturn(registeredClient);
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication = new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
|
- AUTHORIZATION_URI, registeredClient.getClientId(), principal, "https://example.com#fragment", STATE,
|
|
|
- registeredClient.getScopes(), null);
|
|
|
+ AUTHORIZATION_URI, registeredClient.getClientId(), this.principal, "https://example.com#fragment",
|
|
|
+ STATE, registeredClient.getScopes(), null);
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
|
|
.isInstanceOf(OAuth2AuthorizationCodeRequestAuthenticationException.class)
|
|
|
- .satisfies(ex -> assertAuthenticationException((OAuth2AuthorizationCodeRequestAuthenticationException) ex,
|
|
|
+ .satisfies((ex) -> assertAuthenticationException((OAuth2AuthorizationCodeRequestAuthenticationException) ex,
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST, OAuth2ParameterNames.REDIRECT_URI, null));
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void authenticateWhenUnregisteredRedirectUriThenThrowOAuth2AuthorizationCodeRequestAuthenticationException() {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
- when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
- .thenReturn(registeredClient);
|
|
|
+ given(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
+ .willReturn(registeredClient);
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication = new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
|
- AUTHORIZATION_URI, registeredClient.getClientId(), principal, "https://invalid-example.com", STATE,
|
|
|
+ AUTHORIZATION_URI, registeredClient.getClientId(), this.principal, "https://invalid-example.com", STATE,
|
|
|
registeredClient.getScopes(), null);
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
|
|
.isInstanceOf(OAuth2AuthorizationCodeRequestAuthenticationException.class)
|
|
|
- .satisfies(ex -> assertAuthenticationException((OAuth2AuthorizationCodeRequestAuthenticationException) ex,
|
|
|
+ .satisfies((ex) -> assertAuthenticationException((OAuth2AuthorizationCodeRequestAuthenticationException) ex,
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST, OAuth2ParameterNames.REDIRECT_URI, null));
|
|
|
}
|
|
|
|
|
@@ -213,10 +213,10 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient()
|
|
|
.redirectUri("https://127.0.0.1:8080")
|
|
|
.build();
|
|
|
- when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
- .thenReturn(registeredClient);
|
|
|
+ given(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
+ .willReturn(registeredClient);
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication = new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
|
- AUTHORIZATION_URI, registeredClient.getClientId(), principal, "https://127.0.0.1:5000", STATE,
|
|
|
+ AUTHORIZATION_URI, registeredClient.getClientId(), this.principal, "https://127.0.0.1:5000", STATE,
|
|
|
registeredClient.getScopes(), null);
|
|
|
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authenticationResult = (OAuth2AuthorizationCodeRequestAuthenticationToken) this.authenticationProvider
|
|
@@ -232,10 +232,10 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient()
|
|
|
.redirectUri("https://[::1]:8080")
|
|
|
.build();
|
|
|
- when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
- .thenReturn(registeredClient);
|
|
|
+ given(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
+ .willReturn(registeredClient);
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication = new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
|
- AUTHORIZATION_URI, registeredClient.getClientId(), principal, "https://[::1]:5000", STATE,
|
|
|
+ AUTHORIZATION_URI, registeredClient.getClientId(), this.principal, "https://[::1]:5000", STATE,
|
|
|
registeredClient.getScopes(), null);
|
|
|
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authenticationResult = (OAuth2AuthorizationCodeRequestAuthenticationToken) this.authenticationProvider
|
|
@@ -250,14 +250,14 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient()
|
|
|
.redirectUri("https://example2.com")
|
|
|
.build();
|
|
|
- when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
- .thenReturn(registeredClient);
|
|
|
+ given(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
+ .willReturn(registeredClient);
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication = new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
|
- AUTHORIZATION_URI, registeredClient.getClientId(), principal, null, STATE, registeredClient.getScopes(),
|
|
|
- null);
|
|
|
+ AUTHORIZATION_URI, registeredClient.getClientId(), this.principal, null, STATE,
|
|
|
+ registeredClient.getScopes(), null);
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
|
|
.isInstanceOf(OAuth2AuthorizationCodeRequestAuthenticationException.class)
|
|
|
- .satisfies(ex -> assertAuthenticationException((OAuth2AuthorizationCodeRequestAuthenticationException) ex,
|
|
|
+ .satisfies((ex) -> assertAuthenticationException((OAuth2AuthorizationCodeRequestAuthenticationException) ex,
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST, OAuth2ParameterNames.REDIRECT_URI, null));
|
|
|
}
|
|
|
|
|
@@ -265,14 +265,14 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
|
public void authenticateWhenAuthenticationRequestMissingRedirectUriThenThrowOAuth2AuthorizationCodeRequestAuthenticationException() {
|
|
|
// redirect_uri is REQUIRED for OpenID Connect requests
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().scope(OidcScopes.OPENID).build();
|
|
|
- when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
- .thenReturn(registeredClient);
|
|
|
+ given(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
+ .willReturn(registeredClient);
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication = new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
|
- AUTHORIZATION_URI, registeredClient.getClientId(), principal, null, STATE, registeredClient.getScopes(),
|
|
|
- null);
|
|
|
+ AUTHORIZATION_URI, registeredClient.getClientId(), this.principal, null, STATE,
|
|
|
+ registeredClient.getScopes(), null);
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
|
|
.isInstanceOf(OAuth2AuthorizationCodeRequestAuthenticationException.class)
|
|
|
- .satisfies(ex -> assertAuthenticationException((OAuth2AuthorizationCodeRequestAuthenticationException) ex,
|
|
|
+ .satisfies((ex) -> assertAuthenticationException((OAuth2AuthorizationCodeRequestAuthenticationException) ex,
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST, OAuth2ParameterNames.REDIRECT_URI, null));
|
|
|
}
|
|
|
|
|
@@ -282,15 +282,15 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
|
.authorizationGrantTypes(Set::clear)
|
|
|
.authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
|
|
|
.build();
|
|
|
- when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
- .thenReturn(registeredClient);
|
|
|
+ given(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
+ .willReturn(registeredClient);
|
|
|
String redirectUri = registeredClient.getRedirectUris().toArray(new String[0])[1];
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication = new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
|
- AUTHORIZATION_URI, registeredClient.getClientId(), principal, redirectUri, STATE,
|
|
|
+ AUTHORIZATION_URI, registeredClient.getClientId(), this.principal, redirectUri, STATE,
|
|
|
registeredClient.getScopes(), null);
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
|
|
.isInstanceOf(OAuth2AuthorizationCodeRequestAuthenticationException.class)
|
|
|
- .satisfies(ex -> assertAuthenticationException((OAuth2AuthorizationCodeRequestAuthenticationException) ex,
|
|
|
+ .satisfies((ex) -> assertAuthenticationException((OAuth2AuthorizationCodeRequestAuthenticationException) ex,
|
|
|
OAuth2ErrorCodes.UNAUTHORIZED_CLIENT, OAuth2ParameterNames.CLIENT_ID,
|
|
|
authentication.getRedirectUri()));
|
|
|
}
|
|
@@ -298,15 +298,15 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
|
@Test
|
|
|
public void authenticateWhenInvalidScopeThenThrowOAuth2AuthorizationCodeRequestAuthenticationException() {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
- when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
- .thenReturn(registeredClient);
|
|
|
+ given(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
+ .willReturn(registeredClient);
|
|
|
String redirectUri = registeredClient.getRedirectUris().toArray(new String[0])[2];
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication = new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
|
- AUTHORIZATION_URI, registeredClient.getClientId(), principal, redirectUri, STATE,
|
|
|
+ AUTHORIZATION_URI, registeredClient.getClientId(), this.principal, redirectUri, STATE,
|
|
|
Collections.singleton("invalid-scope"), null);
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
|
|
.isInstanceOf(OAuth2AuthorizationCodeRequestAuthenticationException.class)
|
|
|
- .satisfies(ex -> assertAuthenticationException((OAuth2AuthorizationCodeRequestAuthenticationException) ex,
|
|
|
+ .satisfies((ex) -> assertAuthenticationException((OAuth2AuthorizationCodeRequestAuthenticationException) ex,
|
|
|
OAuth2ErrorCodes.INVALID_SCOPE, OAuth2ParameterNames.SCOPE, authentication.getRedirectUri()));
|
|
|
}
|
|
|
|
|
@@ -315,15 +315,15 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient()
|
|
|
.clientSettings(ClientSettings.builder().requireProofKey(true).build())
|
|
|
.build();
|
|
|
- when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
- .thenReturn(registeredClient);
|
|
|
+ given(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
+ .willReturn(registeredClient);
|
|
|
String redirectUri = registeredClient.getRedirectUris().toArray(new String[0])[2];
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication = new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
|
- AUTHORIZATION_URI, registeredClient.getClientId(), principal, redirectUri, STATE,
|
|
|
+ AUTHORIZATION_URI, registeredClient.getClientId(), this.principal, redirectUri, STATE,
|
|
|
registeredClient.getScopes(), null);
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
|
|
.isInstanceOf(OAuth2AuthorizationCodeRequestAuthenticationException.class)
|
|
|
- .satisfies(ex -> assertAuthenticationException((OAuth2AuthorizationCodeRequestAuthenticationException) ex,
|
|
|
+ .satisfies((ex) -> assertAuthenticationException((OAuth2AuthorizationCodeRequestAuthenticationException) ex,
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST, PkceParameterNames.CODE_CHALLENGE,
|
|
|
authentication.getRedirectUri()));
|
|
|
}
|
|
@@ -331,18 +331,18 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
|
@Test
|
|
|
public void authenticateWhenPkceUnsupportedCodeChallengeMethodThenThrowOAuth2AuthorizationCodeRequestAuthenticationException() {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
- when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
- .thenReturn(registeredClient);
|
|
|
+ given(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
+ .willReturn(registeredClient);
|
|
|
String redirectUri = registeredClient.getRedirectUris().toArray(new String[0])[0];
|
|
|
Map<String, Object> additionalParameters = new HashMap<>();
|
|
|
additionalParameters.put(PkceParameterNames.CODE_CHALLENGE, "code-challenge");
|
|
|
additionalParameters.put(PkceParameterNames.CODE_CHALLENGE_METHOD, "unsupported");
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication = new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
|
- AUTHORIZATION_URI, registeredClient.getClientId(), principal, redirectUri, STATE,
|
|
|
+ AUTHORIZATION_URI, registeredClient.getClientId(), this.principal, redirectUri, STATE,
|
|
|
registeredClient.getScopes(), additionalParameters);
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
|
|
.isInstanceOf(OAuth2AuthorizationCodeRequestAuthenticationException.class)
|
|
|
- .satisfies(ex -> assertAuthenticationException((OAuth2AuthorizationCodeRequestAuthenticationException) ex,
|
|
|
+ .satisfies((ex) -> assertAuthenticationException((OAuth2AuthorizationCodeRequestAuthenticationException) ex,
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST, PkceParameterNames.CODE_CHALLENGE_METHOD,
|
|
|
authentication.getRedirectUri()));
|
|
|
}
|
|
@@ -351,17 +351,17 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
|
@Test
|
|
|
public void authenticateWhenPkceMissingCodeChallengeMethodThenThrowOAuth2AuthorizationCodeRequestAuthenticationException() {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
- when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
- .thenReturn(registeredClient);
|
|
|
+ given(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
+ .willReturn(registeredClient);
|
|
|
String redirectUri = registeredClient.getRedirectUris().toArray(new String[0])[2];
|
|
|
Map<String, Object> additionalParameters = new HashMap<>();
|
|
|
additionalParameters.put(PkceParameterNames.CODE_CHALLENGE, "code-challenge");
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication = new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
|
- AUTHORIZATION_URI, registeredClient.getClientId(), principal, redirectUri, STATE,
|
|
|
+ AUTHORIZATION_URI, registeredClient.getClientId(), this.principal, redirectUri, STATE,
|
|
|
registeredClient.getScopes(), additionalParameters);
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
|
|
.isInstanceOf(OAuth2AuthorizationCodeRequestAuthenticationException.class)
|
|
|
- .satisfies(ex -> assertAuthenticationException((OAuth2AuthorizationCodeRequestAuthenticationException) ex,
|
|
|
+ .satisfies((ex) -> assertAuthenticationException((OAuth2AuthorizationCodeRequestAuthenticationException) ex,
|
|
|
OAuth2ErrorCodes.INVALID_REQUEST, PkceParameterNames.CODE_CHALLENGE_METHOD,
|
|
|
authentication.getRedirectUri()));
|
|
|
}
|
|
@@ -369,13 +369,13 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
|
@Test
|
|
|
public void authenticateWhenPrincipalNotAuthenticatedThenReturnAuthorizationCodeRequest() {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
- when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
- .thenReturn(registeredClient);
|
|
|
+ given(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
+ .willReturn(registeredClient);
|
|
|
this.principal.setAuthenticated(false);
|
|
|
|
|
|
String redirectUri = registeredClient.getRedirectUris().toArray(new String[0])[1];
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication = new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
|
- AUTHORIZATION_URI, registeredClient.getClientId(), principal, redirectUri, STATE,
|
|
|
+ AUTHORIZATION_URI, registeredClient.getClientId(), this.principal, redirectUri, STATE,
|
|
|
registeredClient.getScopes(), null);
|
|
|
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authenticationResult = (OAuth2AuthorizationCodeRequestAuthenticationToken) this.authenticationProvider
|
|
@@ -390,12 +390,12 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient()
|
|
|
.clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
|
|
|
.build();
|
|
|
- when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
- .thenReturn(registeredClient);
|
|
|
+ given(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
+ .willReturn(registeredClient);
|
|
|
|
|
|
String redirectUri = registeredClient.getRedirectUris().toArray(new String[0])[0];
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication = new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
|
- AUTHORIZATION_URI, registeredClient.getClientId(), principal, redirectUri, STATE,
|
|
|
+ AUTHORIZATION_URI, registeredClient.getClientId(), this.principal, redirectUri, STATE,
|
|
|
registeredClient.getScopes(), null);
|
|
|
|
|
|
OAuth2AuthorizationConsentAuthenticationToken authenticationResult = (OAuth2AuthorizationConsentAuthenticationToken) this.authenticationProvider
|
|
@@ -436,17 +436,17 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
|
public void authenticateWhenRequireAuthorizationConsentAndOnlyOpenidScopeRequestedThenAuthorizationConsentNotRequired() {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient()
|
|
|
.clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
|
|
|
- .scopes(scopes -> {
|
|
|
+ .scopes((scopes) -> {
|
|
|
scopes.clear();
|
|
|
scopes.add(OidcScopes.OPENID);
|
|
|
})
|
|
|
.build();
|
|
|
- when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
- .thenReturn(registeredClient);
|
|
|
+ given(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
+ .willReturn(registeredClient);
|
|
|
|
|
|
String redirectUri = registeredClient.getRedirectUris().toArray(new String[0])[1];
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication = new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
|
- AUTHORIZATION_URI, registeredClient.getClientId(), principal, redirectUri, STATE,
|
|
|
+ AUTHORIZATION_URI, registeredClient.getClientId(), this.principal, redirectUri, STATE,
|
|
|
registeredClient.getScopes(), null);
|
|
|
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authenticationResult = (OAuth2AuthorizationCodeRequestAuthenticationToken) this.authenticationProvider
|
|
@@ -461,19 +461,19 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient()
|
|
|
.clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
|
|
|
.build();
|
|
|
- when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
- .thenReturn(registeredClient);
|
|
|
+ given(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
+ .willReturn(registeredClient);
|
|
|
|
|
|
OAuth2AuthorizationConsent.Builder builder = OAuth2AuthorizationConsent.withId(registeredClient.getId(),
|
|
|
this.principal.getName());
|
|
|
registeredClient.getScopes().forEach(builder::scope);
|
|
|
OAuth2AuthorizationConsent previousAuthorizationConsent = builder.build();
|
|
|
- when(this.authorizationConsentService.findById(eq(registeredClient.getId()), eq(this.principal.getName())))
|
|
|
- .thenReturn(previousAuthorizationConsent);
|
|
|
+ given(this.authorizationConsentService.findById(eq(registeredClient.getId()), eq(this.principal.getName())))
|
|
|
+ .willReturn(previousAuthorizationConsent);
|
|
|
|
|
|
String redirectUri = registeredClient.getRedirectUris().toArray(new String[0])[2];
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication = new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
|
- AUTHORIZATION_URI, registeredClient.getClientId(), principal, redirectUri, STATE,
|
|
|
+ AUTHORIZATION_URI, registeredClient.getClientId(), this.principal, redirectUri, STATE,
|
|
|
registeredClient.getScopes(), null);
|
|
|
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authenticationResult = (OAuth2AuthorizationCodeRequestAuthenticationToken) this.authenticationProvider
|
|
@@ -511,15 +511,15 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
|
@Test
|
|
|
public void authenticateWhenAuthorizationCodeRequestValidThenReturnAuthorizationCode() {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
- when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
- .thenReturn(registeredClient);
|
|
|
+ given(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
+ .willReturn(registeredClient);
|
|
|
|
|
|
String redirectUri = registeredClient.getRedirectUris().toArray(new String[0])[0];
|
|
|
Map<String, Object> additionalParameters = new HashMap<>();
|
|
|
additionalParameters.put(PkceParameterNames.CODE_CHALLENGE, "code-challenge");
|
|
|
additionalParameters.put(PkceParameterNames.CODE_CHALLENGE_METHOD, "S256");
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication = new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
|
- AUTHORIZATION_URI, registeredClient.getClientId(), principal, redirectUri, STATE,
|
|
|
+ AUTHORIZATION_URI, registeredClient.getClientId(), this.principal, redirectUri, STATE,
|
|
|
registeredClient.getScopes(), additionalParameters);
|
|
|
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authenticationResult = (OAuth2AuthorizationCodeRequestAuthenticationToken) this.authenticationProvider
|
|
@@ -532,8 +532,8 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
|
@Test
|
|
|
public void authenticateWhenAuthorizationCodeNotGeneratedThenThrowOAuth2AuthorizationCodeRequestAuthenticationException() {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
- when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
- .thenReturn(registeredClient);
|
|
|
+ given(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
+ .willReturn(registeredClient);
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
OAuth2TokenGenerator<OAuth2AuthorizationCode> authorizationCodeGenerator = mock(OAuth2TokenGenerator.class);
|
|
@@ -541,13 +541,13 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
|
|
|
|
String redirectUri = registeredClient.getRedirectUris().toArray(new String[0])[1];
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication = new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
|
- AUTHORIZATION_URI, registeredClient.getClientId(), principal, redirectUri, STATE,
|
|
|
+ AUTHORIZATION_URI, registeredClient.getClientId(), this.principal, redirectUri, STATE,
|
|
|
registeredClient.getScopes(), null);
|
|
|
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
|
|
.isInstanceOf(OAuth2AuthorizationCodeRequestAuthenticationException.class)
|
|
|
- .extracting(ex -> ((OAuth2AuthorizationCodeRequestAuthenticationException) ex).getError())
|
|
|
- .satisfies(error -> {
|
|
|
+ .extracting((ex) -> ((OAuth2AuthorizationCodeRequestAuthenticationException) ex).getError())
|
|
|
+ .satisfies((error) -> {
|
|
|
assertThat(error.getErrorCode()).isEqualTo(OAuth2ErrorCodes.SERVER_ERROR);
|
|
|
assertThat(error.getDescription())
|
|
|
.contains("The token generator failed to generate the authorization code.");
|
|
@@ -557,8 +557,8 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
|
@Test
|
|
|
public void authenticateWhenCustomAuthenticationValidatorThenUsed() {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
- when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
- .thenReturn(registeredClient);
|
|
|
+ given(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
+ .willReturn(registeredClient);
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext> authenticationValidator = mock(Consumer.class);
|
|
@@ -566,7 +566,7 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
|
|
|
|
String redirectUri = registeredClient.getRedirectUris().toArray(new String[0])[2];
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication = new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
|
- AUTHORIZATION_URI, registeredClient.getClientId(), principal, redirectUri, STATE,
|
|
|
+ AUTHORIZATION_URI, registeredClient.getClientId(), this.principal, redirectUri, STATE,
|
|
|
registeredClient.getScopes(), null);
|
|
|
|
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authenticationResult = (OAuth2AuthorizationCodeRequestAuthenticationToken) this.authenticationProvider
|