|
@@ -1,5 +1,5 @@
|
|
/*
|
|
/*
|
|
- * Copyright 2020-2023 the original author or authors.
|
|
|
|
|
|
+ * Copyright 2020-2024 the original author or authors.
|
|
*
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* you may not use this file except in compliance with the License.
|
|
@@ -73,7 +73,6 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
private OAuth2AuthorizationConsentService authorizationConsentService;
|
|
private OAuth2AuthorizationConsentService authorizationConsentService;
|
|
private OAuth2AuthorizationCodeRequestAuthenticationProvider authenticationProvider;
|
|
private OAuth2AuthorizationCodeRequestAuthenticationProvider authenticationProvider;
|
|
private TestingAuthenticationToken principal;
|
|
private TestingAuthenticationToken principal;
|
|
- private Predicate<OAuth2AuthorizationCodeRequestAuthenticationContext> requiresAuthorizationConsent;
|
|
|
|
|
|
|
|
@BeforeEach
|
|
@BeforeEach
|
|
public void setUp() {
|
|
public void setUp() {
|
|
@@ -132,10 +131,10 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
- public void setRequiresAuthorizationConsentWhenNullThenThrowIllegalArgumentException() {
|
|
|
|
- assertThatThrownBy(() -> this.authenticationProvider.setRequiresAuthorizationConsent(null))
|
|
|
|
|
|
+ public void setAuthorizationConsentRequiredWhenNullThenThrowIllegalArgumentException() {
|
|
|
|
+ assertThatThrownBy(() -> this.authenticationProvider.setAuthorizationConsentRequired(null))
|
|
.isInstanceOf(IllegalArgumentException.class)
|
|
.isInstanceOf(IllegalArgumentException.class)
|
|
- .hasMessage("requiresAuthorizationConsent cannot be null");
|
|
|
|
|
|
+ .hasMessage("authorizationConsentRequired cannot be null");
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
@@ -453,64 +452,12 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
- public void authenticateWhenRequireAuthorizationConsentAndRequiresAuthorizationConsentPredicateTrueThenReturnAuthorizationConsent() {
|
|
|
|
- this.authenticationProvider.setRequiresAuthorizationConsent((authenticationContext) -> true);
|
|
|
|
-
|
|
|
|
- RegisteredClient registeredClient = TestRegisteredClients.registeredClient()
|
|
|
|
- .clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
|
|
|
|
- .build();
|
|
|
|
- when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
|
|
- .thenReturn(registeredClient);
|
|
|
|
-
|
|
|
|
- String redirectUri = registeredClient.getRedirectUris().toArray(new String[0])[0];
|
|
|
|
- OAuth2AuthorizationCodeRequestAuthenticationToken authentication =
|
|
|
|
- new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
|
|
- AUTHORIZATION_URI, registeredClient.getClientId(), principal,
|
|
|
|
- redirectUri, STATE, registeredClient.getScopes(), null);
|
|
|
|
-
|
|
|
|
- OAuth2AuthorizationConsentAuthenticationToken authenticationResult =
|
|
|
|
- (OAuth2AuthorizationConsentAuthenticationToken) this.authenticationProvider.authenticate(authentication);
|
|
|
|
-
|
|
|
|
- ArgumentCaptor<OAuth2Authorization> authorizationCaptor = ArgumentCaptor.forClass(OAuth2Authorization.class);
|
|
|
|
- verify(this.authorizationService).save(authorizationCaptor.capture());
|
|
|
|
- OAuth2Authorization authorization = authorizationCaptor.getValue();
|
|
|
|
-
|
|
|
|
- OAuth2AuthorizationRequest authorizationRequest = authorization.getAttribute(OAuth2AuthorizationRequest.class.getName());
|
|
|
|
- assertThat(authorizationRequest.getGrantType()).isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE);
|
|
|
|
- assertThat(authorizationRequest.getResponseType()).isEqualTo(OAuth2AuthorizationResponseType.CODE);
|
|
|
|
- assertThat(authorizationRequest.getAuthorizationUri()).isEqualTo(authentication.getAuthorizationUri());
|
|
|
|
- assertThat(authorizationRequest.getClientId()).isEqualTo(registeredClient.getClientId());
|
|
|
|
- assertThat(authorizationRequest.getRedirectUri()).isEqualTo(authentication.getRedirectUri());
|
|
|
|
- assertThat(authorizationRequest.getScopes()).isEqualTo(authentication.getScopes());
|
|
|
|
- assertThat(authorizationRequest.getState()).isEqualTo(authentication.getState());
|
|
|
|
- assertThat(authorizationRequest.getAdditionalParameters()).isEqualTo(authentication.getAdditionalParameters());
|
|
|
|
-
|
|
|
|
- assertThat(authorization.getRegisteredClientId()).isEqualTo(registeredClient.getId());
|
|
|
|
- assertThat(authorization.getPrincipalName()).isEqualTo(this.principal.getName());
|
|
|
|
- assertThat(authorization.getAuthorizationGrantType()).isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE);
|
|
|
|
- assertThat(authorization.<Authentication>getAttribute(Principal.class.getName())).isEqualTo(this.principal);
|
|
|
|
- String state = authorization.getAttribute(OAuth2ParameterNames.STATE);
|
|
|
|
- assertThat(state).isNotNull();
|
|
|
|
- assertThat(state).isNotEqualTo(authentication.getState());
|
|
|
|
-
|
|
|
|
- assertThat(authenticationResult.getClientId()).isEqualTo(registeredClient.getClientId());
|
|
|
|
- assertThat(authenticationResult.getPrincipal()).isEqualTo(this.principal);
|
|
|
|
- assertThat(authenticationResult.getAuthorizationUri()).isEqualTo(authorizationRequest.getAuthorizationUri());
|
|
|
|
- assertThat(authenticationResult.getScopes()).isEmpty();
|
|
|
|
- assertThat(authenticationResult.getState()).isEqualTo(state);
|
|
|
|
- assertThat(authenticationResult.isAuthenticated()).isTrue();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Test
|
|
|
|
- public void authenticateWhenRequireAuthorizationConsentAndRequiresAuthorizationConsentPredicateFalseThenAuthorizationConsentNotRequired() {
|
|
|
|
- this.authenticationProvider.setRequiresAuthorizationConsent((authenticationContext) -> false);
|
|
|
|
-
|
|
|
|
|
|
+ public void authenticateWhenRequireAuthorizationConsentAndOnlyOpenidScopeRequestedThenAuthorizationConsentNotRequired() {
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient()
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient()
|
|
.clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
|
|
.clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
|
|
.scopes(scopes -> {
|
|
.scopes(scopes -> {
|
|
scopes.clear();
|
|
scopes.clear();
|
|
scopes.add(OidcScopes.OPENID);
|
|
scopes.add(OidcScopes.OPENID);
|
|
- scopes.add(OidcScopes.EMAIL);
|
|
|
|
})
|
|
})
|
|
.build();
|
|
.build();
|
|
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
@@ -529,18 +476,21 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
- public void authenticateWhenRequireAuthorizationConsentAndOnlyOpenidScopeRequestedThenAuthorizationConsentNotRequired() {
|
|
|
|
|
|
+ public void authenticateWhenRequireAuthorizationConsentAndAllPreviouslyApprovedThenAuthorizationConsentNotRequired() {
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient()
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient()
|
|
.clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
|
|
.clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
|
|
- .scopes(scopes -> {
|
|
|
|
- scopes.clear();
|
|
|
|
- scopes.add(OidcScopes.OPENID);
|
|
|
|
- })
|
|
|
|
.build();
|
|
.build();
|
|
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
.thenReturn(registeredClient);
|
|
.thenReturn(registeredClient);
|
|
|
|
|
|
- String redirectUri = registeredClient.getRedirectUris().toArray(new String[0])[1];
|
|
|
|
|
|
+ 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);
|
|
|
|
+
|
|
|
|
+ String redirectUri = registeredClient.getRedirectUris().toArray(new String[0])[2];
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication =
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication =
|
|
new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
AUTHORIZATION_URI, registeredClient.getClientId(), principal,
|
|
AUTHORIZATION_URI, registeredClient.getClientId(), principal,
|
|
@@ -553,21 +503,16 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
- public void authenticateWhenRequireAuthorizationConsentAndAllPreviouslyApprovedThenAuthorizationConsentNotRequired() {
|
|
|
|
- RegisteredClient registeredClient = TestRegisteredClients.registeredClient()
|
|
|
|
- .clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
|
|
|
|
- .build();
|
|
|
|
|
|
+ public void authenticateWhenCustomAuthorizationConsentRequiredThenUsed() {
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
+ Predicate<OAuth2AuthorizationCodeRequestAuthenticationContext> authorizationConsentRequired = mock(Predicate.class);
|
|
|
|
+ this.authenticationProvider.setAuthorizationConsentRequired(authorizationConsentRequired);
|
|
|
|
+
|
|
|
|
+ RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
|
|
.thenReturn(registeredClient);
|
|
.thenReturn(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);
|
|
|
|
-
|
|
|
|
- String redirectUri = registeredClient.getRedirectUris().toArray(new String[0])[2];
|
|
|
|
|
|
+ String redirectUri = registeredClient.getRedirectUris().toArray(new String[0])[1];
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication =
|
|
OAuth2AuthorizationCodeRequestAuthenticationToken authentication =
|
|
new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
new OAuth2AuthorizationCodeRequestAuthenticationToken(
|
|
AUTHORIZATION_URI, registeredClient.getClientId(), principal,
|
|
AUTHORIZATION_URI, registeredClient.getClientId(), principal,
|
|
@@ -577,6 +522,8 @@ public class OAuth2AuthorizationCodeRequestAuthenticationProviderTests {
|
|
(OAuth2AuthorizationCodeRequestAuthenticationToken) this.authenticationProvider.authenticate(authentication);
|
|
(OAuth2AuthorizationCodeRequestAuthenticationToken) this.authenticationProvider.authenticate(authentication);
|
|
|
|
|
|
assertAuthorizationCodeRequestWithAuthorizationCodeResult(registeredClient, authentication, authenticationResult);
|
|
assertAuthorizationCodeRequestWithAuthorizationCodeResult(registeredClient, authentication, authenticationResult);
|
|
|
|
+
|
|
|
|
+ verify(authorizationConsentRequired).test(any());
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|