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
d2f24ae5f5
commit
ac9c29b2a0
+2
-2
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -117,7 +117,7 @@ public abstract class AbstractUserDetailsReactiveAuthenticationManager
|
||||
}
|
||||
|
||||
private UsernamePasswordAuthenticationToken createUsernamePasswordAuthenticationToken(UserDetails userDetails) {
|
||||
return new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(),
|
||||
return UsernamePasswordAuthenticationToken.authenticated(userDetails, userDetails.getPassword(),
|
||||
userDetails.getAuthorities());
|
||||
}
|
||||
|
||||
|
||||
+28
@@ -32,6 +32,7 @@ import org.springframework.util.Assert;
|
||||
* <code>String</code>.
|
||||
*
|
||||
* @author Ben Alex
|
||||
* @author Norbert Nowak
|
||||
*/
|
||||
public class UsernamePasswordAuthenticationToken extends AbstractAuthenticationToken {
|
||||
|
||||
@@ -71,6 +72,33 @@ public class UsernamePasswordAuthenticationToken extends AbstractAuthenticationT
|
||||
super.setAuthenticated(true); // must use super, as we override
|
||||
}
|
||||
|
||||
/**
|
||||
* This factory method can be safely used by any code that wishes to create a
|
||||
* unauthenticated <code>UsernamePasswordAuthenticationToken</code>.
|
||||
* @param principal
|
||||
* @param credentials
|
||||
* @return UsernamePasswordAuthenticationToken with false isAuthenticated() result
|
||||
*
|
||||
* @since 5.7
|
||||
*/
|
||||
public static UsernamePasswordAuthenticationToken unauthenticated(Object principal, Object credentials) {
|
||||
return new UsernamePasswordAuthenticationToken(principal, credentials);
|
||||
}
|
||||
|
||||
/**
|
||||
* This factory method can be safely used by any code that wishes to create a
|
||||
* authenticated <code>UsernamePasswordAuthenticationToken</code>.
|
||||
* @param principal
|
||||
* @param credentials
|
||||
* @return UsernamePasswordAuthenticationToken with true isAuthenticated() result
|
||||
*
|
||||
* @since 5.7
|
||||
*/
|
||||
public static UsernamePasswordAuthenticationToken authenticated(Object principal, Object credentials,
|
||||
Collection<? extends GrantedAuthority> authorities) {
|
||||
return new UsernamePasswordAuthenticationToken(principal, credentials, authorities);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getCredentials() {
|
||||
return this.credentials;
|
||||
|
||||
+1
-1
@@ -193,7 +193,7 @@ public abstract class AbstractUserDetailsAuthenticationProvider
|
||||
// so subsequent attempts are successful even with encoded passwords.
|
||||
// Also ensure we return the original getDetails(), so that future
|
||||
// authentication events after cache expiry contain the details
|
||||
UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(principal,
|
||||
UsernamePasswordAuthenticationToken result = UsernamePasswordAuthenticationToken.authenticated(principal,
|
||||
authentication.getCredentials(), this.authoritiesMapper.mapAuthorities(user.getAuthorities()));
|
||||
result.setDetails(authentication.getDetails());
|
||||
this.logger.debug("Authenticated user");
|
||||
|
||||
+2
-1
@@ -47,7 +47,8 @@ public class RemoteAuthenticationManagerImpl implements RemoteAuthenticationMana
|
||||
@Override
|
||||
public Collection<? extends GrantedAuthority> attemptAuthentication(String username, String password)
|
||||
throws RemoteAuthenticationException {
|
||||
UsernamePasswordAuthenticationToken request = new UsernamePasswordAuthenticationToken(username, password);
|
||||
UsernamePasswordAuthenticationToken request = UsernamePasswordAuthenticationToken.unauthenticated(username,
|
||||
password);
|
||||
try {
|
||||
return this.authenticationManager.authenticate(request).getAuthorities();
|
||||
}
|
||||
|
||||
+1
-1
@@ -68,7 +68,7 @@ public class RemoteAuthenticationProvider implements AuthenticationProvider, Ini
|
||||
String password = (credentials != null) ? credentials.toString() : null;
|
||||
Collection<? extends GrantedAuthority> authorities = this.remoteAuthenticationManager
|
||||
.attemptAuthentication(username, password);
|
||||
return new UsernamePasswordAuthenticationToken(username, password, authorities);
|
||||
return UsernamePasswordAuthenticationToken.authenticated(username, password, authorities);
|
||||
}
|
||||
|
||||
public RemoteAuthenticationManager getRemoteAuthenticationManager() {
|
||||
|
||||
+3
-3
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015-2018 the original author or authors.
|
||||
* Copyright 2015-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.
|
||||
@@ -78,8 +78,8 @@ class UsernamePasswordAuthenticationTokenDeserializer extends JsonDeserializer<U
|
||||
List<GrantedAuthority> authorities = mapper.readValue(readJsonNode(jsonNode, "authorities").traverse(mapper),
|
||||
GRANTED_AUTHORITY_LIST);
|
||||
UsernamePasswordAuthenticationToken token = (!authenticated)
|
||||
? new UsernamePasswordAuthenticationToken(principal, credentials)
|
||||
: new UsernamePasswordAuthenticationToken(principal, credentials, authorities);
|
||||
? UsernamePasswordAuthenticationToken.unauthenticated(principal, credentials)
|
||||
: UsernamePasswordAuthenticationToken.authenticated(principal, credentials, authorities);
|
||||
JsonNode detailsNode = readJsonNode(jsonNode, "details");
|
||||
if (detailsNode.isNull() || detailsNode.isMissingNode()) {
|
||||
token.setDetails(null);
|
||||
|
||||
+3
-2
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2021 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.
|
||||
@@ -125,7 +125,8 @@ public class InMemoryUserDetailsManager implements UserDetailsManager, UserDetai
|
||||
// supplied password.
|
||||
if (this.authenticationManager != null) {
|
||||
this.logger.debug(LogMessage.format("Reauthenticating user '%s' for password change request.", username));
|
||||
this.authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, oldPassword));
|
||||
this.authenticationManager
|
||||
.authenticate(UsernamePasswordAuthenticationToken.unauthenticated(username, oldPassword));
|
||||
}
|
||||
else {
|
||||
this.logger.debug("No authentication manager set. Password won't be re-checked.");
|
||||
|
||||
+5
-4
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2021 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.
|
||||
@@ -271,7 +271,8 @@ public class JdbcUserDetailsManager extends JdbcDaoImpl implements UserDetailsMa
|
||||
// supplied password.
|
||||
if (this.authenticationManager != null) {
|
||||
this.logger.debug(LogMessage.format("Reauthenticating user '%s' for password change request.", username));
|
||||
this.authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, oldPassword));
|
||||
this.authenticationManager
|
||||
.authenticate(UsernamePasswordAuthenticationToken.unauthenticated(username, oldPassword));
|
||||
}
|
||||
else {
|
||||
this.logger.debug("No authentication manager set. Password won't be re-checked.");
|
||||
@@ -287,8 +288,8 @@ public class JdbcUserDetailsManager extends JdbcDaoImpl implements UserDetailsMa
|
||||
|
||||
protected Authentication createNewAuthentication(Authentication currentAuth, String newPassword) {
|
||||
UserDetails user = loadUserByUsername(currentAuth.getName());
|
||||
UsernamePasswordAuthenticationToken newAuthentication = new UsernamePasswordAuthenticationToken(user, null,
|
||||
user.getAuthorities());
|
||||
UsernamePasswordAuthenticationToken newAuthentication = UsernamePasswordAuthenticationToken.authenticated(user,
|
||||
null, user.getAuthorities());
|
||||
newAuthentication.setDetails(currentAuth.getDetails());
|
||||
return newAuthentication;
|
||||
}
|
||||
|
||||
+2
-1
@@ -34,7 +34,8 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
|
||||
*/
|
||||
public class AuthorizationFailureEventTests {
|
||||
|
||||
private final UsernamePasswordAuthenticationToken foo = new UsernamePasswordAuthenticationToken("foo", "bar");
|
||||
private final UsernamePasswordAuthenticationToken foo = UsernamePasswordAuthenticationToken.unauthenticated("foo",
|
||||
"bar");
|
||||
|
||||
private List<ConfigAttribute> attributes = SecurityConfig.createList("TEST");
|
||||
|
||||
|
||||
@@ -34,13 +34,13 @@ public class AuthorizedEventTests {
|
||||
@Test
|
||||
public void testRejectsNulls() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new AuthorizedEvent(null,
|
||||
SecurityConfig.createList("TEST"), new UsernamePasswordAuthenticationToken("foo", "bar")));
|
||||
SecurityConfig.createList("TEST"), UsernamePasswordAuthenticationToken.unauthenticated("foo", "bar")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRejectsNulls2() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new AuthorizedEvent(new SimpleMethodInvocation(), null,
|
||||
new UsernamePasswordAuthenticationToken("foo", "bar")));
|
||||
UsernamePasswordAuthenticationToken.unauthenticated("foo", "bar")));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+6
-6
@@ -44,8 +44,8 @@ public class RunAsManagerImplTests {
|
||||
|
||||
@Test
|
||||
public void testDoesNotReturnAdditionalAuthoritiesIfCalledWithoutARunAsSetting() {
|
||||
UsernamePasswordAuthenticationToken inputToken = new UsernamePasswordAuthenticationToken("Test", "Password",
|
||||
AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO"));
|
||||
UsernamePasswordAuthenticationToken inputToken = UsernamePasswordAuthenticationToken.authenticated("Test",
|
||||
"Password", AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO"));
|
||||
RunAsManagerImpl runAs = new RunAsManagerImpl();
|
||||
runAs.setKey("my_password");
|
||||
Authentication resultingToken = runAs.buildRunAs(inputToken, new Object(),
|
||||
@@ -55,8 +55,8 @@ public class RunAsManagerImplTests {
|
||||
|
||||
@Test
|
||||
public void testRespectsRolePrefix() {
|
||||
UsernamePasswordAuthenticationToken inputToken = new UsernamePasswordAuthenticationToken("Test", "Password",
|
||||
AuthorityUtils.createAuthorityList("ONE", "TWO"));
|
||||
UsernamePasswordAuthenticationToken inputToken = UsernamePasswordAuthenticationToken.authenticated("Test",
|
||||
"Password", AuthorityUtils.createAuthorityList("ONE", "TWO"));
|
||||
RunAsManagerImpl runAs = new RunAsManagerImpl();
|
||||
runAs.setKey("my_password");
|
||||
runAs.setRolePrefix("FOOBAR_");
|
||||
@@ -75,8 +75,8 @@ public class RunAsManagerImplTests {
|
||||
|
||||
@Test
|
||||
public void testReturnsAdditionalGrantedAuthorities() {
|
||||
UsernamePasswordAuthenticationToken inputToken = new UsernamePasswordAuthenticationToken("Test", "Password",
|
||||
AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO"));
|
||||
UsernamePasswordAuthenticationToken inputToken = UsernamePasswordAuthenticationToken.authenticated("Test",
|
||||
"Password", AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO"));
|
||||
RunAsManagerImpl runAs = new RunAsManagerImpl();
|
||||
runAs.setKey("my_password");
|
||||
Authentication result = runAs.buildRunAs(inputToken, new Object(),
|
||||
|
||||
+1
-1
@@ -44,7 +44,7 @@ public class AuthenticatedVoterTests {
|
||||
}
|
||||
|
||||
private Authentication createFullyAuthenticated() {
|
||||
return new UsernamePasswordAuthenticationToken("ignored", "ignored",
|
||||
return UsernamePasswordAuthenticationToken.authenticated("ignored", "ignored",
|
||||
AuthorityUtils.createAuthorityList("ignored"));
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -66,12 +66,13 @@ public class ProviderManagerTests {
|
||||
|
||||
@Test
|
||||
public void credentialsAreClearedByDefault() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("Test", "Password");
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("Test",
|
||||
"Password");
|
||||
ProviderManager mgr = makeProviderManager();
|
||||
Authentication result = mgr.authenticate(token);
|
||||
assertThat(result.getCredentials()).isNull();
|
||||
mgr.setEraseCredentialsAfterAuthentication(false);
|
||||
token = new UsernamePasswordAuthenticationToken("Test", "Password");
|
||||
token = UsernamePasswordAuthenticationToken.unauthenticated("Test", "Password");
|
||||
result = mgr.authenticate(token);
|
||||
assertThat(result.getCredentials()).isNotNull();
|
||||
}
|
||||
|
||||
+6
-6
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 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.
|
||||
@@ -72,7 +72,7 @@ public class ReactiveUserDetailsServiceAuthenticationManagerTests {
|
||||
@Test
|
||||
public void authenticateWhenUserNotFoundThenBadCredentials() {
|
||||
given(this.repository.findByUsername(this.username)).willReturn(Mono.empty());
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(this.username,
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated(this.username,
|
||||
this.password);
|
||||
Mono<Authentication> authentication = this.manager.authenticate(token);
|
||||
// @formatter:off
|
||||
@@ -91,7 +91,7 @@ public class ReactiveUserDetailsServiceAuthenticationManagerTests {
|
||||
.build();
|
||||
// @formatter:on
|
||||
given(this.repository.findByUsername(user.getUsername())).willReturn(Mono.just(user));
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(this.username,
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated(this.username,
|
||||
this.password + "INVALID");
|
||||
Mono<Authentication> authentication = this.manager.authenticate(token);
|
||||
// @formatter:off
|
||||
@@ -110,7 +110,7 @@ public class ReactiveUserDetailsServiceAuthenticationManagerTests {
|
||||
.build();
|
||||
// @formatter:on
|
||||
given(this.repository.findByUsername(user.getUsername())).willReturn(Mono.just(user));
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(this.username,
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated(this.username,
|
||||
this.password);
|
||||
Authentication authentication = this.manager.authenticate(token).block();
|
||||
assertThat(authentication).isEqualTo(authentication);
|
||||
@@ -122,7 +122,7 @@ public class ReactiveUserDetailsServiceAuthenticationManagerTests {
|
||||
given(this.passwordEncoder.matches(any(), any())).willReturn(true);
|
||||
User user = new User(this.username, this.password, AuthorityUtils.createAuthorityList("ROLE_USER"));
|
||||
given(this.repository.findByUsername(user.getUsername())).willReturn(Mono.just(user));
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(this.username,
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated(this.username,
|
||||
this.password);
|
||||
Authentication authentication = this.manager.authenticate(token).block();
|
||||
assertThat(authentication).isEqualTo(authentication);
|
||||
@@ -134,7 +134,7 @@ public class ReactiveUserDetailsServiceAuthenticationManagerTests {
|
||||
given(this.passwordEncoder.matches(any(), any())).willReturn(false);
|
||||
User user = new User(this.username, this.password, AuthorityUtils.createAuthorityList("ROLE_USER"));
|
||||
given(this.repository.findByUsername(user.getUsername())).willReturn(Mono.just(user));
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(this.username,
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated(this.username,
|
||||
this.password);
|
||||
Mono<Authentication> authentication = this.manager.authenticate(token);
|
||||
// @formatter:off
|
||||
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -35,7 +35,7 @@ public class TestAuthentication extends PasswordEncodedUser {
|
||||
}
|
||||
|
||||
public static Authentication autheticated(UserDetails user) {
|
||||
return new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
|
||||
return UsernamePasswordAuthenticationToken.authenticated(user, null, user.getAuthorities());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+11
-11
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2021 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.
|
||||
@@ -95,7 +95,7 @@ public class UserDetailsRepositoryReactiveAuthenticationManagerTests {
|
||||
given(this.encoder.matches(any(), any())).willReturn(true);
|
||||
this.manager.setScheduler(this.scheduler);
|
||||
this.manager.setPasswordEncoder(this.encoder);
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(this.user,
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated(this.user,
|
||||
this.user.getPassword());
|
||||
Authentication result = this.manager.authenticate(token).block();
|
||||
verify(this.scheduler).schedule(any());
|
||||
@@ -111,7 +111,7 @@ public class UserDetailsRepositoryReactiveAuthenticationManagerTests {
|
||||
given(this.userDetailsPasswordService.updatePassword(any(), any())).willReturn(Mono.just(this.user));
|
||||
this.manager.setPasswordEncoder(this.encoder);
|
||||
this.manager.setUserDetailsPasswordService(this.userDetailsPasswordService);
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(this.user,
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated(this.user,
|
||||
this.user.getPassword());
|
||||
Authentication result = this.manager.authenticate(token).block();
|
||||
verify(this.encoder).encode(this.user.getPassword());
|
||||
@@ -124,7 +124,7 @@ public class UserDetailsRepositoryReactiveAuthenticationManagerTests {
|
||||
given(this.encoder.matches(any(), any())).willReturn(false);
|
||||
this.manager.setPasswordEncoder(this.encoder);
|
||||
this.manager.setUserDetailsPasswordService(this.userDetailsPasswordService);
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(this.user,
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated(this.user,
|
||||
this.user.getPassword());
|
||||
assertThatExceptionOfType(BadCredentialsException.class)
|
||||
.isThrownBy(() -> this.manager.authenticate(token).block());
|
||||
@@ -138,7 +138,7 @@ public class UserDetailsRepositoryReactiveAuthenticationManagerTests {
|
||||
given(this.encoder.upgradeEncoding(any())).willReturn(false);
|
||||
this.manager.setPasswordEncoder(this.encoder);
|
||||
this.manager.setUserDetailsPasswordService(this.userDetailsPasswordService);
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(this.user,
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated(this.user,
|
||||
this.user.getPassword());
|
||||
Authentication result = this.manager.authenticate(token).block();
|
||||
verifyZeroInteractions(this.userDetailsPasswordService);
|
||||
@@ -152,8 +152,8 @@ public class UserDetailsRepositoryReactiveAuthenticationManagerTests {
|
||||
this.manager.setPasswordEncoder(this.encoder);
|
||||
this.manager.setPostAuthenticationChecks(this.postAuthenticationChecks);
|
||||
assertThatExceptionOfType(LockedException.class).isThrownBy(() -> this.manager
|
||||
.authenticate(new UsernamePasswordAuthenticationToken(this.user, this.user.getPassword())).block())
|
||||
.withMessage("account is locked");
|
||||
.authenticate(UsernamePasswordAuthenticationToken.unauthenticated(this.user, this.user.getPassword()))
|
||||
.block()).withMessage("account is locked");
|
||||
verify(this.postAuthenticationChecks).check(eq(this.user));
|
||||
}
|
||||
|
||||
@@ -162,7 +162,7 @@ public class UserDetailsRepositoryReactiveAuthenticationManagerTests {
|
||||
given(this.userDetailsService.findByUsername(any())).willReturn(Mono.just(this.user));
|
||||
given(this.encoder.matches(any(), any())).willReturn(true);
|
||||
this.manager.setPasswordEncoder(this.encoder);
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(this.user,
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated(this.user,
|
||||
this.user.getPassword());
|
||||
this.manager.authenticate(token).block();
|
||||
verifyZeroInteractions(this.postAuthenticationChecks);
|
||||
@@ -179,7 +179,7 @@ public class UserDetailsRepositoryReactiveAuthenticationManagerTests {
|
||||
.build();
|
||||
// @formatter:on
|
||||
given(this.userDetailsService.findByUsername(any())).willReturn(Mono.just(expiredUser));
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(expiredUser,
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated(expiredUser,
|
||||
expiredUser.getPassword());
|
||||
assertThatExceptionOfType(AccountExpiredException.class)
|
||||
.isThrownBy(() -> this.manager.authenticate(token).block());
|
||||
@@ -196,7 +196,7 @@ public class UserDetailsRepositoryReactiveAuthenticationManagerTests {
|
||||
.build();
|
||||
// @formatter:on
|
||||
given(this.userDetailsService.findByUsername(any())).willReturn(Mono.just(lockedUser));
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(lockedUser,
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated(lockedUser,
|
||||
lockedUser.getPassword());
|
||||
assertThatExceptionOfType(LockedException.class).isThrownBy(() -> this.manager.authenticate(token).block());
|
||||
}
|
||||
@@ -212,7 +212,7 @@ public class UserDetailsRepositoryReactiveAuthenticationManagerTests {
|
||||
.build();
|
||||
// @formatter:on
|
||||
given(this.userDetailsService.findByUsername(any())).willReturn(Mono.just(disabledUser));
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(disabledUser,
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated(disabledUser,
|
||||
disabledUser.getPassword());
|
||||
assertThatExceptionOfType(DisabledException.class).isThrownBy(() -> this.manager.authenticate(token).block());
|
||||
}
|
||||
|
||||
+20
-6
@@ -33,8 +33,8 @@ public class UsernamePasswordAuthenticationTokenTests {
|
||||
|
||||
@Test
|
||||
public void authenticatedPropertyContractIsSatisfied() {
|
||||
UsernamePasswordAuthenticationToken grantedToken = new UsernamePasswordAuthenticationToken("Test", "Password",
|
||||
AuthorityUtils.NO_AUTHORITIES);
|
||||
UsernamePasswordAuthenticationToken grantedToken = UsernamePasswordAuthenticationToken.authenticated("Test",
|
||||
"Password", AuthorityUtils.NO_AUTHORITIES);
|
||||
// check default given we passed some GrantedAuthorty[]s (well, we passed empty
|
||||
// list)
|
||||
assertThat(grantedToken.isAuthenticated()).isTrue();
|
||||
@@ -44,8 +44,8 @@ public class UsernamePasswordAuthenticationTokenTests {
|
||||
assertThat(!grantedToken.isAuthenticated()).isTrue();
|
||||
// Now let's create a UsernamePasswordAuthenticationToken without any
|
||||
// GrantedAuthorty[]s (different constructor)
|
||||
UsernamePasswordAuthenticationToken noneGrantedToken = new UsernamePasswordAuthenticationToken("Test",
|
||||
"Password");
|
||||
UsernamePasswordAuthenticationToken noneGrantedToken = UsernamePasswordAuthenticationToken
|
||||
.unauthenticated("Test", "Password");
|
||||
assertThat(!noneGrantedToken.isAuthenticated()).isTrue();
|
||||
// check we're allowed to still set it to untrusted
|
||||
noneGrantedToken.setAuthenticated(false);
|
||||
@@ -56,8 +56,8 @@ public class UsernamePasswordAuthenticationTokenTests {
|
||||
|
||||
@Test
|
||||
public void gettersReturnCorrectData() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("Test", "Password",
|
||||
AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO"));
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.authenticated("Test",
|
||||
"Password", AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO"));
|
||||
assertThat(token.getPrincipal()).isEqualTo("Test");
|
||||
assertThat(token.getCredentials()).isEqualTo("Password");
|
||||
assertThat(AuthorityUtils.authorityListToSet(token.getAuthorities())).contains("ROLE_ONE");
|
||||
@@ -71,4 +71,18 @@ public class UsernamePasswordAuthenticationTokenTests {
|
||||
.isThrownBy(() -> clazz.getDeclaredConstructor((Class[]) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unauthenticatedFactoryMethodResultsUnauthenticatedToken() {
|
||||
UsernamePasswordAuthenticationToken grantedToken = UsernamePasswordAuthenticationToken.unauthenticated("Test",
|
||||
"Password");
|
||||
assertThat(grantedToken.isAuthenticated()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void authenticatedFactoryMethodResultsAuthenticatedToken() {
|
||||
UsernamePasswordAuthenticationToken grantedToken = UsernamePasswordAuthenticationToken.authenticated("Test",
|
||||
"Password", AuthorityUtils.NO_AUTHORITIES);
|
||||
assertThat(grantedToken.isAuthenticated()).isTrue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
-2
@@ -81,8 +81,8 @@ public class AnonymousAuthenticationTokenTests {
|
||||
@Test
|
||||
public void testNotEqualsDueToDifferentAuthenticationClass() {
|
||||
AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken("key", "Test", ROLES_12);
|
||||
UsernamePasswordAuthenticationToken token2 = new UsernamePasswordAuthenticationToken("Test", "Password",
|
||||
ROLES_12);
|
||||
UsernamePasswordAuthenticationToken token2 = UsernamePasswordAuthenticationToken.authenticated("Test",
|
||||
"Password", ROLES_12);
|
||||
assertThat(token1.equals(token2)).isFalse();
|
||||
}
|
||||
|
||||
|
||||
+48
-31
@@ -74,7 +74,7 @@ public class DaoAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testAuthenticateFailsForIncorrectPasswordCase() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("rod", "KOala");
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("rod", "KOala");
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
@@ -87,14 +87,16 @@ public class DaoAuthenticationProviderTests {
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken("rod", null);
|
||||
UsernamePasswordAuthenticationToken authenticationToken = UsernamePasswordAuthenticationToken
|
||||
.unauthenticated("rod", null);
|
||||
assertThatExceptionOfType(BadCredentialsException.class)
|
||||
.isThrownBy(() -> provider.authenticate(authenticationToken));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthenticateFailsIfAccountExpired() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("peter", "opal");
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("peter",
|
||||
"opal");
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserPeterAccountExpired());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
@@ -103,7 +105,8 @@ public class DaoAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testAuthenticateFailsIfAccountLocked() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("peter", "opal");
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("peter",
|
||||
"opal");
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserPeterAccountLocked());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
@@ -115,17 +118,18 @@ public class DaoAuthenticationProviderTests {
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserPeterCredentialsExpired());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
assertThatExceptionOfType(CredentialsExpiredException.class)
|
||||
.isThrownBy(() -> provider.authenticate(new UsernamePasswordAuthenticationToken("peter", "opal")));
|
||||
assertThatExceptionOfType(CredentialsExpiredException.class).isThrownBy(
|
||||
() -> provider.authenticate(UsernamePasswordAuthenticationToken.unauthenticated("peter", "opal")));
|
||||
// Check that wrong password causes BadCredentialsException, rather than
|
||||
// CredentialsExpiredException
|
||||
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(
|
||||
() -> provider.authenticate(new UsernamePasswordAuthenticationToken("peter", "wrong_password")));
|
||||
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> provider
|
||||
.authenticate(UsernamePasswordAuthenticationToken.unauthenticated("peter", "wrong_password")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthenticateFailsIfUserDisabled() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("peter", "opal");
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("peter",
|
||||
"opal");
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserPeter());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
@@ -134,7 +138,7 @@ public class DaoAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testAuthenticateFailsWhenAuthenticationDaoHasBackendFailure() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("rod", "koala");
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("rod", "koala");
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceSimulateBackendError());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
@@ -144,7 +148,7 @@ public class DaoAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testAuthenticateFailsWithEmptyUsername() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(null, "koala");
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated(null, "koala");
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
@@ -153,7 +157,8 @@ public class DaoAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testAuthenticateFailsWithInvalidPassword() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("rod", "INVALID_PASSWORD");
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("rod",
|
||||
"INVALID_PASSWORD");
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
@@ -162,7 +167,8 @@ public class DaoAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testAuthenticateFailsWithInvalidUsernameAndHideUserNotFoundExceptionFalse() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("INVALID_USER", "koala");
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("INVALID_USER",
|
||||
"koala");
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setHideUserNotFoundExceptions(false); // we want
|
||||
// UsernameNotFoundExceptions
|
||||
@@ -173,7 +179,8 @@ public class DaoAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testAuthenticateFailsWithInvalidUsernameAndHideUserNotFoundExceptionsWithDefaultOfTrue() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("INVALID_USER", "koala");
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("INVALID_USER",
|
||||
"koala");
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
assertThat(provider.isHideUserNotFoundExceptions()).isTrue();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
|
||||
@@ -183,7 +190,8 @@ public class DaoAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testAuthenticateFailsWithInvalidUsernameAndChangePasswordEncoder() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("INVALID_USER", "koala");
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("INVALID_USER",
|
||||
"koala");
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
assertThat(provider.isHideUserNotFoundExceptions()).isTrue();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
|
||||
@@ -195,7 +203,7 @@ public class DaoAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testAuthenticateFailsWithMixedCaseUsernameIfDefaultChanged() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("RoD", "koala");
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("RoD", "koala");
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
@@ -204,7 +212,7 @@ public class DaoAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testAuthenticates() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("rod", "koala");
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("rod", "koala");
|
||||
token.setDetails("192.168.0.1");
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
|
||||
@@ -222,7 +230,7 @@ public class DaoAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testAuthenticatesASecondTime() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("rod", "koala");
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("rod", "koala");
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
@@ -240,7 +248,7 @@ public class DaoAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testAuthenticatesWithForcePrincipalAsString() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("rod", "koala");
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("rod", "koala");
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
|
||||
provider.setUserCache(new MockUserCache());
|
||||
@@ -258,7 +266,8 @@ public class DaoAuthenticationProviderTests {
|
||||
public void authenticateWhenSuccessAndPasswordManagerThenUpdates() {
|
||||
String password = "password";
|
||||
String encodedPassword = "encoded";
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("user", password);
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("user",
|
||||
password);
|
||||
PasswordEncoder encoder = mock(PasswordEncoder.class);
|
||||
UserDetailsService userDetailsService = mock(UserDetailsService.class);
|
||||
UserDetailsPasswordService passwordManager = mock(UserDetailsPasswordService.class);
|
||||
@@ -279,7 +288,8 @@ public class DaoAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void authenticateWhenBadCredentialsAndPasswordManagerThenNoUpdate() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("user", "password");
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("user",
|
||||
"password");
|
||||
PasswordEncoder encoder = mock(PasswordEncoder.class);
|
||||
UserDetailsService userDetailsService = mock(UserDetailsService.class);
|
||||
UserDetailsPasswordService passwordManager = mock(UserDetailsPasswordService.class);
|
||||
@@ -296,7 +306,8 @@ public class DaoAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void authenticateWhenNotUpgradeAndPasswordManagerThenNoUpdate() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("user", "password");
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("user",
|
||||
"password");
|
||||
PasswordEncoder encoder = mock(PasswordEncoder.class);
|
||||
UserDetailsService userDetailsService = mock(UserDetailsService.class);
|
||||
UserDetailsPasswordService passwordManager = mock(UserDetailsPasswordService.class);
|
||||
@@ -314,7 +325,7 @@ public class DaoAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testDetectsNullBeingReturnedFromAuthenticationDao() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("rod", "koala");
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("rod", "koala");
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceReturnsNull());
|
||||
assertThatExceptionOfType(AuthenticationServiceException.class).isThrownBy(() -> provider.authenticate(token))
|
||||
@@ -335,7 +346,7 @@ public class DaoAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testGoesBackToAuthenticationDaoToObtainLatestPasswordIfCachedPasswordSeemsIncorrect() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("rod", "koala");
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("rod", "koala");
|
||||
MockUserDetailsServiceUserRod authenticationDao = new MockUserDetailsServiceUserRod();
|
||||
MockUserCache cache = new MockUserCache();
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
@@ -348,7 +359,7 @@ public class DaoAuthenticationProviderTests {
|
||||
// Now change the password the AuthenticationDao will return
|
||||
authenticationDao.setPassword("easternLongNeckTurtle");
|
||||
// Now try authentication again, with the new password
|
||||
token = new UsernamePasswordAuthenticationToken("rod", "easternLongNeckTurtle");
|
||||
token = UsernamePasswordAuthenticationToken.unauthenticated("rod", "easternLongNeckTurtle");
|
||||
provider.authenticate(token);
|
||||
// To get this far, the new password was accepted
|
||||
// Check the cache was updated
|
||||
@@ -390,7 +401,8 @@ public class DaoAuthenticationProviderTests {
|
||||
// SEC-2056
|
||||
@Test
|
||||
public void testUserNotFoundEncodesPassword() throws Exception {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("missing", "koala");
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("missing",
|
||||
"koala");
|
||||
PasswordEncoder encoder = mock(PasswordEncoder.class);
|
||||
given(encoder.encode(anyString())).willReturn("koala");
|
||||
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
|
||||
@@ -406,7 +418,8 @@ public class DaoAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testUserNotFoundBCryptPasswordEncoder() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("missing", "koala");
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("missing",
|
||||
"koala");
|
||||
PasswordEncoder encoder = new BCryptPasswordEncoder();
|
||||
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
|
||||
provider.setHideUserNotFoundExceptions(false);
|
||||
@@ -419,7 +432,8 @@ public class DaoAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testUserNotFoundDefaultEncoder() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("missing", null);
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("missing",
|
||||
null);
|
||||
DaoAuthenticationProvider provider = createProvider();
|
||||
provider.setHideUserNotFoundExceptions(false);
|
||||
provider.setUserDetailsService(new MockUserDetailsServiceUserRod());
|
||||
@@ -432,8 +446,10 @@ public class DaoAuthenticationProviderTests {
|
||||
* SEC-2056 is fixed.
|
||||
*/
|
||||
public void IGNOREtestSec2056() {
|
||||
UsernamePasswordAuthenticationToken foundUser = new UsernamePasswordAuthenticationToken("rod", "koala");
|
||||
UsernamePasswordAuthenticationToken notFoundUser = new UsernamePasswordAuthenticationToken("notFound", "koala");
|
||||
UsernamePasswordAuthenticationToken foundUser = UsernamePasswordAuthenticationToken.unauthenticated("rod",
|
||||
"koala");
|
||||
UsernamePasswordAuthenticationToken notFoundUser = UsernamePasswordAuthenticationToken
|
||||
.unauthenticated("notFound", "koala");
|
||||
PasswordEncoder encoder = new BCryptPasswordEncoder(10, new SecureRandom());
|
||||
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
|
||||
provider.setHideUserNotFoundExceptions(false);
|
||||
@@ -467,7 +483,8 @@ public class DaoAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testUserNotFoundNullCredentials() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("missing", null);
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("missing",
|
||||
null);
|
||||
PasswordEncoder encoder = mock(PasswordEncoder.class);
|
||||
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
|
||||
provider.setHideUserNotFoundExceptions(false);
|
||||
|
||||
+2
-2
@@ -34,8 +34,8 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
|
||||
public class AuthenticationEventTests {
|
||||
|
||||
private Authentication getAuthentication() {
|
||||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("Principal",
|
||||
"Credentials");
|
||||
UsernamePasswordAuthenticationToken authentication = UsernamePasswordAuthenticationToken
|
||||
.unauthenticated("Principal", "Credentials");
|
||||
authentication.setDetails("127.0.0.1");
|
||||
return authentication;
|
||||
}
|
||||
|
||||
+2
-2
@@ -30,8 +30,8 @@ import org.springframework.security.core.Authentication;
|
||||
public class LoggerListenerTests {
|
||||
|
||||
private Authentication getAuthentication() {
|
||||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("Principal",
|
||||
"Credentials");
|
||||
UsernamePasswordAuthenticationToken authentication = UsernamePasswordAuthenticationToken
|
||||
.unauthenticated("Principal", "Credentials");
|
||||
authentication.setDetails("127.0.0.1");
|
||||
return authentication;
|
||||
}
|
||||
|
||||
+6
-6
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2016 the original author or authors.
|
||||
* Copyright 2010-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.
|
||||
@@ -79,7 +79,7 @@ public class DefaultJaasAuthenticationProviderTests {
|
||||
new AppConfigurationEntry(TestLoginModule.class.getName(), LoginModuleControlFlag.REQUIRED,
|
||||
Collections.<String, Object>emptyMap()) };
|
||||
given(configuration.getAppConfigurationEntry(this.provider.getLoginContextName())).willReturn(aces);
|
||||
this.token = new UsernamePasswordAuthenticationToken("user", "password");
|
||||
this.token = UsernamePasswordAuthenticationToken.unauthenticated("user", "password");
|
||||
ReflectionTestUtils.setField(this.provider, "log", this.log);
|
||||
}
|
||||
|
||||
@@ -113,15 +113,15 @@ public class DefaultJaasAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void authenticateBadPassword() {
|
||||
assertThatExceptionOfType(AuthenticationException.class)
|
||||
.isThrownBy(() -> this.provider.authenticate(new UsernamePasswordAuthenticationToken("user", "asdf")));
|
||||
assertThatExceptionOfType(AuthenticationException.class).isThrownBy(
|
||||
() -> this.provider.authenticate(UsernamePasswordAuthenticationToken.unauthenticated("user", "asdf")));
|
||||
verifyFailedLogin();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void authenticateBadUser() {
|
||||
assertThatExceptionOfType(AuthenticationException.class).isThrownBy(
|
||||
() -> this.provider.authenticate(new UsernamePasswordAuthenticationToken("asdf", "password")));
|
||||
assertThatExceptionOfType(AuthenticationException.class).isThrownBy(() -> this.provider
|
||||
.authenticate(UsernamePasswordAuthenticationToken.unauthenticated("asdf", "password")));
|
||||
verifyFailedLogin();
|
||||
}
|
||||
|
||||
|
||||
+9
-8
@@ -75,8 +75,8 @@ public class JaasAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testBadPassword() {
|
||||
assertThatExceptionOfType(AuthenticationException.class).isThrownBy(
|
||||
() -> this.jaasProvider.authenticate(new UsernamePasswordAuthenticationToken("user", "asdf")));
|
||||
assertThatExceptionOfType(AuthenticationException.class).isThrownBy(() -> this.jaasProvider
|
||||
.authenticate(UsernamePasswordAuthenticationToken.unauthenticated("user", "asdf")));
|
||||
assertThat(this.eventCheck.failedEvent).as("Failure event not fired").isNotNull();
|
||||
assertThat(this.eventCheck.failedEvent.getException()).withFailMessage("Failure event exception was null")
|
||||
.isNotNull();
|
||||
@@ -85,8 +85,8 @@ public class JaasAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testBadUser() {
|
||||
assertThatExceptionOfType(AuthenticationException.class).isThrownBy(
|
||||
() -> this.jaasProvider.authenticate(new UsernamePasswordAuthenticationToken("asdf", "password")));
|
||||
assertThatExceptionOfType(AuthenticationException.class).isThrownBy(() -> this.jaasProvider
|
||||
.authenticate(UsernamePasswordAuthenticationToken.unauthenticated("asdf", "password")));
|
||||
assertThat(this.eventCheck.failedEvent).as("Failure event not fired").isNotNull();
|
||||
assertThat(this.eventCheck.failedEvent.getException()).withFailMessage("Failure event exception was null")
|
||||
.isNotNull();
|
||||
@@ -158,8 +158,8 @@ public class JaasAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testFull() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("user", "password",
|
||||
AuthorityUtils.createAuthorityList("ROLE_ONE"));
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.authenticated("user",
|
||||
"password", AuthorityUtils.createAuthorityList("ROLE_ONE"));
|
||||
assertThat(this.jaasProvider.supports(UsernamePasswordAuthenticationToken.class)).isTrue();
|
||||
Authentication auth = this.jaasProvider.authenticate(token);
|
||||
assertThat(this.jaasProvider.getAuthorityGranters()).isNotNull();
|
||||
@@ -198,7 +198,7 @@ public class JaasAuthenticationProviderTests {
|
||||
assertThat(this.jaasProvider.getLoginExceptionResolver()).isNotNull();
|
||||
this.jaasProvider.setLoginExceptionResolver((e) -> new LockedException("This is just a test!"));
|
||||
try {
|
||||
this.jaasProvider.authenticate(new UsernamePasswordAuthenticationToken("user", "password"));
|
||||
this.jaasProvider.authenticate(UsernamePasswordAuthenticationToken.unauthenticated("user", "password"));
|
||||
}
|
||||
catch (LockedException ex) {
|
||||
}
|
||||
@@ -221,7 +221,8 @@ public class JaasAuthenticationProviderTests {
|
||||
|
||||
@Test
|
||||
public void testNullDefaultAuthorities() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("user", "password");
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("user",
|
||||
"password");
|
||||
assertThat(this.jaasProvider.supports(UsernamePasswordAuthenticationToken.class)).isTrue();
|
||||
Authentication auth = this.jaasProvider.authenticate(token);
|
||||
assertThat(auth.getAuthorities()).withFailMessage("Only ROLE_TEST1 and ROLE_TEST2 should have been returned")
|
||||
|
||||
+3
-3
@@ -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.
|
||||
@@ -56,8 +56,8 @@ public class Sec760Tests {
|
||||
}
|
||||
|
||||
private void testAuthenticate(JaasAuthenticationProvider p1) {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("user", "password",
|
||||
AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO"));
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.authenticated("user",
|
||||
"password", AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO"));
|
||||
Authentication auth = p1.authenticate(token);
|
||||
assertThat(auth).isNotNull();
|
||||
}
|
||||
|
||||
+1
-1
@@ -44,7 +44,7 @@ public class SecurityContextLoginModuleTests {
|
||||
|
||||
private Subject subject = new Subject(false, new HashSet<>(), new HashSet<>(), new HashSet<>());
|
||||
|
||||
private UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken("principal",
|
||||
private UsernamePasswordAuthenticationToken auth = UsernamePasswordAuthenticationToken.unauthenticated("principal",
|
||||
"credentials");
|
||||
|
||||
@BeforeEach
|
||||
|
||||
+6
-5
@@ -40,8 +40,8 @@ public class RemoteAuthenticationProviderTests {
|
||||
public void testExceptionsGetPassedBackToCaller() {
|
||||
RemoteAuthenticationProvider provider = new RemoteAuthenticationProvider();
|
||||
provider.setRemoteAuthenticationManager(new MockRemoteAuthenticationManager(false));
|
||||
assertThatExceptionOfType(RemoteAuthenticationException.class)
|
||||
.isThrownBy(() -> provider.authenticate(new UsernamePasswordAuthenticationToken("rod", "password")));
|
||||
assertThatExceptionOfType(RemoteAuthenticationException.class).isThrownBy(
|
||||
() -> provider.authenticate(UsernamePasswordAuthenticationToken.unauthenticated("rod", "password")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -63,7 +63,8 @@ public class RemoteAuthenticationProviderTests {
|
||||
public void testSuccessfulAuthenticationCreatesObject() {
|
||||
RemoteAuthenticationProvider provider = new RemoteAuthenticationProvider();
|
||||
provider.setRemoteAuthenticationManager(new MockRemoteAuthenticationManager(true));
|
||||
Authentication result = provider.authenticate(new UsernamePasswordAuthenticationToken("rod", "password"));
|
||||
Authentication result = provider
|
||||
.authenticate(UsernamePasswordAuthenticationToken.unauthenticated("rod", "password"));
|
||||
assertThat(result.getPrincipal()).isEqualTo("rod");
|
||||
assertThat(result.getCredentials()).isEqualTo("password");
|
||||
assertThat(AuthorityUtils.authorityListToSet(result.getAuthorities())).contains("foo");
|
||||
@@ -73,8 +74,8 @@ public class RemoteAuthenticationProviderTests {
|
||||
public void testNullCredentialsDoesNotCauseNullPointerException() {
|
||||
RemoteAuthenticationProvider provider = new RemoteAuthenticationProvider();
|
||||
provider.setRemoteAuthenticationManager(new MockRemoteAuthenticationManager(false));
|
||||
assertThatExceptionOfType(RemoteAuthenticationException.class)
|
||||
.isThrownBy(() -> provider.authenticate(new UsernamePasswordAuthenticationToken("rod", null)));
|
||||
assertThatExceptionOfType(RemoteAuthenticationException.class).isThrownBy(
|
||||
() -> provider.authenticate(UsernamePasswordAuthenticationToken.unauthenticated("rod", null)));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+2
-2
@@ -76,8 +76,8 @@ public class RememberMeAuthenticationTokenTests {
|
||||
@Test
|
||||
public void testNotEqualsDueToDifferentAuthenticationClass() {
|
||||
RememberMeAuthenticationToken token1 = new RememberMeAuthenticationToken("key", "Test", ROLES_12);
|
||||
UsernamePasswordAuthenticationToken token2 = new UsernamePasswordAuthenticationToken("Test", "Password",
|
||||
ROLES_12);
|
||||
UsernamePasswordAuthenticationToken token2 = UsernamePasswordAuthenticationToken.authenticated("Test",
|
||||
"Password", ROLES_12);
|
||||
assertThat(token1.equals(token2)).isFalse();
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -41,7 +41,7 @@ public class SecurityContextHolderTests {
|
||||
@Test
|
||||
public void testContextHolderGetterSetterClearer() {
|
||||
SecurityContext sc = new SecurityContextImpl();
|
||||
sc.setAuthentication(new UsernamePasswordAuthenticationToken("Foobar", "pass"));
|
||||
sc.setAuthentication(UsernamePasswordAuthenticationToken.unauthenticated("Foobar", "pass"));
|
||||
SecurityContextHolder.setContext(sc);
|
||||
assertThat(SecurityContextHolder.getContext()).isEqualTo(sc);
|
||||
SecurityContextHolder.clearContext();
|
||||
|
||||
+1
-1
@@ -40,7 +40,7 @@ public class SecurityContextImplTests {
|
||||
@Test
|
||||
public void testSecurityContextCorrectOperation() {
|
||||
SecurityContext context = new SecurityContextImpl();
|
||||
Authentication auth = new UsernamePasswordAuthenticationToken("rod", "koala");
|
||||
Authentication auth = UsernamePasswordAuthenticationToken.unauthenticated("rod", "koala");
|
||||
context.setAuthentication(auth);
|
||||
assertThat(context.getAuthentication()).isEqualTo(auth);
|
||||
assertThat(context.toString().lastIndexOf("rod") != -1).isTrue();
|
||||
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
* Copyright 2015-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.
|
||||
@@ -47,7 +47,7 @@ public class SecurityContextMixinTests extends AbstractMixinTests {
|
||||
@Test
|
||||
public void securityContextSerializeTest() throws JsonProcessingException, JSONException {
|
||||
SecurityContext context = new SecurityContextImpl();
|
||||
context.setAuthentication(new UsernamePasswordAuthenticationToken("admin", "1234",
|
||||
context.setAuthentication(UsernamePasswordAuthenticationToken.authenticated("admin", "1234",
|
||||
Collections.singleton(new SimpleGrantedAuthority("ROLE_USER"))));
|
||||
String actualJson = this.mapper.writeValueAsString(context);
|
||||
JSONAssert.assertEquals(SECURITY_CONTEXT_JSON, actualJson, true);
|
||||
|
||||
+12
-9
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
* Copyright 2015-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.
|
||||
@@ -71,7 +71,8 @@ public class UsernamePasswordAuthenticationTokenMixinTests extends AbstractMixin
|
||||
@Test
|
||||
public void serializeUnauthenticatedUsernamePasswordAuthenticationTokenMixinTest()
|
||||
throws JsonProcessingException, JSONException {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("admin", "1234");
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("admin",
|
||||
"1234");
|
||||
String serializedJson = this.mapper.writeValueAsString(token);
|
||||
JSONAssert.assertEquals(UNAUTHENTICATED_STRINGPRINCIPAL_JSON, serializedJson, true);
|
||||
}
|
||||
@@ -80,8 +81,8 @@ public class UsernamePasswordAuthenticationTokenMixinTests extends AbstractMixin
|
||||
public void serializeAuthenticatedUsernamePasswordAuthenticationTokenMixinTest()
|
||||
throws JsonProcessingException, JSONException {
|
||||
User user = createDefaultUser();
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getUsername(),
|
||||
user.getPassword(), user.getAuthorities());
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken
|
||||
.authenticated(user.getUsername(), user.getPassword(), user.getAuthorities());
|
||||
String serializedJson = this.mapper.writeValueAsString(token);
|
||||
JSONAssert.assertEquals(AUTHENTICATED_STRINGPRINCIPAL_JSON, serializedJson, true);
|
||||
}
|
||||
@@ -140,7 +141,7 @@ public class UsernamePasswordAuthenticationTokenMixinTests extends AbstractMixin
|
||||
throws JsonProcessingException, JSONException {
|
||||
NonUserPrincipal principal = new NonUserPrincipal();
|
||||
principal.setUsername("admin");
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(principal, null,
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.authenticated(principal, null,
|
||||
new ArrayList<>());
|
||||
String actualJson = this.mapper.writeValueAsString(token);
|
||||
JSONAssert.assertEquals(AUTHENTICATED_NON_USER_PRINCIPAL_JSON, actualJson, true);
|
||||
@@ -170,7 +171,8 @@ public class UsernamePasswordAuthenticationTokenMixinTests extends AbstractMixin
|
||||
|
||||
@Test
|
||||
public void serializingThenDeserializingWithNoCredentialsOrDetailsShouldWork() throws IOException {
|
||||
UsernamePasswordAuthenticationToken original = new UsernamePasswordAuthenticationToken("Frodo", null);
|
||||
UsernamePasswordAuthenticationToken original = UsernamePasswordAuthenticationToken.unauthenticated("Frodo",
|
||||
null);
|
||||
String serialized = this.mapper.writeValueAsString(original);
|
||||
UsernamePasswordAuthenticationToken deserialized = this.mapper.readValue(serialized,
|
||||
UsernamePasswordAuthenticationToken.class);
|
||||
@@ -181,7 +183,8 @@ public class UsernamePasswordAuthenticationTokenMixinTests extends AbstractMixin
|
||||
public void serializingThenDeserializingWithConfiguredObjectMapperShouldWork() throws IOException {
|
||||
this.mapper.setDefaultPropertyInclusion(Value.construct(Include.ALWAYS, Include.NON_NULL))
|
||||
.setSerializationInclusion(Include.NON_ABSENT);
|
||||
UsernamePasswordAuthenticationToken original = new UsernamePasswordAuthenticationToken("Frodo", null);
|
||||
UsernamePasswordAuthenticationToken original = UsernamePasswordAuthenticationToken.unauthenticated("Frodo",
|
||||
null);
|
||||
String serialized = this.mapper.writeValueAsString(original);
|
||||
UsernamePasswordAuthenticationToken deserialized = this.mapper.readValue(serialized,
|
||||
UsernamePasswordAuthenticationToken.class);
|
||||
@@ -190,8 +193,8 @@ public class UsernamePasswordAuthenticationTokenMixinTests extends AbstractMixin
|
||||
|
||||
private UsernamePasswordAuthenticationToken createToken() {
|
||||
User user = createDefaultUser();
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user, user.getPassword(),
|
||||
user.getAuthorities());
|
||||
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.authenticated(user,
|
||||
user.getPassword(), user.getAuthorities());
|
||||
return token;
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -344,14 +344,14 @@ public class JdbcUserDetailsManagerTests {
|
||||
@Test
|
||||
public void createNewAuthenticationUsesNullPasswordToKeepPassordsSave() {
|
||||
insertJoe();
|
||||
UsernamePasswordAuthenticationToken currentAuth = new UsernamePasswordAuthenticationToken("joe", null,
|
||||
UsernamePasswordAuthenticationToken currentAuth = UsernamePasswordAuthenticationToken.authenticated("joe", null,
|
||||
AuthorityUtils.createAuthorityList("ROLE_USER"));
|
||||
Authentication updatedAuth = this.manager.createNewAuthentication(currentAuth, "new");
|
||||
assertThat(updatedAuth.getCredentials()).isNull();
|
||||
}
|
||||
|
||||
private Authentication authenticateJoe() {
|
||||
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken("joe", "password",
|
||||
UsernamePasswordAuthenticationToken auth = UsernamePasswordAuthenticationToken.authenticated("joe", "password",
|
||||
joe.getAuthorities());
|
||||
SecurityContextHolder.getContext().setAuthentication(auth);
|
||||
return auth;
|
||||
|
||||
Reference in New Issue
Block a user