net.sf.jkniv.jaas.LdapAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jkniv-jaas-common Show documentation
Show all versions of jkniv-jaas-common Show documentation
JAAS Hybrid realm module commons
/*
* JKNIV JAAS,
* Copyright (C) 2017, the original author or authors.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.sf.jkniv.jaas;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.security.auth.login.LoginException;
public class LdapAdapter
{
private static final Logger LOG = MyLoggerFactory.getLogger(LdapAdapter.class);
// find the pattern CN=...
private static final String REGEX_COMMON_NAME = "CN=[\\w\\.?]+"; // CN=my_group,OU=dev,OU=acme,DC=acme,DC=com,DC=br ---> CN=my_group
public static final Pattern PATTERN_CN = Pattern.compile(REGEX_COMMON_NAME,
Pattern.CASE_INSENSITIVE);
/** LDAP URL for your server */
public static final String PROP_DIRURL = "directories";
/* LDAP base DN for the location of user data */
//public static final String PROP_BASEDN = "base-dn";
/** security level to use "none", "simple", "strong". */
public static final String PROP_SECURITY_AUTHENTICATION = "auth-level";
/** Default domain from users [email protected] */
public static final String PROP_DEFAULT_DOMAIN = "default-domain";
/** Attribute name thats representgroup-member-attr group of user group-member-attr */
public static final String PROP_ATTR_GROUP_MEMBER = "group-member-attr";
public static final String DEFAULT_AUTH = "simple";
public static final String DEFAULT_FETCH_ATTR = "memberOf";
private static final String DEFAULT_REFERRAL = "follow";
private static final String PROP_FORCE_AUTH_LDAP = "force-auth-ldap";
private static final String URL_LDAP = "ldap://";
private static final String URL_LDAPS = "ldaps://";
private static final String DEFAULT_POOL_PROTOCOL = "plain ssl";
private static final String SSL = "SSL";
private static final String PORT_SSL = "636";
private static final String PORT = "389";
// --------------------------------------------------------------------------------------------- //
// These are optional, defaults are provided
// %s = subject name
// %d = DN of user search result
public static final String PROP_SEARCH_FILTER = "search-filter";
public static final String PROP_JNDICF = "jndiCtxFactory";
public static final String PROP_READ_TIMEOUT = "read.timeout";
// Expansion strings
public static final String SUBST_SUBJECT_NAME = "%s";
public static final String SUBST_SUBJECT_DN = "%d";
// Defaults
private static final String DEFAULT_SEARCH_FILTER = "mail=" + SUBST_SUBJECT_NAME;
private static final String DEFAULT_JNDICF = "com.sun.jndi.ldap.LdapCtxFactory";
private Properties propsLdap = new Properties();
private String defaultBaseDn;
/** pairs from url and baseDn: acme.com.br -> dc=acme,dc=com,dc=br */
private Map urlDc;
private boolean sslEnable;
private boolean forceAuthLdap;
private Map> cacheGroup;
public LdapAdapter(Properties props) throws BadRealmException//, NoSuchRealmException
{
this.sslEnable = false; // FIXME configure ssl
this.urlDc = new HashMap();
this.cacheGroup = new HashMap>();
setPropertyValue(PROP_DIRURL, "", props);
setPropertyValue(PROP_DEFAULT_DOMAIN, "", props);
String ctxF = setPropertyValue(PROP_JNDICF, DEFAULT_JNDICF, props);
this.propsLdap.setProperty(Context.INITIAL_CONTEXT_FACTORY, ctxF);
String authSec = setPropertyValue(PROP_SECURITY_AUTHENTICATION, DEFAULT_AUTH, props);
this.propsLdap.setProperty(Context.SECURITY_AUTHENTICATION, authSec);
this.forceAuthLdap = Boolean.valueOf(props.getProperty(PROP_FORCE_AUTH_LDAP, "false"));
setPropertyValue(Context.REFERRAL, DEFAULT_REFERRAL, props);
settingLdapProperties(props);
// using search filters
String filter = props.getProperty(PROP_SEARCH_FILTER);
if (filter == null)
filter = DEFAULT_SEARCH_FILTER;
else
filter = filter + "=" + SUBST_SUBJECT_NAME;
setPropertyValue(PROP_SEARCH_FILTER, filter);
setPropertyValue(PROP_ATTR_GROUP_MEMBER, DEFAULT_FETCH_ATTR, props);
buildDomainComponent();
checkMandatoryProperties();
LOG.info("LDAP Adapter Properties");
for( Entry