Example usage for javax.naming Context SECURITY_PRINCIPAL

List of usage examples for javax.naming Context SECURITY_PRINCIPAL

Introduction

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

Prototype

String SECURITY_PRINCIPAL

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

Click Source Link

Document

Constant that holds the name of the environment property for specifying the identity of the principal for authenticating the caller to the service.

Usage

From source file:org.wso2.carbon.identity.agent.onprem.userstore.manager.ldap.LDAPConnectionContext.java

@SuppressWarnings({ "rawtypes", "unchecked" })
LDAPConnectionContext(Map<String, String> userStoreProperties) throws UserStoreException {

    String connectionURL = userStoreProperties.get(LDAPConstants.CONNECTION_URL);
    String connectionName = userStoreProperties.get(LDAPConstants.CONNECTION_NAME);
    String connectionPassword = userStoreProperties.get(LDAPConstants.CONNECTION_PASSWORD);

    if (log.isDebugEnabled()) {
        log.debug("Connection Name :: " + connectionName + ", Connection URL :: " + connectionURL);
    }/*w w w .  ja va  2 s .c o m*/

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

    if (connectionName != null) {
        environment.put(Context.SECURITY_PRINCIPAL, connectionName);
    }

    if (connectionPassword != null) {
        environment.put(Context.SECURITY_CREDENTIALS, connectionPassword);
    }

    if (connectionURL != null) {
        environment.put(Context.PROVIDER_URL, connectionURL);
    }

    // Enable connection pooling if property is set in user-mgt.xml
    boolean isLDAPConnectionPoolingEnabled = false;
    String value = userStoreProperties.get(LDAPConstants.CONNECTION_POOLING_ENABLED);

    if (value != null && !value.trim().isEmpty()) {
        isLDAPConnectionPoolingEnabled = Boolean.parseBoolean(value);
    }

    environment.put("com.sun.jndi.ldap.connect.pool", isLDAPConnectionPoolingEnabled ? "true" : "false");

    // set referral status if provided in configuration.
    if (userStoreProperties.get(LDAPConstants.PROPERTY_REFERRAL) != null) {
        environment.put("java.naming.referral", userStoreProperties.get(LDAPConstants.PROPERTY_REFERRAL));
    }
    //Set connect timeout if provided in configuration. Otherwise set default value
    String connectTimeout = userStoreProperties.get(CONNECTION_TIME_OUT);
    String readTimeout = userStoreProperties.get(READ_TIME_OUT);
    if (connectTimeout != null && !connectTimeout.trim().isEmpty()) {
        environment.put("com.sun.jndi.ldap.connect.timeout", connectTimeout);
    } else {
        environment.put("com.sun.jndi.ldap.connect.timeout", "5000");
    }

    if (StringUtils.isNotEmpty(readTimeout)) {
        environment.put("com.sun.jndi.ldap.read.timeout", readTimeout);
    }
}

From source file:com.marklogic.samplestack.integration.web.LDAPIT.java

@Before
public void setup() throws NamingException {
    env = new Hashtable<String, Object>();
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    if (ldapUsername != null) {
        env.put(Context.SECURITY_PRINCIPAL, ldapUsername);
    }/*from   ww  w .  j  av a 2 s .c  o m*/
    if (ldapPassword != null) {
        env.put(Context.SECURITY_CREDENTIALS, ldapPassword);
    }
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, ldapServer);

    // ensures that objectSID attribute values
    // will be returned as a byte[] instead of a String
    // env.put("java.naming.ldap.attributes.binary", "uid");

    // the following is helpful in debugging errors
    //env.put("com.sun.jndi.ldap.trace.ber", System.err);

    ctx = new InitialLdapContext(env, null);

}

From source file:org.aludratest.service.jms.impl.JmsServiceImpl.java

