Example usage for javax.naming Context SECURITY_AUTHENTICATION

List of usage examples for javax.naming Context SECURITY_AUTHENTICATION

Introduction

In this page you can find the example usage for javax.naming Context SECURITY_AUTHENTICATION.

Prototype

String SECURITY_AUTHENTICATION

To view the source code for javax.naming Context SECURITY_AUTHENTICATION.

Click Source Link

Document

Constant that holds the name of the environment property for specifying the security level to use.

Usage

From source file:org.apache.syncope.core.rest.AbstractTest.java

@SuppressWarnings({ "unchecked", "rawtypes", "UseOfObsoleteCollectionType" })
protected Object getLdapRemoteObject(final String bindDn, final String bindPwd, final String objectDn) {
    ResourceTO ldapRes = resourceService.read(RESOURCE_NAME_LDAP);
    final Map<String, ConnConfProperty> ldapConnConf = connectorService.read(ldapRes.getConnectorId())
            .getConfigurationMap();//from   w  w  w  . j  a va  2  s .c  o  m

    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://" + ldapConnConf.get("host").getValues().get(0) + ":"
            + ldapConnConf.get("port").getValues().get(0) + "/");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL,
            bindDn == null ? ldapConnConf.get("principal").getValues().get(0) : bindDn);
    env.put(Context.SECURITY_CREDENTIALS,
            bindPwd == null ? ldapConnConf.get("credentials").getValues().get(0) : bindPwd);

    try {
        final InitialDirContext ctx = new InitialDirContext(env);
        return ctx.lookup(objectDn);
    } catch (Exception e) {
        return null;
    }
}

From source file:org.apache.syncope.fit.AbstractITCase.java

@SuppressWarnings({ "unchecked", "rawtypes", "UseOfObsoleteCollectionType" })
protected InitialDirContext getLdapResourceDirContext(final String bindDn, final String bindPwd)
        throws NamingException {
    ResourceTO ldapRes = resourceService.read(RESOURCE_NAME_LDAP);
    ConnInstanceTO ldapConn = connectorService.read(ldapRes.getConnector(), Locale.ENGLISH.getLanguage());

    Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://" + ldapConn.getConf("host").get().getValues().get(0) + ":"
            + ldapConn.getConf("port").get().getValues().get(0) + "/");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL,
            bindDn == null ? ldapConn.getConf("principal").get().getValues().get(0) : bindDn);
    env.put(Context.SECURITY_CREDENTIALS,
            bindPwd == null ? ldapConn.getConf("credentials").get().getValues().get(0) : bindPwd);

    return new InitialDirContext(env);
}

From source file:org.apache.syncope.fit.core.reference.AbstractITCase.java

@SuppressWarnings({ "unchecked", "rawtypes", "UseOfObsoleteCollectionType" })
protected InitialDirContext getLdapResourceDirContext(final String bindDn, final String bindPwd)
        throws NamingException {
    ResourceTO ldapRes = resourceService.read(RESOURCE_NAME_LDAP);
    final Map<String, ConnConfProperty> ldapConnConf = connectorService.read(ldapRes.getConnector())
            .getConfigurationMap();/*from w  w w. j ava2s.co  m*/

    Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://" + ldapConnConf.get("host").getValues().get(0) + ":"
            + ldapConnConf.get("port").getValues().get(0) + "/");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL,
            bindDn == null ? ldapConnConf.get("principal").getValues().get(0) : bindDn);
    env.put(Context.SECURITY_CREDENTIALS,
            bindPwd == null ? ldapConnConf.get("credentials").getValues().get(0) : bindPwd);

    return new InitialDirContext(env);
}

From source file:org.apereo.portal.groups.ldap.LDAPGroupStore.java

protected DirContext getConnection() {
    //JNDI boilerplate to connect to an initial context
    DirContext context = (DirContext) contexts.get("context");
    if (context == null) {
        Hashtable jndienv = new Hashtable();
        jndienv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        jndienv.put(Context.SECURITY_AUTHENTICATION, "simple");
        if (url.startsWith("ldaps")) { // Handle SSL connections
            String newurl = url.substring(0, 4) + url.substring(5);
            jndienv.put(Context.SECURITY_PROTOCOL, "ssl");
            jndienv.put(Context.PROVIDER_URL, newurl);
        } else {//from  w ww .  j ava  2  s.  c om
            jndienv.put(Context.PROVIDER_URL, url);
        }
        if (logonid != null)
            jndienv.put(Context.SECURITY_PRINCIPAL, logonid);
        if (logonpassword != null)
            jndienv.put(Context.SECURITY_CREDENTIALS, logonpassword);
        try {
            context = new InitialDirContext(jndienv);
        } catch (NamingException nex) {
            log.error("LDAPGroupStore: unable to get context", nex);
        }
        contexts.put("context", context);
    }
    return context;
}

