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)
        };
      }
    };
  }
}

@@ -382,16 +382,753 @@
 uration;
+%0A  private final String configBase = %22security.authentication.method.%22;%0A  private static final String%5B%5D mandatoryConfigurables = new String%5B%5D %7B %22debug%22, %22hostname%22, %22port%22, %22userBaseDn%22,%0A                                                                                %22userRdnAttribute%22, %22userObjectClass%22 %7D;%0A  private static final String%5B%5D optionalConfigurables = new String%5B%5D %7B %22bindDn%22, %22bindPassword%22, %22userIdAttribute%22,%0A                                                                      %22userPasswordAttribute%22, %22roleBaseDn%22,%0A                                                                      %22roleNameAttribute%22, %22roleMemberAttribute%22,%0A                                                                      %22roleObjectClass%22 %7D;
 %0A%0A  @Inj
@@ -1705,32 +1705,33 @@
 %22, %22true%22);%0A
+%0A
         
 map.put(%22deb
@@ -1722,85 +1722,62 @@
     
-map.put(%22debug%22, configuration.get(%22security.authentication.method.debug%22));%0A
+for(String configurable : mandatoryConfigurables) %7B%0A  
     
@@ -1788,26 +1788,28 @@
 map.put(
-%22hostname%22
+configurable
 , config
@@ -1824,442 +1824,283 @@
 get(
-%22security.authentication.method.hostname%22));%0A        map.put(%22port%22, configuration.get(%22security.authentication.method.port%22));%0A        map.put(%22userBaseDn%22, configuration.get(%22security.authentication.method.userBaseDn%22));%0A        map.put(%22userRdnAttribute%22, configuration.get(%22security.authentication.method.userRdnAttribute%22));%0A        map.put(%22userObjectClass%22, configuration.get(%22security.authentication.method.userObjectClass%22));
+configBase.concat(configurable)));%0A        %7D%0A%0A        for(String configurable: optionalConfigurables) %7B%0A          String value = configuration.get(configBase.concat(configurable));%0A          if (value != null) %7B%0A            map.put(configurable, value);%0A          %7D%0A        %7D
 %0A   

Allow LDAPHandler to have optional configurables