Search in sources :

Example 1 with PasswordEncoderProperties

use of org.apereo.cas.configuration.model.core.authentication.PasswordEncoderProperties in project cas by apereo.

the class Beans method newPasswordEncoder.

/**
     * New password encoder password encoder.
     *
     * @param properties the properties
     * @return the password encoder
     */
public static PasswordEncoder newPasswordEncoder(final PasswordEncoderProperties properties) {
    final String type = properties.getType();
    if (StringUtils.isBlank(type)) {
        LOGGER.debug("No password encoder type is defined, and so none shall be created");
        return NoOpPasswordEncoder.getInstance();
    }
    if (type.contains(".")) {
        try {
            LOGGER.debug("Configuration indicates use of a custom password encoder [{}]", type);
            final Class<PasswordEncoder> clazz = (Class<PasswordEncoder>) Class.forName(type);
            return clazz.newInstance();
        } catch (final Exception e) {
            LOGGER.error("Falling back to a no-op password encoder as CAS has failed to create " + "an instance of the custom password encoder class " + type, e);
            return NoOpPasswordEncoder.getInstance();
        }
    }
    final PasswordEncoderProperties.PasswordEncoderTypes encoderType = PasswordEncoderProperties.PasswordEncoderTypes.valueOf(type);
    switch(encoderType) {
        case DEFAULT:
            LOGGER.debug("Creating default password encoder with encoding alg [{}] and character encoding [{}]", properties.getEncodingAlgorithm(), properties.getCharacterEncoding());
            return new DefaultPasswordEncoder(properties.getEncodingAlgorithm(), properties.getCharacterEncoding());
        case STANDARD:
            LOGGER.debug("Creating standard password encoder with the secret defined in the configuration");
            return new StandardPasswordEncoder(properties.getSecret());
        case BCRYPT:
            LOGGER.debug("Creating BCRYPT password encoder given the strength [{}] and secret in the configuration", properties.getStrength());
            if (StringUtils.isBlank(properties.getSecret())) {
                LOGGER.debug("Creating BCRYPT encoder without secret");
                return new BCryptPasswordEncoder(properties.getStrength());
            }
            LOGGER.debug("Creating BCRYPT encoder with secret");
            return new BCryptPasswordEncoder(properties.getStrength(), new SecureRandom(properties.getSecret().getBytes(StandardCharsets.UTF_8)));
        case SCRYPT:
            LOGGER.debug("Creating SCRYPT encoder");
            return new SCryptPasswordEncoder();
        case PBKDF2:
            if (StringUtils.isBlank(properties.getSecret())) {
                LOGGER.debug("Creating PBKDF2 encoder without secret");
                return new Pbkdf2PasswordEncoder();
            }
            final int hashWidth = 256;
            return new Pbkdf2PasswordEncoder(properties.getSecret(), properties.getStrength(), hashWidth);
        case NONE:
        default:
            LOGGER.debug("No password encoder shall be created given the requested encoder type [{}]", type);
            return NoOpPasswordEncoder.getInstance();
    }
}
Also used : StandardPasswordEncoder(org.springframework.security.crypto.password.StandardPasswordEncoder) DefaultPasswordEncoder(org.apereo.cas.util.crypto.DefaultPasswordEncoder) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) StandardPasswordEncoder(org.springframework.security.crypto.password.StandardPasswordEncoder) Pbkdf2PasswordEncoder(org.springframework.security.crypto.password.Pbkdf2PasswordEncoder) NoOpPasswordEncoder(org.springframework.security.crypto.password.NoOpPasswordEncoder) SCryptPasswordEncoder(org.springframework.security.crypto.scrypt.SCryptPasswordEncoder) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) SecureRandom(java.security.SecureRandom) Pbkdf2PasswordEncoder(org.springframework.security.crypto.password.Pbkdf2PasswordEncoder) BeanCreationException(org.springframework.beans.factory.BeanCreationException) SCryptPasswordEncoder(org.springframework.security.crypto.scrypt.SCryptPasswordEncoder) PasswordEncoderProperties(org.apereo.cas.configuration.model.core.authentication.PasswordEncoderProperties) DefaultPasswordEncoder(org.apereo.cas.util.crypto.DefaultPasswordEncoder) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder)

Example 2 with PasswordEncoderProperties

use of org.apereo.cas.configuration.model.core.authentication.PasswordEncoderProperties in project cas by apereo.

the class FileAuthenticationHandlerTests method setUp.

