|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.authentication; |
| 18 | + |
| 19 | +import org.apache.commons.logging.Log; |
| 20 | +import org.apache.commons.logging.LogFactory; |
| 21 | +import reactor.core.publisher.Mono; |
| 22 | +import reactor.core.scheduler.Scheduler; |
| 23 | +import reactor.core.scheduler.Schedulers; |
| 24 | + |
| 25 | +import org.springframework.context.support.MessageSourceAccessor; |
| 26 | +import org.springframework.security.core.Authentication; |
| 27 | +import org.springframework.security.core.SpringSecurityMessageSource; |
| 28 | +import org.springframework.security.core.userdetails.ReactiveUserDetailsPasswordService; |
| 29 | +import org.springframework.security.core.userdetails.UserDetails; |
| 30 | +import org.springframework.security.core.userdetails.UserDetailsChecker; |
| 31 | +import org.springframework.security.crypto.factory.PasswordEncoderFactories; |
| 32 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 33 | +import org.springframework.util.Assert; |
| 34 | + |
| 35 | +/** |
| 36 | + * A base {@link ReactiveAuthenticationManager} that allows subclasses to override and work with |
| 37 | + * {@link UserDetails} objects. |
| 38 | + * |
| 39 | + * <p> |
| 40 | + * Upon successful validation, a <code>UsernamePasswordAuthenticationToken</code> will be |
| 41 | + * created and returned to the caller. The token will include as its principal either a |
| 42 | + * <code>String</code> representation of the username, or the {@link UserDetails} that was |
| 43 | + * returned from the authentication repository. |
| 44 | + * |
| 45 | + * @author Eddú Meléndez |
| 46 | + * @since 5.2 |
| 47 | + */ |
| 48 | +public abstract class AbstractUserDetailsReactiveAuthenticationManager implements ReactiveAuthenticationManager { |
| 49 | + |
| 50 | + protected final Log logger = LogFactory.getLog(getClass()); |
| 51 | + |
| 52 | + protected MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor(); |
| 53 | + |
| 54 | + private PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder(); |
| 55 | + |
| 56 | + private ReactiveUserDetailsPasswordService userDetailsPasswordService; |
| 57 | + |
| 58 | + private Scheduler scheduler = Schedulers.parallel(); |
| 59 | + |
| 60 | + private UserDetailsChecker preAuthenticationChecks = user -> { |
| 61 | + if (!user.isAccountNonLocked()) { |
| 62 | + logger.debug("User account is locked"); |
| 63 | + |
| 64 | + throw new LockedException(this.messages.getMessage( |
| 65 | + "AbstractUserDetailsAuthenticationProvider.locked", |
| 66 | + "User account is locked")); |
| 67 | + } |
| 68 | + |
| 69 | + if (!user.isEnabled()) { |
| 70 | + logger.debug("User account is disabled"); |
| 71 | + |
| 72 | + throw new DisabledException(this.messages.getMessage( |
| 73 | + "AbstractUserDetailsAuthenticationProvider.disabled", |
| 74 | + "User is disabled")); |
| 75 | + } |
| 76 | + |
| 77 | + if (!user.isAccountNonExpired()) { |
| 78 | + logger.debug("User account is expired"); |
| 79 | + |
| 80 | + throw new AccountExpiredException(this.messages.getMessage( |
| 81 | + "AbstractUserDetailsAuthenticationProvider.expired", |
| 82 | + "User account has expired")); |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + private UserDetailsChecker postAuthenticationChecks = user -> { |
| 87 | + if (!user.isCredentialsNonExpired()) { |
| 88 | + logger.debug("User account credentials have expired"); |
| 89 | + |
| 90 | + throw new CredentialsExpiredException(this.messages.getMessage( |
| 91 | + "AbstractUserDetailsAuthenticationProvider.credentialsExpired", |
| 92 | + "User credentials have expired")); |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + @Override |
| 97 | + public Mono<Authentication> authenticate(Authentication authentication) { |
| 98 | + final String username = authentication.getName(); |
| 99 | + final String presentedPassword = (String) authentication.getCredentials(); |
| 100 | + return retrieveUser(username) |
| 101 | + .doOnNext(this.preAuthenticationChecks::check) |
| 102 | + .publishOn(this.scheduler) |
| 103 | + .filter(u -> this.passwordEncoder.matches(presentedPassword, u.getPassword())) |
| 104 | + .switchIfEmpty(Mono.defer(() -> Mono.error(new BadCredentialsException("Invalid Credentials")))) |
| 105 | + .flatMap(u -> { |
| 106 | + boolean upgradeEncoding = this.userDetailsPasswordService != null |
| 107 | + && this.passwordEncoder.upgradeEncoding(u.getPassword()); |
| 108 | + if (upgradeEncoding) { |
| 109 | + String newPassword = this.passwordEncoder.encode(presentedPassword); |
| 110 | + return this.userDetailsPasswordService.updatePassword(u, newPassword); |
| 111 | + } |
| 112 | + return Mono.just(u); |
| 113 | + }) |
| 114 | + .doOnNext(this.postAuthenticationChecks::check) |
| 115 | + .map(u -> new UsernamePasswordAuthenticationToken(u, u.getPassword(), u.getAuthorities()) ); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * The {@link PasswordEncoder} that is used for validating the password. The default is |
| 120 | + * {@link PasswordEncoderFactories#createDelegatingPasswordEncoder()} |
| 121 | + * @param passwordEncoder the {@link PasswordEncoder} to use. Cannot be null |
| 122 | + */ |
| 123 | + public void setPasswordEncoder(PasswordEncoder passwordEncoder) { |
| 124 | + Assert.notNull(passwordEncoder, "passwordEncoder cannot be null"); |
| 125 | + this.passwordEncoder = passwordEncoder; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Sets the {@link Scheduler} used by the {@link UserDetailsRepositoryReactiveAuthenticationManager}. |
| 130 | + * The default is {@code Schedulers.parallel()} because modern password encoding is |
| 131 | + * a CPU intensive task that is non blocking. This means validation is bounded by the |
| 132 | + * number of CPUs. Some applications may want to customize the {@link Scheduler}. For |
| 133 | + * example, if users are stuck using the insecure {@link org.springframework.security.crypto.password.NoOpPasswordEncoder} |
| 134 | + * they might want to leverage {@code Schedulers.immediate()}. |
| 135 | + * |
| 136 | + * @param scheduler the {@link Scheduler} to use. Cannot be null. |
| 137 | + * @since 5.0.6 |
| 138 | + */ |
| 139 | + public void setScheduler(Scheduler scheduler) { |
| 140 | + Assert.notNull(scheduler, "scheduler cannot be null"); |
| 141 | + this.scheduler = scheduler; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Sets the service to use for upgrading passwords on successful authentication. |
| 146 | + * @param userDetailsPasswordService the service to use |
| 147 | + */ |
| 148 | + public void setUserDetailsPasswordService( |
| 149 | + ReactiveUserDetailsPasswordService userDetailsPasswordService) { |
| 150 | + this.userDetailsPasswordService = userDetailsPasswordService; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Sets the strategy which will be used to validate the loaded <tt>UserDetails</tt> |
| 155 | + * object after authentication occurs. |
| 156 | + * |
| 157 | + * @param postAuthenticationChecks The {@link UserDetailsChecker} |
| 158 | + * @since 5.2 |
| 159 | + */ |
| 160 | + public void setPostAuthenticationChecks(UserDetailsChecker postAuthenticationChecks) { |
| 161 | + Assert.notNull(this.postAuthenticationChecks, "postAuthenticationChecks cannot be null"); |
| 162 | + this.postAuthenticationChecks = postAuthenticationChecks; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Allows subclasses to retrieve the <code>UserDetails</code> |
| 167 | + * from an implementation-specific location. |
| 168 | + * |
| 169 | + * @param username The username to retrieve |
| 170 | + * @return the user information. If authentication fails, a Mono error is returned. |
| 171 | + */ |
| 172 | + protected abstract Mono<UserDetails> retrieveUser(String username); |
| 173 | + |
| 174 | +} |
0 commit comments