package com.continuuity.security.server;

import com.continuuity.common.conf.CConfiguration;
import com.google.inject.Inject;

import javax.security.auth.login.AppConfigurationEntry;
import javax.security.auth.login.Configuration;
import java.util.HashMap;

/**
 *
 */
public class LDAPAuthenticationHandler extends JAASAuthenticationHandler {
  private final CConfiguration configuration;

  @Inject
  public LDAPAuthenticationHandler(CConfiguration configuration) throws Exception {
    super("ldaploginmodule");
    this.configuration = configuration;
  }

  @Override
  protected Configuration getConfiguration() {
    return new Configuration() {
      @Override
      public AppConfigurationEntry[] getAppConfigurationEntry(String s) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("contextFactory", "com.sun.jndi.ldap.LdapCtxFactory");
        map.put("authenticationMethod", "simple");
        map.put("forceBindingLogin", "true");
        map.put("debug", configuration.get("security.authentication.method.debug"));
        map.put("hostname", configuration.get("security.authentication.method.hostname"));
        map.put("port", configuration.get("security.authentication.method.port"));
        map.put("userBaseDn", configuration.get("security.authentication.method.userBaseDn"));
        map.put("userRdnAttribute", configuration.get("security.authentication.method.userRdnAttribute"));
        map.put("userObjectClass", configuration.get("security.authentication.method.userObjectClass"));
        return new AppConfigurationEntry[] {
          new AppConfigurationEntry(configuration.get("security.authentication.method.className"),
                                    AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, map)
        };
      }
    };
  }
}

package com.continuuity.security.server;

import com.continuuity.common.conf.CConfiguration;
import com.google.inject.Inject;

import javax.security.auth.login.AppConfigurationEntry;
import javax.security.auth.login.Configuration;
import java.util.HashMap;

/**
 *
 */
public class LDAPAuthenticationHandler extends JAASAuthenticationHandler {
  private final CConfiguration configuration;
  private final String configBase = "security.authentication.method.";
  private static final String[] mandatoryConfigurables = new String[] { "debug", "hostname", "port", "userBaseDn",
                                                                                "userRdnAttribute", "userObjectClass" };
  private static final String[] optionalConfigurables = new String[] { "bindDn", "bindPassword", "userIdAttribute",
                                                                      "userPasswordAttribute", "roleBaseDn",
                                                                      "roleNameAttribute", "roleMemberAttribute",
                                                                      "roleObjectClass" };

  @Inject
  public LDAPAuthenticationHandler(CConfiguration configuration) throws Exception {
    super("ldaploginmodule");
    this.configuration = configuration;
  }

  @Override
  protected Configuration getConfiguration() {
    return new Configuration() {
      @Override
      public AppConfigurationEntry[] getAppConfigurationEntry(String s) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("contextFactory", "com.sun.jndi.ldap.LdapCtxFactory");
        map.put("authenticationMethod", "simple");
        map.put("forceBindingLogin", "true");

        for(String configurable : mandatoryConfigurables) {
          map.put(configurable, configuration.get(configBase.concat(configurable)));
        }

        for(String configurable: optionalConfigurables) {
          String value = configuration.get(configBase.concat(configurable));
          if (value != null) {
            map.put(configurable, value);
          }
        }
        return new AppConfigurationEntry[] {
          new AppConfigurationEntry(configuration.get("security.authentication.method.className"),
                                    AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, map)
        };
      }
    };
  }
}

Allow LDAPHandler to have optional configurables