All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apereo.cas.jdbc.QueryAndEncodeDatabasePasswordEncoder Maven / Gradle / Ivy

The newest version!
package org.apereo.cas.jdbc;

import org.apereo.cas.configuration.model.support.jdbc.authn.QueryEncodeJdbcAuthenticationProperties;
import org.apereo.cas.util.DigestUtils;
import org.apereo.cas.util.function.FunctionUtils;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.apache.commons.lang3.ArrayUtils;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * This is {@link QueryAndEncodeDatabasePasswordEncoder}.
 *
 * @author Misagh Moayyed
 * @since 7.0.0
 */
@RequiredArgsConstructor
public class QueryAndEncodeDatabasePasswordEncoder implements DatabasePasswordEncoder {
    protected final QueryEncodeJdbcAuthenticationProperties properties;

    @Override
    public String encode(final String password, final Map queryValues) {
        val iterations = getIterations(queryValues);
        val dynaSalt = getDynamicSalt(queryValues);
        val staticSalt = getStaticSalt(queryValues);
        return DigestUtils.rawDigest(properties.getAlgorithmName(), staticSalt, dynaSalt, password, iterations);
    }

    protected int getIterations(final Map queryValues) {
        var iterations = properties.getNumberOfIterations();
        if (queryValues.containsKey(properties.getNumberOfIterationsFieldName())) {
            val longAsStr = queryValues.get(properties.getNumberOfIterationsFieldName()).toString();
            iterations = Integer.parseInt(longAsStr);
        }
        return iterations;
    }

    protected byte[] getStaticSalt(final Map queryValues) {
        return FunctionUtils.doIfNotBlank(properties.getStaticSalt(),
            () -> properties.getStaticSalt().getBytes(StandardCharsets.UTF_8),
            () -> ArrayUtils.EMPTY_BYTE_ARRAY);
    }

    protected byte[] getDynamicSalt(final Map queryValues) {
        return queryValues.containsKey(properties.getSaltFieldName())
            ? queryValues.get(properties.getSaltFieldName()).toString().getBytes(StandardCharsets.UTF_8)
            : ArrayUtils.EMPTY_BYTE_ARRAY;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy