Add UsernamePasswordAuthenticationToken factory methods
- unauthenticated factory method - authenticated factory method - test for unauthenticated factory method - test for authenticated factory method - make existing constructor protected - use newly factory methods in rest of the project - update copyright dates Closes gh-10790
This commit is contained in:
committed by
Josh Cummings
parent
28c7a4be11
commit
abd33389be
@@ -67,16 +67,17 @@ public class LdapAuthenticationProviderTests {
|
||||
public void testEmptyOrNullUserNameThrowsException() {
|
||||
LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(new MockAuthenticator(),
|
||||
new MockAuthoritiesPopulator());
|
||||
assertThatExceptionOfType(BadCredentialsException.class)
|
||||
.isThrownBy(() -> ldapProvider.authenticate(new UsernamePasswordAuthenticationToken(null, "password")));
|
||||
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(
|
||||
() -> ldapProvider.authenticate(new UsernamePasswordAuthenticationToken("", "bobspassword")));
|
||||
() -> ldapProvider.authenticate(UsernamePasswordAuthenticationToken.unauthenticated(null, "password")));
|
||||
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> ldapProvider
|
||||
.authenticate(UsernamePasswordAuthenticationToken.unauthenticated("", "bobspassword")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usernameNotFoundExceptionIsHiddenByDefault() {
|
||||
final LdapAuthenticator authenticator = mock(LdapAuthenticator.class);
|
||||
final UsernamePasswordAuthenticationToken joe = new UsernamePasswordAuthenticationToken("joe", "password");
|
||||
final UsernamePasswordAuthenticationToken joe = UsernamePasswordAuthenticationToken.unauthenticated("joe",
|
||||
"password");
|
||||
given(authenticator.authenticate(joe)).willThrow(new UsernameNotFoundException("nobody"));
|
||||
LdapAuthenticationProvider provider = new LdapAuthenticationProvider(authenticator);
|
||||
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> provider.authenticate(joe));
|
||||
@@ -85,7 +86,8 @@ public class LdapAuthenticationProviderTests {
|
||||
@Test
|
||||
public void usernameNotFoundExceptionIsNotHiddenIfConfigured() {
|
||||
final LdapAuthenticator authenticator = mock(LdapAuthenticator.class);
|
||||
final UsernamePasswordAuthenticationToken joe = new UsernamePasswordAuthenticationToken("joe", "password");
|
||||
final UsernamePasswordAuthenticationToken joe = UsernamePasswordAuthenticationToken.unauthenticated("joe",
|
||||
"password");
|
||||
given(authenticator.authenticate(joe)).willThrow(new UsernameNotFoundException("nobody"));
|
||||
LdapAuthenticationProvider provider = new LdapAuthenticationProvider(authenticator);
|
||||
provider.setHideUserNotFoundExceptions(false);
|
||||
@@ -100,7 +102,7 @@ public class LdapAuthenticationProviderTests {
|
||||
userMapper.setRoleAttributes(new String[] { "ou" });
|
||||
ldapProvider.setUserDetailsContextMapper(userMapper);
|
||||
assertThat(ldapProvider.getAuthoritiesPopulator()).isNotNull();
|
||||
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken("ben",
|
||||
UsernamePasswordAuthenticationToken authRequest = UsernamePasswordAuthenticationToken.unauthenticated("ben",
|
||||
"benspassword");
|
||||
Object authDetails = new Object();
|
||||
authRequest.setDetails(authDetails);
|
||||
@@ -121,7 +123,7 @@ public class LdapAuthenticationProviderTests {
|
||||
LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(new MockAuthenticator(),
|
||||
new MockAuthoritiesPopulator());
|
||||
ldapProvider.setUseAuthenticationRequestCredentials(false);
|
||||
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken("ben",
|
||||
UsernamePasswordAuthenticationToken authRequest = UsernamePasswordAuthenticationToken.unauthenticated("ben",
|
||||
"benspassword");
|
||||
Authentication authResult = ldapProvider.authenticate(authRequest);
|
||||
assertThat(authResult.getCredentials()).isEqualTo("{SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ=");
|
||||
@@ -133,7 +135,7 @@ public class LdapAuthenticationProviderTests {
|
||||
LdapUserDetailsMapper userMapper = new LdapUserDetailsMapper();
|
||||
userMapper.setRoleAttributes(new String[] { "ou" });
|
||||
ldapProvider.setUserDetailsContextMapper(userMapper);
|
||||
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken("ben",
|
||||
UsernamePasswordAuthenticationToken authRequest = UsernamePasswordAuthenticationToken.unauthenticated("ben",
|
||||
"benspassword");
|
||||
UserDetails user = (UserDetails) ldapProvider.authenticate(authRequest).getPrincipal();
|
||||
assertThat(user.getAuthorities()).hasSize(1);
|
||||
@@ -142,7 +144,7 @@ public class LdapAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void authenticateWithNamingException() {
|
||||
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken("ben",
|
||||
UsernamePasswordAuthenticationToken authRequest = UsernamePasswordAuthenticationToken.unauthenticated("ben",
|
||||
"benspassword");
|
||||
LdapAuthenticator mockAuthenticator = mock(LdapAuthenticator.class);
|
||||
CommunicationException expectedCause = new CommunicationException(new javax.naming.CommunicationException());
|
||||
|
||||
@@ -53,7 +53,7 @@ public class PasswordComparisonAuthenticatorMockTests {
|
||||
final NamingEnumeration searchResults = new BasicAttributes("", null).getAll();
|
||||
given(dirCtx.search(eq("cn=Bob,ou=people"), eq("(userPassword={0})"), any(Object[].class),
|
||||
any(SearchControls.class))).willReturn(searchResults);
|
||||
authenticator.authenticate(new UsernamePasswordAuthenticationToken("Bob", "bobspassword"));
|
||||
authenticator.authenticate(UsernamePasswordAuthenticationToken.unauthenticated("Bob", "bobspassword"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2022 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.
|
||||
@@ -68,7 +68,7 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
|
||||
|
||||
ActiveDirectoryLdapAuthenticationProvider provider;
|
||||
|
||||
UsernamePasswordAuthenticationToken joe = new UsernamePasswordAuthenticationToken("joe", "password");
|
||||
UsernamePasswordAuthenticationToken joe = UsernamePasswordAuthenticationToken.unauthenticated("joe", "password");
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
@@ -162,7 +162,7 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
|
||||
any(SearchControls.class))).willReturn(new MockNamingEnumeration(sr));
|
||||
this.provider.contextFactory = createContextFactoryReturning(ctx);
|
||||
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> this.provider.authenticate(this.joe));
|
||||
this.provider.authenticate(new UsernamePasswordAuthenticationToken("joe@mydomain.eu", "password"));
|
||||
this.provider.authenticate(UsernamePasswordAuthenticationToken.unauthenticated("joe@mydomain.eu", "password"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -189,8 +189,8 @@ public class ActiveDirectoryLdapAuthenticationProviderTests {
|
||||
// SEC-2500
|
||||
@Test
|
||||
public void sec2500PreventAnonymousBind() {
|
||||
assertThatExceptionOfType(BadCredentialsException.class)
|
||||
.isThrownBy(() -> this.provider.authenticate(new UsernamePasswordAuthenticationToken("rwinch", "")));
|
||||
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(
|
||||
() -> this.provider.authenticate(UsernamePasswordAuthenticationToken.unauthenticated("rwinch", "")));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user