Search in sources :

Example 21 with InitialLdapContext

use of javax.naming.ldap.InitialLdapContext in project aries by apache.

the class InitialContextTest method testLookFromLdapICF.

@Test
public void testLookFromLdapICF() throws Exception {
    InitialContextFactoryBuilder icf = Skeleton.newMock(InitialContextFactoryBuilder.class);
    bc.registerService(new String[] { InitialContextFactoryBuilder.class.getName(), icf.getClass().getName() }, icf, (Dictionary) new Properties());
    LdapContext backCtx = Skeleton.newMock(LdapContext.class);
    InitialContextFactory fac = Skeleton.newMock(InitialContextFactory.class);
    Skeleton.getSkeleton(fac).setReturnValue(new MethodCall(InitialContextFactory.class, "getInitialContext", Hashtable.class), backCtx);
    Skeleton.getSkeleton(icf).setReturnValue(new MethodCall(InitialContextFactoryBuilder.class, "createInitialContextFactory", Hashtable.class), fac);
    Properties props = new Properties();
    props.put(JNDIConstants.BUNDLE_CONTEXT, bc);
    props.put(Context.INITIAL_CONTEXT_FACTORY, "dummy.factory");
    InitialLdapContext ilc = new InitialLdapContext(props, new Control[0]);
    ExtendedRequest req = Skeleton.newMock(ExtendedRequest.class);
    ilc.extendedOperation(req);
    Skeleton.getSkeleton(backCtx).assertCalled(new MethodCall(LdapContext.class, "extendedOperation", req));
}
Also used : InitialContextFactoryBuilder(javax.naming.spi.InitialContextFactoryBuilder) Hashtable(java.util.Hashtable) ExtendedRequest(javax.naming.ldap.ExtendedRequest) InitialLdapContext(javax.naming.ldap.InitialLdapContext) Properties(java.util.Properties) InitialContextFactory(javax.naming.spi.InitialContextFactory) MethodCall(org.apache.aries.unittest.mocks.MethodCall) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LdapContext(javax.naming.ldap.LdapContext) Test(org.junit.Test)

Example 22 with InitialLdapContext

use of javax.naming.ldap.InitialLdapContext in project tomcat by apache.

the class JNDIRealm method createTlsDirContext.

/**
 * Create a tls enabled LdapContext and set the StartTlsResponse tls
 * instance variable.
 *
 * @param env
 *            Environment to use for context creation
 * @return configured {@link LdapContext}
 * @throws NamingException
 *             when something goes wrong while negotiating the connection
 */
private DirContext createTlsDirContext(Hashtable<String, String> env) throws NamingException {
    Map<String, Object> savedEnv = new HashMap<>();
    for (String key : Arrays.asList(Context.SECURITY_AUTHENTICATION, Context.SECURITY_CREDENTIALS, Context.SECURITY_PRINCIPAL, Context.SECURITY_PROTOCOL)) {
        Object entry = env.remove(key);
        if (entry != null) {
            savedEnv.put(key, entry);
        }
    }
    LdapContext result = null;
    try {
        result = new InitialLdapContext(env, null);
        tls = (StartTlsResponse) result.extendedOperation(new StartTlsRequest());
        if (getHostnameVerifier() != null) {
            tls.setHostnameVerifier(getHostnameVerifier());
        }
        if (getCipherSuitesArray() != null) {
            tls.setEnabledCipherSuites(getCipherSuitesArray());
        }
        try {
            SSLSession negotiate = tls.negotiate(getSSLSocketFactory());
            containerLog.debug(sm.getString("jndiRealm.negotiatedTls", negotiate.getProtocol()));
        } catch (IOException e) {
            throw new NamingException(e.getMessage());
        }
    } finally {
        if (result != null) {
            for (Map.Entry<String, Object> savedEntry : savedEnv.entrySet()) {
                result.addToEnvironment(savedEntry.getKey(), savedEntry.getValue());
            }
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) InitialLdapContext(javax.naming.ldap.InitialLdapContext) SSLSession(javax.net.ssl.SSLSession) NamingException(javax.naming.NamingException) IOException(java.io.IOException) StartTlsRequest(javax.naming.ldap.StartTlsRequest) Map(java.util.Map) HashMap(java.util.HashMap) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LdapContext(javax.naming.ldap.LdapContext)

Example 23 with InitialLdapContext

use of javax.naming.ldap.InitialLdapContext in project openolat by klemens.

the class LDAPLoginManagerImpl method bindSystem.

/**
 * Connect to the LDAP server with System DN and Password
 *
 * Configuration: LDAP URL = ldapContext.xml (property=ldapURL) System DN =
 * ldapContext.xml (property=ldapSystemDN) System PW = ldapContext.xml
 * (property=ldapSystemPW)
 *
 * @return The LDAP connection (LdapContext) or NULL if connect fails
 *
 * @throws NamingException
 */
public LdapContext bindSystem() {
    // set LDAP connection attributes
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, ldapLoginModule.getLdapUrl());
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, ldapLoginModule.getLdapSystemDN());
    env.put(Context.SECURITY_CREDENTIALS, ldapLoginModule.getLdapSystemPW());
    if (ldapLoginModule.getLdapConnectionTimeout() != null) {
        env.put(TIMEOUT_KEY, ldapLoginModule.getLdapConnectionTimeout().toString());
    }
    // check ssl
    if (ldapLoginModule.isSslEnabled()) {
        enableSSL(env);
    }
    try {
        InitialLdapContext ctx = new InitialLdapContext(env, new Control[] {});
        ctx.getConnectControls();
        return ctx;
    } catch (NamingException e) {
        log.error("NamingException when trying to bind system with DN::" + ldapLoginModule.getLdapSystemDN() + " and PW::" + ldapLoginModule.getLdapSystemPW() + " on URL::" + ldapLoginModule.getLdapUrl(), e);
        return null;
    } catch (Exception e) {
        log.error("Exception when trying to bind system with DN::" + ldapLoginModule.getLdapSystemDN() + " and PW::" + ldapLoginModule.getLdapSystemPW() + " on URL::" + ldapLoginModule.getLdapUrl(), e);
        return null;
    }
}
Also used : Hashtable(java.util.Hashtable) InitialLdapContext(javax.naming.ldap.InitialLdapContext) NamingException(javax.naming.NamingException) NamingException(javax.naming.NamingException) AuthenticationException(javax.naming.AuthenticationException)

