All Downloads are FREE. Search and download functionalities are using the official Maven repository.

co.cask.common.security.server.LDAPAuthenticationHandler Maven / Gradle / Ivy

There is a newer version: 0.11.0
Show newest version
/*
 * Copyright © 2014 Cask Data, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package co.cask.common.security.server;

import co.cask.common.security.Constants;
import co.cask.common.security.config.SecurityConfiguration;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import com.google.inject.Inject;

import java.util.List;
import java.util.Map;
import javax.security.auth.login.AppConfigurationEntry;

/**
 * An Authentication handler that authenticates against a LDAP server instance for External Authentication.
 */
public class LDAPAuthenticationHandler extends JAASAuthenticationHandler {
  private static final List mandatoryConfigurables = ImmutableList.of("debug", "hostname", "port", "userBaseDn",
                                                                              "userRdnAttribute", "userObjectClass");

  private static final List optionalConfigurables = ImmutableList.of("bindDn", "bindPassword", "useLdaps",
                                                                             "userIdAttribute", "userPasswordAttribute",
                                                                             "roleBaseDn", "roleNameAttribute",
                                                                             "roleMemberAttribute", "roleObjectClass");
  private static boolean ldapSSLVerifyCertificate = true;

  /**
   * Create a new Authentication handler to use LDAP for external authentication.
   */
  @Inject
  public LDAPAuthenticationHandler(SecurityConfiguration configuration) throws Exception {
    super(configuration);
  }

  /**
   * Create a configuration from properties. Allows optional configurables.
   */
  @Override
  protected javax.security.auth.login.Configuration getLoginModuleConfiguration() {
    return new javax.security.auth.login.Configuration() {
      @Override
      public AppConfigurationEntry[] getAppConfigurationEntry(String s) {
        Map map = Maps.newHashMap();
        map.put("contextFactory", "com.sun.jndi.ldap.LdapCtxFactory");
        map.put("authenticationMethod", "simple");
        map.put("forceBindingLogin", "true");

        String authConfigBase = Constants.AUTH_HANDLER_CONFIG_BASE;
        for (String configurable : mandatoryConfigurables) {
          String key = authConfigBase.concat(configurable);
          String value = configuration.get(key);
          if (value == null) {
            String errorMessage = String.format("Mandatory configuration %s is not set.", key);
            throw Throwables.propagate(new RuntimeException(errorMessage));
          }
          map.put(configurable, value);
        }

        for (String configurable: optionalConfigurables) {
          String value = configuration.get(authConfigBase.concat(configurable));
          if (value != null) {
            map.put(configurable, value);
          }
        }

        ldapSSLVerifyCertificate = configuration.getBoolean(authConfigBase.concat("ldapsVerifyCertificate"), true);

        return new AppConfigurationEntry[] {
          new AppConfigurationEntry(configuration.get(Constants.LOGIN_MODULE_CLASS_NAME),
                                    AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, map)
        };
      }
    };
  }

  static boolean getLdapSSLVerifyCertificate() {
    return ldapSSLVerifyCertificate;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy