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

net.unicon.cas.mfa.authentication.RegexAuthenticationMethodTranslator Maven / Gradle / Ivy

Go to download

This module is intended to include all the Java you need to add to a CAS implementation to take advantage of the extended multifactor authentication features in this project.

There is a newer version: 2.0.0-RC3
Show newest version
package net.unicon.cas.mfa.authentication;

import net.unicon.cas.mfa.web.support.UnrecognizedAuthenticationMethodException;
import org.jasig.cas.authentication.principal.WebApplicationService;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * A translator that will check a list of regex patterns and return an authentication method name.
 *
 * @author John Gasper
 */
public class RegexAuthenticationMethodTranslator implements AuthenticationMethodTranslator {
    private final Map translationMap;

    private String defaultMfaMethod = null;

    /**
     * Instantiates a new Regex authentication method translator.
     *
     * @param translationMap the regex/mfa method translation map (maybe an ordered map)
     */
    public RegexAuthenticationMethodTranslator(final Map translationMap) {
        this(translationMap, null);
    }

    /**
     * Instantiates a new Regex authentication method translator.
     *
     * @param translationMap the regex/mfa method translation map (maybe an ordered map)
     * @param defaultMfaMethod the default MFA merhod to use if no match is found.
     */
    public RegexAuthenticationMethodTranslator(final Map translationMap, final String defaultMfaMethod) {
        this.defaultMfaMethod = defaultMfaMethod;

        final Map optimizedMap = new LinkedHashMap();

        for (final String pattern : translationMap.keySet()) {
            optimizedMap.put(Pattern.compile(pattern), translationMap.get(pattern));
        }

        this.translationMap = optimizedMap;
    }

    @Override
    public String translate(final WebApplicationService targetService, final String triggerValue) {
        for (final Pattern pattern : translationMap.keySet()) {
            if (pattern.matcher(triggerValue).matches()) {
                return this.translationMap.get(pattern);
            }
        }

        if (this.defaultMfaMethod != null) {
            return defaultMfaMethod;
        }

        throw new UnrecognizedAuthenticationMethodException(triggerValue, targetService.getId());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy