package it.mice.voila.runtime.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.userdetails.UserDetailsService;

import it.mice.voila.runtime.security.encoders.PasswordEncoder;

/**
 * This class exists only because the new spring security infrastructure is too protected !!!
 * Overriding all getters to extends its visibility from protected to public to allow more
 * flexible usage.
 */
public class DefaultDaoAuthenticationProvider extends DaoAuthenticationProvider {

	private SaltSource saltSource;
	
	private PasswordEncoder passwordEncoder;
	
    protected void doAfterPropertiesSet() {
    	super.doAfterPropertiesSet();
    }

	@Autowired
	public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
		// TODO Auto-generated method stub
//		super.setPasswordEncoder(passwordEncoder);
		this.passwordEncoder = passwordEncoder;
	}
	
	/**
     * 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>.
     * <p>
     * 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.
     *
     * @param saltSource to use when attempting to decode passwords via the <code>PasswordEncoder</code>
     */
	@Autowired(required=false)
	public void setSaltSource(SaltSource saltSource) {
		this.saltSource = saltSource;
//		super.setSaltSource(saltSource);
	}

	@Autowired
	public void setUserDetailsService(UserDetailsService userDetailsService) {
		// TODO Auto-generated method stub
		super.setUserDetailsService(userDetailsService);
	}

	public PasswordEncoder getPasswordEncoder() {
		// TODO Auto-generated method stub
//		return super.getPasswordEncoder();
		return this.passwordEncoder;
	}

	public SaltSource getSaltSource() {
		return saltSource;
	}

	@Override
	public UserDetailsService getUserDetailsService() {
		// TODO Auto-generated method stub
		return super.getUserDetailsService();
	}

}
