|
@@ -1,5 +1,5 @@
|
|
/*
|
|
/*
|
|
- * Copyright 2020 the original author or authors.
|
|
|
|
|
|
+ * Copyright 2020-2021 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.
|
|
@@ -21,6 +21,8 @@ import java.util.Map;
|
|
import org.junit.Before;
|
|
import org.junit.Before;
|
|
import org.junit.Test;
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
+import org.springframework.security.crypto.password.NoOpPasswordEncoder;
|
|
|
|
+import org.springframework.security.crypto.password.PasswordEncoder;
|
|
import org.springframework.security.oauth2.core.AuthorizationGrantType;
|
|
import org.springframework.security.oauth2.core.AuthorizationGrantType;
|
|
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
|
|
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
|
|
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
|
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
|
@@ -37,8 +39,11 @@ import org.springframework.security.oauth2.server.authorization.client.TestRegis
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
import static org.mockito.ArgumentMatchers.eq;
|
|
import static org.mockito.ArgumentMatchers.eq;
|
|
import static org.mockito.Mockito.mock;
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
+import static org.mockito.Mockito.spy;
|
|
|
|
+import static org.mockito.Mockito.verify;
|
|
import static org.mockito.Mockito.when;
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -64,6 +69,7 @@ public class OAuth2ClientAuthenticationProviderTests {
|
|
private RegisteredClientRepository registeredClientRepository;
|
|
private RegisteredClientRepository registeredClientRepository;
|
|
private OAuth2AuthorizationService authorizationService;
|
|
private OAuth2AuthorizationService authorizationService;
|
|
private OAuth2ClientAuthenticationProvider authenticationProvider;
|
|
private OAuth2ClientAuthenticationProvider authenticationProvider;
|
|
|
|
+ private PasswordEncoder passwordEncoder;
|
|
|
|
|
|
@Before
|
|
@Before
|
|
public void setUp() {
|
|
public void setUp() {
|
|
@@ -71,6 +77,18 @@ public class OAuth2ClientAuthenticationProviderTests {
|
|
this.authorizationService = mock(OAuth2AuthorizationService.class);
|
|
this.authorizationService = mock(OAuth2AuthorizationService.class);
|
|
this.authenticationProvider = new OAuth2ClientAuthenticationProvider(
|
|
this.authenticationProvider = new OAuth2ClientAuthenticationProvider(
|
|
this.registeredClientRepository, this.authorizationService);
|
|
this.registeredClientRepository, this.authorizationService);
|
|
|
|
+ this.passwordEncoder = spy(new PasswordEncoder() {
|
|
|
|
+ @Override
|
|
|
|
+ public String encode(CharSequence rawPassword) {
|
|
|
|
+ return NoOpPasswordEncoder.getInstance().encode(rawPassword);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean matches(CharSequence rawPassword, String encodedPassword) {
|
|
|
|
+ return NoOpPasswordEncoder.getInstance().matches(rawPassword, encodedPassword);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ this.authenticationProvider.setPasswordEncoder(this.passwordEncoder);
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
@@ -88,8 +106,8 @@ public class OAuth2ClientAuthenticationProviderTests {
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
- public void constructorWhenPasswordEncoderNullThenThrowIllegalArgumentException() {
|
|
|
|
- assertThatThrownBy(() -> authenticationProvider.setPasswordEncoder(null))
|
|
|
|
|
|
+ public void setPasswordEncoderWhenNullThenThrowIllegalArgumentException() {
|
|
|
|
+ assertThatThrownBy(() -> this.authenticationProvider.setPasswordEncoder(null))
|
|
.isInstanceOf(IllegalArgumentException.class)
|
|
.isInstanceOf(IllegalArgumentException.class)
|
|
.hasMessage("passwordEncoder cannot be null");
|
|
.hasMessage("passwordEncoder cannot be null");
|
|
}
|
|
}
|
|
@@ -127,6 +145,7 @@ public class OAuth2ClientAuthenticationProviderTests {
|
|
.extracting(ex -> ((OAuth2AuthenticationException) ex).getError())
|
|
.extracting(ex -> ((OAuth2AuthenticationException) ex).getError())
|
|
.extracting("errorCode")
|
|
.extracting("errorCode")
|
|
.isEqualTo(OAuth2ErrorCodes.INVALID_CLIENT);
|
|
.isEqualTo(OAuth2ErrorCodes.INVALID_CLIENT);
|
|
|
|
+ verify(this.passwordEncoder).matches(any(), any());
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
@@ -151,9 +170,11 @@ public class OAuth2ClientAuthenticationProviderTests {
|
|
.thenReturn(registeredClient);
|
|
.thenReturn(registeredClient);
|
|
|
|
|
|
OAuth2ClientAuthenticationToken authentication = new OAuth2ClientAuthenticationToken(
|
|
OAuth2ClientAuthenticationToken authentication = new OAuth2ClientAuthenticationToken(
|
|
- registeredClient.getClientId(), TestRegisteredClients.CLIENT_SECRET, ClientAuthenticationMethod.BASIC, null);
|
|
|
|
|
|
+ registeredClient.getClientId(), registeredClient.getClientSecret(), ClientAuthenticationMethod.BASIC, null);
|
|
OAuth2ClientAuthenticationToken authenticationResult =
|
|
OAuth2ClientAuthenticationToken authenticationResult =
|
|
(OAuth2ClientAuthenticationToken) this.authenticationProvider.authenticate(authentication);
|
|
(OAuth2ClientAuthenticationToken) this.authenticationProvider.authenticate(authentication);
|
|
|
|
+
|
|
|
|
+ verify(this.passwordEncoder).matches(any(), any());
|
|
assertThat(authenticationResult.isAuthenticated()).isTrue();
|
|
assertThat(authenticationResult.isAuthenticated()).isTrue();
|
|
assertThat(authenticationResult.getPrincipal().toString()).isEqualTo(registeredClient.getClientId());
|
|
assertThat(authenticationResult.getPrincipal().toString()).isEqualTo(registeredClient.getClientId());
|
|
assertThat(authenticationResult.getCredentials()).isNull();
|
|
assertThat(authenticationResult.getCredentials()).isNull();
|