@Override
public OptionMap getOptionMap() {
List<String> mechanisms = new LinkedList<String>();
Set<Property> properties = new HashSet<Property>();
Builder builder = OptionMap.builder();
if (realm == null) {
mechanisms.add(ANONYMOUS);
builder.set(SASL_POLICY_NOANONYMOUS, false);
builder.set(SSL_ENABLED, false);
} else {
Set<AuthenticationMechanism> authMechs = realm.getSupportedAuthenticationMechanisms();
if (authMechs.contains(AuthenticationMechanism.LOCAL)) {
mechanisms.add(JBOSS_LOCAL_USER);
Map<String, String> mechConfig = realm.getMechanismConfig(AuthenticationMechanism.LOCAL);
if (mechConfig.containsKey(LOCAL_DEFAULT_USER)) {
properties.add(Property.of(SASL_OPT_LOCAL_DEFAULT_USER, mechConfig.get(LOCAL_DEFAULT_USER)));
}
if (tokensDir != null) {
properties.add(Property.of(SASL_OPT_LOCAL_USER_CHALLENGE_PATH, tokensDir));
}
}
if (authMechs.contains(AuthenticationMechanism.DIGEST)) {
mechanisms.add(DIGEST_MD5);
properties.add(Property.of(SASL_OPT_REALM_PROPERTY, realm.getName()));
Map<String, String> mechConfig = realm.getMechanismConfig(AuthenticationMechanism.DIGEST);
boolean plainTextDigest = true;
if (mechConfig.containsKey(DIGEST_PLAIN_TEXT)) {
plainTextDigest = Boolean.parseBoolean(mechConfig.get(DIGEST_PLAIN_TEXT));
}
if (plainTextDigest == false) {
properties.add(Property.of(SASL_OPT_PRE_DIGESTED_PROPERTY, Boolean.TRUE.toString()));
}
}
if (authMechs.contains(AuthenticationMechanism.PLAIN)) {
mechanisms.add(PLAIN);
builder.set(SASL_POLICY_NOPLAINTEXT, false);
}
if (realm.getSSLContext() == null) {
builder.set(SSL_ENABLED, false);
} else {
if (authMechs.contains(AuthenticationMechanism.CLIENT_CERT)) {
builder.set(SSL_ENABLED, true);
builder.set(SSL_STARTTLS, true);
mechanisms.add(0, EXTERNAL);
// TODO - If no other mechanisms are available we can use REQUIRED.
builder.set(SSL_CLIENT_AUTH_MODE, REQUESTED);
} else {
builder.set(SSL_ENABLED, true);
builder.set(SSL_STARTTLS, true);
}
}
}
if (mechanisms.size() == 0) {
throw MESSAGES.noSupportingMechanismsForRealm();
}
builder.set(SASL_MECHANISMS, Sequence.of(mechanisms));
builder.set(SASL_PROPERTIES, Sequence.of(properties));
return builder.getMap();
}