net.unicon.cas.mfa.authentication.JsonBackedAuthenticationMethodConfigurationProvider 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 com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.FileUtils;
import org.springframework.core.io.Resource;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
/**
* Loads authentication methods and their rank from an external configuration file
* that is expected to be JSON. The ranking of authentication methods is
* determined by the {@link RequestedAuthenticationMethodRankingStrategy}.
*
* Example configuration:
*
[ {
"rank" : 1,
"name" : "duo_two_factor"
}, {
"rank" : 2,
"name" : "strong_two_factor"
}, {
"rank" : 3,
"name" : "sample_two_factor"
} ]
*
* @author Misagh Moayyed
*/
public final class JsonBackedAuthenticationMethodConfigurationProvider implements AuthenticationMethodConfigurationProvider {
private final Set authnMethods;
private final ObjectMapper objectMapper = new ObjectMapper();
/**
* Instantiates a new Authentication method loader.
* Loads supported authentication methods from
* the specified resource.
* @param configuration the configuration
* @throws IOException the iO exception
*/
public JsonBackedAuthenticationMethodConfigurationProvider(final Resource configuration) throws IOException {
this.authnMethods = new TreeSet();
final String json = FileUtils.readFileToString(configuration.getFile());
final Set> set = this.objectMapper.readValue(json, Set.class);
for (final Iterator> it = set.iterator(); it.hasNext();) {
final AuthenticationMethod method = this.objectMapper.convertValue(it.next(), AuthenticationMethod.class);
this.authnMethods.add(method);
}
}
/**
* Instantiates a new Authentication method loader.
* Populates the supported authn methods with the given set.
*
* @param authnMethods the authn methods
*/
public JsonBackedAuthenticationMethodConfigurationProvider(final Set authnMethods) {
this.authnMethods = authnMethods;
}
/**
* Instantiates a new Authentication method loader.
*/
public JsonBackedAuthenticationMethodConfigurationProvider() {
this.authnMethods = new TreeSet();
}
/** {@inheritDoc} **/
@Override
public boolean containsAuthenticationMethod(final String name) {
return getAuthenticationMethod(name) != null;
}
/** {@inheritDoc} **/
@Override
public AuthenticationMethod getAuthenticationMethod(final String name) {
for (final Iterator it = this.authnMethods.iterator(); it.hasNext();) {
final AuthenticationMethod f = it.next();
if (f.getName().equals(name)) {
return f;
}
}
return null;
}
}