From source file:org.atricore.idbus.idojos.ldapidentitystore.LDAPIdentityStore.java

/**
 * Creates an InitialLdapContext by logging into the configured Ldap Server using the provided
 * username and credential./*  w  w w.  jav a 2s.c  om*/
 *
 * @return the Initial Ldap Context to be used to perform searches, etc.
 * @throws NamingException LDAP binding error.
 */
protected InitialLdapContext createLdapInitialContext(String securityPrincipal, String securityCredential)
        throws NamingException {

    Properties env = new Properties();

    env.setProperty(Context.INITIAL_CONTEXT_FACTORY, getInitialContextFactory());
    env.setProperty(Context.SECURITY_AUTHENTICATION, getSecurityAuthentication());
    env.setProperty(Context.PROVIDER_URL, getProviderUrl());
    env.setProperty(Context.SECURITY_PROTOCOL, (getSecurityProtocol() == null ? "" : getSecurityProtocol()));

    // Set defaults for key values if they are missing

    String factoryName = env.getProperty(Context.INITIAL_CONTEXT_FACTORY);
    if (factoryName == null) {
        factoryName = "com.sun.jndi.ldap.LdapCtxFactory";
        env.setProperty(Context.INITIAL_CONTEXT_FACTORY, factoryName);
    }

    String authType = env.getProperty(Context.SECURITY_AUTHENTICATION);

    if (authType == null)
        env.setProperty(Context.SECURITY_AUTHENTICATION, "simple");

    String protocol = env.getProperty(Context.SECURITY_PROTOCOL);
    String providerURL = getProviderUrl();
    // Use localhost if providerUrl not set
    if (providerURL == null) {
        providerURL = "ldap://localhost:" + ((protocol != null && protocol.equals("ssl")) ? "636" : "389");
    } else {
        // In case user configured provided URL
        if (providerURL.startsWith("ldaps")) {
            protocol = "ssl";
            env.setProperty(Context.SECURITY_PROTOCOL, "ssl");
        }

    }

    env.setProperty(Context.PROVIDER_URL, providerURL);

    if (securityPrincipal != null && !"".equals(securityPrincipal))
        env.setProperty(Context.SECURITY_PRINCIPAL, securityPrincipal);

    if (securityCredential != null && !"".equals(securityCredential))
        env.put(Context.SECURITY_CREDENTIALS, securityCredential);

    // always follow referrals transparently
    env.put(Context.REFERRAL, "follow");

    // Logon into LDAP server
    if (logger.isDebugEnabled())
        logger.debug("Logging into LDAP server, env=" + env);

    InitialLdapContext ctx = new InitialLdapContext(env, null);

    if (logger.isDebugEnabled())
        logger.debug("Logged into LDAP server, " + ctx);

    return ctx;
}

From source file:org.easy.ldap.LdapContextFactory.java

public DirContext createSecureContext(LdapName rootDn, LdapName principal, String password,
        String securityMethod) throws NamingException {
    Hashtable<String, String> environment = getEnviroment();
    environment.put(Context.PROVIDER_URL, createProviderUrl(rootDn.toString()));

    environment.put(Context.SECURITY_AUTHENTICATION, securityMethod);
    environment.put(Context.SECURITY_PRINCIPAL, principal.toString());
    environment.put(Context.SECURITY_CREDENTIALS, password);

    return createContext(environment);
}

From source file:org.easy.ldap.LdapContextFactory.java

/**
 * @return/*  w w w.  j a va2s .  c  om*/
 */
private Hashtable<String, String> getEnviroment() {
    Hashtable<String, String> properties = new Hashtable<String, String>();

    properties.put(Context.PROVIDER_URL, createProviderUrl(environment.getProperty(PropertyNames.DOMAIN_DN)));

    properties.put(Context.INITIAL_CONTEXT_FACTORY,
            environment.getProperty(PropertyNames.INITIAL_CONTEXT_FACTORY_CLASS));
    properties.put("com.sun.jndi.ldap.connect.pool",
            environment.getProperty(PropertyNames.USE_LDAP_CONNECT_POOL));
    properties.put(Context.SECURITY_AUTHENTICATION,
            environment.getProperty(PropertyNames.ADMIN_AUTHENTICATION_METHOD));
    properties.put(Context.SECURITY_PRINCIPAL, environment.getProperty(PropertyNames.ADMIN_PRINCIPAL));
    properties.put(Context.SECURITY_CREDENTIALS, environment.getProperty(PropertyNames.ADMIN_CREDENTIALS));

    return properties;
}

From source file:org.eclipse.skalli.core.user.ldap.LDAPClient.java