@Before
public void setUp() {
    this.authenticationHandler = new FileAuthenticationHandler("", null, null, new ClassPathResource("authentication.txt"), FileAuthenticationHandler.DEFAULT_SEPARATOR);
    final PasswordEncoderProperties p = new PasswordEncoderProperties();
    p.setType(PasswordEncoderProperties.PasswordEncoderTypes.DEFAULT.name());
    p.setEncodingAlgorithm("MD5");
    p.setCharacterEncoding("UTF-8");
    this.authenticationHandler.setPasswordEncoder(PasswordEncoderUtils.newPasswordEncoder(p));
}
Also used : PasswordEncoderProperties(org.apereo.cas.configuration.model.core.authentication.PasswordEncoderProperties) ClassPathResource(org.springframework.core.io.ClassPathResource) Before(org.junit.Before)

Example 3 with PasswordEncoderProperties

use of org.apereo.cas.configuration.model.core.authentication.PasswordEncoderProperties in project cas by apereo.

the class FileAuthenticationHandlerTests method initialize.

@BeforeEach
public void initialize() {
    this.authenticationHandler = new FileAuthenticationHandler(StringUtils.EMPTY, null, null, new ClassPathResource("authentication.txt"), FileAuthenticationHandler.DEFAULT_SEPARATOR);
    val p = new PasswordEncoderProperties();
    p.setType(PasswordEncoderProperties.PasswordEncoderTypes.DEFAULT.name());
    p.setEncodingAlgorithm("MD5");
    p.setCharacterEncoding("UTF-8");
    this.authenticationHandler.setPasswordEncoder(PasswordEncoderUtils.newPasswordEncoder(p, mock(ApplicationContext.class)));
}
Also used : lombok.val(lombok.val) PasswordEncoderProperties(org.apereo.cas.configuration.model.core.authentication.PasswordEncoderProperties) ClassPathResource(org.springframework.core.io.ClassPathResource) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with PasswordEncoderProperties

use of org.apereo.cas.configuration.model.core.authentication.PasswordEncoderProperties in project cas by apereo.

the class DefaultPasswordEncoderTests method verifyPasswordEncoderByMD5.

@Test
public void verifyPasswordEncoderByMD5() {
    val p = new PasswordEncoderProperties();
    p.setType(PasswordEncoderProperties.PasswordEncoderTypes.DEFAULT.name());
    p.setEncodingAlgorithm("MD5");
    p.setCharacterEncoding("UTF-8");
    val e = PasswordEncoderUtils.newPasswordEncoder(p, mock(ApplicationContext.class));
    assertTrue(e.matches("asd123", "bfd59291e825b5f2bbf1eb76569f8fe7"));
}
Also used : lombok.val(lombok.val) ApplicationContext(org.springframework.context.ApplicationContext) PasswordEncoderProperties(org.apereo.cas.configuration.model.core.authentication.PasswordEncoderProperties) Test(org.junit.jupiter.api.Test)

Example 5 with PasswordEncoderProperties

use of org.apereo.cas.configuration.model.core.authentication.PasswordEncoderProperties in project cas by apereo.

the class PasswordEncoderUtilsTests method verifyNoType.

@Test
public void verifyNoType() {
    val applicationContext = new StaticApplicationContext();
    applicationContext.refresh();
    val properties = new PasswordEncoderProperties();
    properties.setType(null);
    var encoder = PasswordEncoderUtils.newPasswordEncoder(properties, applicationContext);
    assertNotNull(encoder);
    properties.setType(StringUtils.EMPTY);
    encoder = PasswordEncoderUtils.newPasswordEncoder(properties, applicationContext);
    assertNotNull(encoder);
}
Also used : lombok.val(lombok.val) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) PasswordEncoderProperties(org.apereo.cas.configuration.model.core.authentication.PasswordEncoderProperties) Test(org.junit.jupiter.api.Test)

Aggregations

PasswordEncoderProperties (org.apereo.cas.configuration.model.core.authentication.PasswordEncoderProperties)13 lombok.val (lombok.val)11 Test (org.junit.jupiter.api.Test)10 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)6 ApplicationContext (org.springframework.context.ApplicationContext)4 ClassPathResource (org.springframework.core.io.ClassPathResource)2 StandardPasswordEncoder (org.springframework.security.crypto.password.StandardPasswordEncoder)2 SecureRandom (java.security.SecureRandom)1 DefaultPasswordEncoder (org.apereo.cas.util.crypto.DefaultPasswordEncoder)1 Before (org.junit.Before)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 BeanCreationException (org.springframework.beans.factory.BeanCreationException)1 BCryptPasswordEncoder (org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder)1 NoOpPasswordEncoder (org.springframework.security.crypto.password.NoOpPasswordEncoder)1 PasswordEncoder (org.springframework.security.crypto.password.PasswordEncoder)1 Pbkdf2PasswordEncoder (org.springframework.security.crypto.password.Pbkdf2PasswordEncoder)1 SCryptPasswordEncoder (org.springframework.security.crypto.scrypt.SCryptPasswordEncoder)1