|
| 1 | +package org.rest.sec.security; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Set; |
| 5 | + |
| 6 | +import org.rest.common.security.SpringSecurityPrincipal; |
| 7 | +import org.rest.sec.client.template.auth.AuthenticationRestTemplate; |
| 8 | +import org.rest.sec.model.Role; |
| 9 | +import org.rest.sec.model.dto.User; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 12 | +import org.springframework.context.annotation.Profile; |
| 13 | +import org.springframework.http.ResponseEntity; |
| 14 | +import org.springframework.security.authentication.AuthenticationProvider; |
| 15 | +import org.springframework.security.authentication.AuthenticationServiceException; |
| 16 | +import org.springframework.security.authentication.BadCredentialsException; |
| 17 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 18 | +import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider; |
| 19 | +import org.springframework.security.authentication.dao.SaltSource; |
| 20 | +import org.springframework.security.authentication.encoding.PasswordEncoder; |
| 21 | +import org.springframework.security.authentication.encoding.PlaintextPasswordEncoder; |
| 22 | +import org.springframework.security.core.AuthenticationException; |
| 23 | +import org.springframework.security.core.GrantedAuthority; |
| 24 | +import org.springframework.security.core.authority.AuthorityUtils; |
| 25 | +import org.springframework.security.core.userdetails.UserDetails; |
| 26 | +import org.springframework.security.core.userdetails.UserDetailsService; |
| 27 | +import org.springframework.stereotype.Component; |
| 28 | +import org.springframework.util.Assert; |
| 29 | + |
| 30 | +import com.google.common.base.Functions; |
| 31 | +import com.google.common.collect.Collections2; |
| 32 | +import com.google.common.collect.Lists; |
| 33 | +import com.google.common.collect.Sets; |
| 34 | + |
| 35 | +/** |
| 36 | + * - note: the original is DaoAuthenticationProvider <br/> |
| 37 | + * An {@link AuthenticationProvider} implementation that retrieves user details from a {@link UserDetailsService}. |
| 38 | + */ |
| 39 | +@Component |
| 40 | +@Profile("client") |
| 41 | +public class RestAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider { |
| 42 | + |
| 43 | + @Autowired |
| 44 | + @Qualifier("authenticationRestTemplate") |
| 45 | + private AuthenticationRestTemplate authenticationApi; |
| 46 | + |
| 47 | + // ~ Instance fields ================================================================================================ |
| 48 | + |
| 49 | + private PasswordEncoder passwordEncoder = new PlaintextPasswordEncoder(); |
| 50 | + |
| 51 | + private SaltSource saltSource; |
| 52 | + |
| 53 | + public RestAuthenticationProvider() { |
| 54 | + super(); |
| 55 | + } |
| 56 | + |
| 57 | + // ~ Methods ======================================================================================================== |
| 58 | + |
| 59 | + @Override |
| 60 | + @SuppressWarnings("deprecation") |
| 61 | + protected void additionalAuthenticationChecks(final UserDetails userDetails, final UsernamePasswordAuthenticationToken authentication) throws AuthenticationException { |
| 62 | + Object salt = null; |
| 63 | + |
| 64 | + if (saltSource != null) { |
| 65 | + salt = saltSource.getSalt(userDetails); |
| 66 | + } |
| 67 | + |
| 68 | + if (authentication.getCredentials() == null) { |
| 69 | + logger.debug("Authentication failed: no credentials provided"); |
| 70 | + |
| 71 | + throw new BadCredentialsException(messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"), userDetails); |
| 72 | + } |
| 73 | + |
| 74 | + final String presentedPassword = authentication.getCredentials().toString(); |
| 75 | + |
| 76 | + if (!passwordEncoder.isPasswordValid(userDetails.getPassword(), presentedPassword, salt)) { |
| 77 | + logger.debug("Authentication failed: password does not match stored value"); |
| 78 | + |
| 79 | + throw new BadCredentialsException(messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"), userDetails); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected final UserDetails retrieveUser(final String name, final UsernamePasswordAuthenticationToken authentication) { |
| 85 | + final String password = authentication.getCredentials().toString(); |
| 86 | + |
| 87 | + UserDetails loadedUser = null; |
| 88 | + try { |
| 89 | + final ResponseEntity<User> authenticationResponse = authenticationApi.authenticate(name, password); |
| 90 | + if (authenticationResponse.getStatusCode().value() == 401) { |
| 91 | + // temporary - the idea here is to generate the not authorized exception - not by hand, but by returning wrong credentials which in turn will be refused later |
| 92 | + return new org.springframework.security.core.userdetails.User("wrongUsername", "wrongPass", Lists.<GrantedAuthority> newArrayList()); |
| 93 | + } |
| 94 | + |
| 95 | + final User principalFromRest = authenticationResponse.getBody(); |
| 96 | + |
| 97 | + final Set<String> privilegesFromRest = Sets.newHashSet(); |
| 98 | + final Set<Role> roles = principalFromRest.getRoles(); |
| 99 | + for (final Role role : roles) { |
| 100 | + privilegesFromRest.addAll(Collections2.transform(role.getPrivileges(), Functions.toStringFunction())); |
| 101 | + } |
| 102 | + final String[] authorityStringsAsArray = privilegesFromRest.toArray(new String[privilegesFromRest.size()]); |
| 103 | + final List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList(authorityStringsAsArray); |
| 104 | + |
| 105 | + loadedUser = new SpringSecurityPrincipal(name, password, true, authorities, "principalFromRest.getUuid()"); |
| 106 | + } catch (final Exception repositoryProblem) { |
| 107 | + throw new AuthenticationServiceException(repositoryProblem.getMessage(), repositoryProblem); |
| 108 | + } |
| 109 | + |
| 110 | + return loadedUser; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Sets the PasswordEncoder instance to be used to encode and validate passwords. If not set, the password will be compared as plain text. |
| 115 | + * <p/> |
| 116 | + * For systems which are already using salted password which are encoded with a previous release, the encoder should be of type {@code org.springframework.security.authentication.encoding.PasswordEncoder}. Otherwise, the recommended |
| 117 | + * approach is to use {@code org.springframework.security.crypto.password.PasswordEncoder}. |
| 118 | + * |
| 119 | + * @param passwordEncoderToSet |
| 120 | + * must be an instance of one of the {@code PasswordEncoder} types. |
| 121 | + */ |
| 122 | + public void setPasswordEncoder(final Object passwordEncoderToSet) { |
| 123 | + Assert.notNull(passwordEncoderToSet, "passwordEncoder cannot be null"); |
| 124 | + |
| 125 | + if (passwordEncoderToSet instanceof PasswordEncoder) { |
| 126 | + passwordEncoder = (PasswordEncoder) passwordEncoderToSet; |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + if (passwordEncoderToSet instanceof org.springframework.security.crypto.password.PasswordEncoder) { |
| 131 | + final org.springframework.security.crypto.password.PasswordEncoder delegate = (org.springframework.security.crypto.password.PasswordEncoder) passwordEncoderToSet; |
| 132 | + passwordEncoder = new PasswordEncoder() { |
| 133 | + @Override |
| 134 | + public String encodePassword(final String rawPass, final Object salt) { |
| 135 | + checkSalt(salt); |
| 136 | + return delegate.encode(rawPass); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public boolean isPasswordValid(final String encPass, final String rawPass, final Object salt) { |
| 141 | + checkSalt(salt); |
| 142 | + return delegate.matches(rawPass, encPass); |
| 143 | + } |
| 144 | + |
| 145 | + private void checkSalt(final Object salt) { |
| 146 | + Assert.isNull(salt, "Salt value must be null when used with crypto module PasswordEncoder"); |
| 147 | + } |
| 148 | + }; |
| 149 | + |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + throw new IllegalArgumentException("passwordEncoder must be a PasswordEncoder instance"); |
| 154 | + } |
| 155 | + |
| 156 | + protected PasswordEncoder getPasswordEncoder() { |
| 157 | + return passwordEncoder; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * The source of salts to use when decoding passwords. <code>null</code> is a valid value, meaning the <code>DaoAuthenticationProvider</code> will present <code>null</code> to the relevant <code>PasswordEncoder</code>. |
| 162 | + * <p/> |
| 163 | + * Instead, it is recommended that you use an encoder which uses a random salt and combines it with the password field. This is the default approach taken in the {@code org.springframework.security.crypto.password} package. |
| 164 | + * |
| 165 | + * @param saltSourceToSet |
| 166 | + * to use when attempting to decode passwords via the <code>PasswordEncoder</code> |
| 167 | + */ |
| 168 | + public void setSaltSource(final SaltSource saltSourceToSet) { |
| 169 | + saltSource = saltSourceToSet; |
| 170 | + } |
| 171 | + |
| 172 | + protected SaltSource getSaltSource() { |
| 173 | + return saltSource; |
| 174 | + } |
| 175 | + |
| 176 | +} |
0 commit comments