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