net.unicon.cas.mfa.authentication.RegexAuthenticationMethodTranslator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cas-mfa-java Show documentation
Show all versions of cas-mfa-java Show documentation
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.
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());
}
}