@Override
public void configure(Preferences preferences) {
    ValidatingPreferencesWrapper prefs = new ValidatingPreferencesWrapper(preferences);
    providerUrl = prefs.getRequiredStringValue("providerUrl");

    String initialContextFactory = prefs.getRequiredStringValue("initialContextFactory");
    String connectionFactoryName = prefs.getRequiredStringValue("connectionFactoryJndiName");

    String userName = prefs.getStringValue("jmsUser");
    String password = prefs.getStringValue("jmsPassword");

    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
    env.put(Context.PROVIDER_URL, providerUrl);
    if (!StringUtils.isEmpty(userName)) {
        env.put(Context.SECURITY_PRINCIPAL, userName);
    }// w  ww  .j  a  v  a 2  s. com
    if (!StringUtils.isEmpty(password)) {
        env.put(Context.SECURITY_CREDENTIALS, password);
    }

    try {
        initialContext = new InitialContext(env);
        connectionFactory = (ConnectionFactory) initialContext.lookup(connectionFactoryName);
        if (connectionFactory == null) {
            throw new ConfigurationException("The connection factory could not be found.");
        }

    } catch (NamingException e) {
        throw new TechnicalException("Could not retrieve objects from JNDI context", e);
    }

    action = new JmsActionImpl(connectionFactory, initialContext, userName, password);
}

From source file:org.apache.directory.server.core.jndi.LdapJndiPropertiesTest.java

License:asdf

@Test
public void testNoAuthWithNoCredsEnv() throws Exception {
    Hashtable<String, Object> env = new Hashtable<String, Object>();
    env.put(Context.SECURITY_PRINCIPAL, "");
    env.put(Context.PROVIDER_URL, "");
    LdapJndiProperties props = LdapJndiProperties.getLdapJndiProperties(env);
    assertEquals(AuthenticationLevel.NONE, props.getAuthenticationLevel());
    assertTrue(props.getCredentials() == null);
}

From source file:org.web4thejob.security.ADAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {

    if (authentication.getName() == null || (String) authentication.getCredentials() == null) {
        throw new BadCredentialsException("");
    }/*from   w ww  .  j  av  a  2  s .c  om*/

    String principal = getPrincipal(authentication.getName());
    String passwd = (String) authentication.getCredentials();

    LdapContext ctx = null;
    try {
        Hashtable<String, Object> env = new Hashtable<String, Object>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, LdapCtxFactory.class.getCanonicalName());
        env.put(Context.SECURITY_AUTHENTICATION, "Simple");
        env.put(Context.SECURITY_PRINCIPAL, principal);
        env.put(Context.SECURITY_CREDENTIALS, passwd);
        env.put(Context.PROVIDER_URL, url);
        ctx = new InitialLdapContext(env, null);
        //LDAP Connection Successful

        UserDetails userDetails = userDetailsService.loadUserByUsername(principal);
        return new UsernamePasswordAuthenticationToken(userDetails, "", userDetails.getAuthorities());
    } catch (NamingException nex) {
        throw new BadCredentialsException("LDAP authentication failed.", nex);
    } catch (UsernameNotFoundException e) {
        throw new BadCredentialsException("UserDetails did not find a valid user for name: " + principal, e);
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception ignore) {
            }
        }
    }
}

From source file:org.pegadi.server.user.LDAPUserServerImpl.java

public void init() {
    env.put("java.naming.ldap.version", "3");
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, url + "/" + ldapBaseDN);
    env.put(Context.SECURITY_AUTHENTICATION, auth);
    env.put(Context.SECURITY_PRINCIPAL, ldapLoginDN);
    env.put(Context.SECURITY_CREDENTIALS, ldapPassword);

    try {//  w ww .jav a 2s  .  c om
        ctx = new InitialDirContext(env);
        log.info("Successfully created a Context");
    } catch (NamingException e) {
        log.error("Unable to create a Context", e);
    } catch (Exception e) {
        log.error("This should never come", e);
    }
}

From source file:py.una.pol.karaku.util.LDAPUtil.java