private LdapContext getLdapContext() throws NamingException, AuthenticationException {
    if (config == null) {
        throw new NamingException("LDAP not configured");
    }//from   w  w w .  j  a  va2 s .c  o  m
    if (StringUtils.isBlank(config.getProviderUrl())) {
        throw new NamingException("No LDAP server available");
    }
    if (StringUtils.isBlank(config.getUsername()) || StringUtils.isBlank(config.getPassword())) {
        throw new AuthenticationException("No LDAP credentials available");
    }
    String ctxFactory = config.getCtxFactory();
    if (StringUtils.isBlank(ctxFactory)) {
        ctxFactory = DEFAULT_CONTEXT_FACTORY;
    }
    String authentication = config.getAuthentication();
    if (StringUtils.isBlank(authentication)) {
        authentication = SIMPLE_AUTHENTICATION;
    }

    Hashtable<String, Object> env = new Hashtable<String, Object>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, ctxFactory);
    env.put(Context.PROVIDER_URL, config.getProviderUrl());
    env.put(Context.SECURITY_PRINCIPAL, config.getUsername());
    env.put(Context.SECURITY_CREDENTIALS, config.getPassword());
    env.put(Context.SECURITY_AUTHENTICATION, authentication);
    if (StringUtils.isNotBlank(config.getReferral())) {
        env.put(Context.REFERRAL, config.getReferral());
    }
    if (config.getProviderUrl().startsWith(LDAPS_SCHEME)) {
        env.put(Context.SECURITY_PROTOCOL, "ssl"); //$NON-NLS-1$
        if (config.isSslNoVerify()) {
            env.put(JNDI_SOCKET_FACTORY, LDAPTrustAllSocketFactory.class.getName());
        }
    }
    // Gemini-specific properties
    env.put(JNDIConstants.BUNDLE_CONTEXT, FrameworkUtil.getBundle(LDAPClient.class).getBundleContext());

    // com.sun.jndi.ldap.LdapCtxFactory specific properties
    env.put(READ_TIMEOUT, DEFAULT_READ_TIMEOUT);
    env.put(USE_CONNECTION_POOLING, "true"); //$NON-NLS-1$

    // extremly ugly classloading workaround:
    // com.sun.jndi.ldap.LdapCtxFactory uses Class.forName() to load the socket factory, shame on them!
    InitialLdapContext ctx = null;
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(LDAPTrustAllSocketFactory.class.getClassLoader());
        ctx = new InitialLdapContext(env, null);
    } finally {
        if (classloader != null) {
            Thread.currentThread().setContextClassLoader(classloader);
        }
    }
    return ctx;
}

From source file:org.eclipselabs.etrack.util.security.ldap.impl.LdapSecurityService.java

@Override
public boolean authenticate(String id, char[] password) {
    String cachedPassword = credentialCache.get(id);
    String encodedPassword = null;

    try {/* w w w .j  ava 2s.c om*/
        encodedPassword = codec.encode(new String(password));
    } catch (EncoderException e1) {
    }

    if (cachedPassword != null && encodedPassword != null && cachedPassword.equals(encodedPassword))
        return true;

    Hashtable<String, String> environment = new Hashtable<String, String>();
    environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    environment.put(Context.PROVIDER_URL, url);
    environment.put(Context.SECURITY_AUTHENTICATION, "simple");
    environment.put(Context.SECURITY_PRINCIPAL, id);
    environment.put(Context.SECURITY_CREDENTIALS, new String(password));

    try {
        InitialDirContext context = new InitialDirContext(environment);
        context.close();

        if (encodedPassword != null)
            credentialCache.put(id, encodedPassword);

        return true;
    } catch (NamingException e) {
        return false;
    }
}

From source file:org.eclipselabs.etrack.util.security.ldap.impl.LdapSecurityService.java

void activate(Map<?, ?> configuration) throws NamingException {
    this.url = (String) configuration.get(CONFIG_URL);
    this.baseDN = (String) configuration.get(CONFIG_BASE_DN);
    String managerDN = (String) configuration.get(CONFIG_MANAGER_DN);
    String managerPassword = (String) configuration.get(CONFIG_MANAGER_PASSWORD);

    Hashtable<String, String> environment = new Hashtable<String, String>();
    environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    environment.put(Context.PROVIDER_URL, url);

    if (managerDN != null) {
        environment.put(Context.SECURITY_AUTHENTICATION, "simple");
        environment.put(Context.SECURITY_PRINCIPAL, managerDN);
        environment.put(Context.SECURITY_CREDENTIALS, managerPassword);
    } else/* w w w.  j  a  va  2s  .  c o  m*/
        environment.put(Context.SECURITY_AUTHENTICATION, "none");

    searchContext = new InitialDirContext(environment);
}