Example 24 with InitialLdapContext

use of javax.naming.ldap.InitialLdapContext in project sonarqube by SonarSource.

the class LdapContextFactory method createInitialDirContext.

private InitialDirContext createInitialDirContext(String principal, String credentials, boolean pooling) throws NamingException {
    final InitialLdapContext ctx;
    if (startTLS) {
        // Note that pooling is not enabled for such connections, because "Stop TLS" is not performed.
        Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY, factory);
        env.put(Context.PROVIDER_URL, providerUrl);
        env.put(Context.REFERRAL, referral);
        // At this point env should not contain properties SECURITY_AUTHENTICATION, SECURITY_PRINCIPAL and SECURITY_CREDENTIALS to avoid
        // "bind" operation prior to StartTLS:
        ctx = new InitialLdapContext(env, null);
        // http://docs.oracle.com/javase/jndi/tutorial/ldap/ext/starttls.html
        StartTlsResponse tls = (StartTlsResponse) ctx.extendedOperation(new StartTlsRequest());
        try {
            tls.negotiate();
        } catch (IOException e) {
            NamingException ex = new NamingException("StartTLS failed");
            ex.initCause(e);
            throw ex;
        }
        // Explicitly initiate "bind" operation:
        ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, authentication);
        if (principal != null) {
            ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, principal);
        }
        if (credentials != null) {
            ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, credentials);
        }
        ctx.reconnect(null);
    } else {
        ctx = new InitialLdapContext(getEnvironment(principal, credentials, pooling), null);
    }
    return ctx;
}
Also used : StartTlsResponse(javax.naming.ldap.StartTlsResponse) InitialLdapContext(javax.naming.ldap.InitialLdapContext) NamingException(javax.naming.NamingException) IOException(java.io.IOException) Properties(java.util.Properties) StartTlsRequest(javax.naming.ldap.StartTlsRequest)

Example 25 with InitialLdapContext

use of javax.naming.ldap.InitialLdapContext in project sonarqube by SonarSource.

the class LdapContextFactory method createInitialDirContextUsingGssapi.

private InitialDirContext createInitialDirContextUsingGssapi(String principal, String credentials) throws NamingException {
    Configuration.setConfiguration(new Krb5LoginConfiguration());
    InitialDirContext initialDirContext;
    try {
        LoginContext lc = new LoginContext(getClass().getName(), new CallbackHandlerImpl(principal, credentials));
        lc.login();
        initialDirContext = Subject.doAs(lc.getSubject(), new PrivilegedExceptionAction<InitialDirContext>() {

            @Override
            public InitialDirContext run() throws NamingException {
                Properties env = new Properties();
                env.put(Context.INITIAL_CONTEXT_FACTORY, factory);
                env.put(Context.PROVIDER_URL, providerUrl);
                env.put(Context.REFERRAL, referral);
                return new InitialLdapContext(env, null);
            }
        });
    } catch (LoginException | PrivilegedActionException e) {
        NamingException namingException = new NamingException(e.getMessage());
        namingException.initCause(e);
        throw namingException;
    }
    return initialDirContext;
}
Also used : LoginContext(javax.security.auth.login.LoginContext) PrivilegedActionException(java.security.PrivilegedActionException) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LoginException(javax.security.auth.login.LoginException) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) Properties(java.util.Properties)

Aggregations

InitialLdapContext (javax.naming.ldap.InitialLdapContext)54 NamingException (javax.naming.NamingException)30 Hashtable (java.util.Hashtable)17 LdapContext (javax.naming.ldap.LdapContext)17 Attributes (javax.naming.directory.Attributes)16 Properties (java.util.Properties)14 SearchResult (javax.naming.directory.SearchResult)14 IOException (java.io.IOException)11 AuthenticationException (javax.naming.AuthenticationException)10 NamingEnumeration (javax.naming.NamingEnumeration)10 StartTlsRequest (javax.naming.ldap.StartTlsRequest)10 BasicAttributes (javax.naming.directory.BasicAttributes)9 Attribute (javax.naming.directory.Attribute)8 SearchControls (javax.naming.directory.SearchControls)8 LdapConfigProperties (org.bedework.calfacade.configs.LdapConfigProperties)7 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)7 StartTlsResponse (javax.naming.ldap.StartTlsResponse)6 CommunicationException (javax.naming.CommunicationException)5 DirContext (javax.naming.directory.DirContext)5 BwGroup (org.bedework.calfacade.BwGroup)5