private DirContext createInitialDirContext() {

    Map<Object, String> env = new HashMap<Object, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, propertiesUtil.get(LDAP_SERVER_KEY) + "/" + propertiesUtil.get(LDAP_DN_KEY));
    env.put(Context.SECURITY_PRINCIPAL, propertiesUtil.get(LDAP_ADMIN_KEY));
    env.put(Context.SECURITY_CREDENTIALS, propertiesUtil.get(LDAP_ADMIN_PASS_KEY));

    try {/* w ww  . jav a 2  s . c  om*/
        return new InitialDirContext(new Hashtable<Object, String>(env));

    } catch (NamingException e) {
        throw new KarakuRuntimeException(e.getMessage(), e);
    }

}

From source file:org.projectforge.ldap.LdapConnector.java

private Hashtable<String, String> createEnv(final String user, final String password) {
    // Set up the environment for creating the initial context
    final Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, ldapConfig.getCompleteServerUrl());
    final String authentication = ldapConfig.getAuthentication();
    if (StringUtils.isNotBlank(authentication) == true) {
        env.put(Context.SECURITY_AUTHENTICATION, ldapConfig.getAuthentication());
        if ("none".equals(authentication) == false || user != null || password != null) {
            env.put(Context.SECURITY_PRINCIPAL, user);
            env.put(Context.SECURITY_CREDENTIALS, password);
        }//from   w  w  w  . ja  v  a 2s  .co m
    }
    if (ldapConfig != null && StringUtils.isNotBlank(ldapConfig.getSslCertificateFile()) == true) {
        env.put("java.naming.ldap.factory.socket", "org.projectforge.ldap.MySSLSocketFactory");
    }
    log.info("Trying to connect the LDAP server: url=[" + ldapConfig.getCompleteServerUrl()
            + "], authentication=[" + ldapConfig.getAuthentication() + "], principal=[" + user + "]");
    return env;
}

From source file:org.jboss.test.security.test.SubjectContextUnitTestCase.java

/**
 Access an unchecked method with a valid login that calls the same method
 on another bean using a run-as role./*from  w  ww  . ja v  a2 s .c  om*/
        
 @throws Exception
 */
public void testPublicMethod() throws Exception {
    log.debug("+++ testPublicMethod()");
    Properties env = new Properties();
    env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.JndiLoginInitialContextFactory");
    env.setProperty(Context.SECURITY_PRINCIPAL, "jduke");
    env.setProperty(Context.SECURITY_CREDENTIALS, "theduke");
    InitialContext ctx = new InitialContext(env);
    Object obj = ctx.lookup("jacc/Secured");
    obj = PortableRemoteObject.narrow(obj, SecuredServiceRemoteHome.class);
    SecuredServiceRemoteHome home = (SecuredServiceRemoteHome) obj;
    log.debug("Found SecuredServiceRemoteHome");
    SecuredServiceRemote bean = home.create();
    log.debug("Created SecuredServiceRemote");

    Principal callerIdentity = new SimplePrincipal("jduke");
    Principal runAsIdentity = new SimplePrincipal("runAsUser");
    HashSet expectedCallerRoles = new HashSet();
    expectedCallerRoles.add("groupMemberCaller");
    expectedCallerRoles.add("userCaller");
    expectedCallerRoles.add("allAuthCaller");
    expectedCallerRoles.add("webUser");
    HashSet expectedRunAsRoles = new HashSet();
    expectedRunAsRoles.add("identitySubstitutionCaller");
    expectedRunAsRoles.add("extraRunAsRole");
    CallerInfo info = new CallerInfo(callerIdentity, runAsIdentity, expectedCallerRoles, expectedRunAsRoles);
    bean.publicMethod(info);
    bean.remove();
}

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//ww  w.  j  a va 2 s .  c  o  m
        environment.put(Context.SECURITY_AUTHENTICATION, "none");

    searchContext = new InitialDirContext(environment);
}