Example usage for javax.naming Context SECURITY_CREDENTIALS

List of usage examples for javax.naming Context SECURITY_CREDENTIALS

Introduction

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

Prototype

String SECURITY_CREDENTIALS

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

Click Source Link

Document

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

Usage

From source file:org.olat.ldap.LDAPLoginManagerImpl.java

/**
 * Connect to the LDAP server with System DN and Password Configuration: LDAP URL = olatextconfig.xml (property=ldapURL) System DN = olatextconfig.xml
 * (property=ldapSystemDN) System PW = olatextconfig.xml (property=ldapSystemPW)
 * /*from   w  w  w .j  av  a2 s .  com*/
 * @return The LDAP connection (LdapContext) or NULL if connect fails
 * @throws NamingException
 */
public LdapContext bindSystem() {
    // set LDAP connection attributes
    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, LDAPLoginModule.getLdapUrl());
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, LDAPLoginModule.getLdapSystemDN());
    env.put(Context.SECURITY_CREDENTIALS, LDAPLoginModule.getLdapSystemPW());

    // check ssl
    if (LDAPLoginModule.isSslEnabled()) {
        enableSSL(env);
    }

    try {
        final InitialLdapContext ctx = new InitialLdapContext(env, new Control[] {});
        ctx.getConnectControls();
        return ctx;
    } catch (final NamingException e) {
        logError("NamingException when trying to bind system with DN::" + LDAPLoginModule.getLdapSystemDN()
                + " and PW::" + LDAPLoginModule.getLdapSystemPW() + " on URL::" + LDAPLoginModule.getLdapUrl(),
                e);
        return null;
    } catch (final Exception e) {
        logError("Exception when trying to bind system with DN::" + LDAPLoginModule.getLdapSystemDN()
                + " and PW::" + LDAPLoginModule.getLdapSystemPW() + " on URL::" + LDAPLoginModule.getLdapUrl(),
                e);
        return null;
    }

}

From source file:com.wfp.utils.LDAPUtils.java

/**
 * Overloaded method for getting the LDAP COntext based on the host,username, password
 * @param host//w ww  .  j av  a2  s .c  om
 * @param adminName
 * @param adminPassword
 * @return
 * @throws NamingException
 */
@SuppressWarnings("unchecked")
public static LdapContext getLDAPContext(String host, String adminName, String adminPassword)
        throws NamingException {
    //Logger.info("Creating LDAP Context", LDAPUtils.class);
    Hashtable props = System.getProperties();
    props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    props.put(Context.SECURITY_AUTHENTICATION, LDAP_SECURITY_AUTHENTICATION);
    props.put(Context.SECURITY_PRINCIPAL, adminName);
    props.put(Context.SECURITY_CREDENTIALS, adminPassword);
    props.put(Context.PROVIDER_URL, host);
    if (!StringUtils.isNull(LDAPConfigUtils.getTrustStorePath())) {
        System.setProperty("javax.net.ssl.trustStore", LDAPConfigUtils.getTrustStorePath());
        props.put(Context.SECURITY_PROTOCOL, "ssl");
    }
    //Logger.info("Completed creating LDAP Context for host ["+host+"]", LDAPUtils.class);
    return (new InitialLdapContext(props, null));
}

From source file:org.webterm.core.plugin.authentication.LdapAuthentication.java

@Override
public void init() {
    LOG.info("Initializing LDAP authentication..."); //$NON-NLS-1$

    try {//  www . j  a v a  2 s .  com
        final ConfigurationReader config = ConfigurationReader.getInstance();
        final String serverName = config.getApplicationProperty(CONFIG_SERVER_NAME);
        final String serverPort = config.getApplicationProperty(CONFIG_SERVER_PORT);
        final String bindDn = config.getApplicationProperty(CONFIG_BIND_DN);
        final String bindPwd = config.getApplicationProperty(CONFIG_BIND_PWD);
        this.baseDn = config.getApplicationProperty(CONFIG_BASE_DN);
        this.attrUser = config.getApplicationProperty(CONFIG_ATTR_USER);
        this.attrPwd = config.getApplicationProperty(CONFIG_ATTR_PWD);
        this.checkMethode = this.map.get(config.getApplicationProperty(CONFIG_PASSWORD_ENCODE));
        if (this.checkMethode == null) {
            LOG.fatal("unknown method: " + config.getApplicationProperty(CONFIG_PASSWORD_ENCODE)); //$NON-NLS-1$
        }

        final Hashtable<String, String> ldapEnv = new Hashtable<String, String>(); // NOPMD - HashTable is needed
        ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); //$NON-NLS-1$
        ldapEnv.put(Context.PROVIDER_URL, "ldap://" + serverName + ":" + serverPort); //$NON-NLS-1$ //$NON-NLS-2$
        ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple");//$NON-NLS-1$
        ldapEnv.put(Context.SECURITY_PRINCIPAL, bindDn);
        ldapEnv.put(Context.SECURITY_CREDENTIALS, bindPwd);
        this.ldapContext = new InitialDirContext(ldapEnv);
    } catch (Exception ex) {
        LOG.error(ex, ex);
    }
}

From source file:org.apache.jackrabbit.oak.security.authentication.ldap.AbstractServer.java

/**
 * Common code to get an initial context via a simple bind to the
 * server over the wire using the SUN JNDI LDAP provider. Do not use
 * this method until after the setUp() method is called to start the
 * server otherwise it will fail.//w  w w . java2s  .co m
 *
 * @param bindPrincipalDn the DN of the principal to bind as
 * @param password        the password of the bind principal
 * @return an LDAP context as the the administrator to the rootDSE
 * @throws NamingException if the server cannot be contacted
 */
protected LdapContext getWiredContext(String bindPrincipalDn, String password) throws Exception {
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, CTX_FACTORY);
    env.put(Context.PROVIDER_URL, "ldap://localhost:" + port);
    env.put(Context.SECURITY_PRINCIPAL, bindPrincipalDn);
    env.put(Context.SECURITY_CREDENTIALS, password);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    return new InitialLdapContext(env, null);
}

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

public void testGroupMemberMethod() throws Exception {
    log.debug("+++ testGroupMemberMethod()");
    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.groupMemberMethod(info);//from   w ww.  j  a va 2  s  .c o  m
    bean.remove();
}

From source file:com.evolveum.midpoint.pwdfilter.opendj.PasswordPusher.java

private void readConfig() throws InitializationException {

    String configFile = "/opt/midpoint/opendj-pwdpusher.xml";
    if (System.getProperty("config") != null) {
        configFile = System.getProperty("config");
    }//from   w  ww  . j a v  a2  s  .c o m

    File f = new File(configFile);
    if (!f.exists() || !f.canRead()) {
        throw new IllegalArgumentException("Config file " + configFile + " does not exist or is not readable");
    }

    try {
        XMLConfiguration config = new XMLConfiguration(f);

        String notifierDN = "cn=" + config.getString("passwordpusher.statusNotifierName")
                + ",cn=Account Status Notification Handlers";
        String ldapURL = config.getString("passwordpusher.ldapServerURL");
        boolean ldapSSL = config.getBoolean("passwordpusher.ldapServerSSL");
        String ldapUsername = config.getString("passwordpusher.ldapServerUsername");
        String ldapPassword = config.getString("passwordpusher.ldapServerPassword");

        Hashtable<Object, Object> env = new Hashtable<Object, Object>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, ldapURL + "/cn=config");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, ldapUsername);
        env.put(Context.SECURITY_CREDENTIALS, ldapPassword);

        if (ldapSSL) {
            env.put(Context.SECURITY_PROTOCOL, "ssl");
        }

        try {
            DirContext context = new InitialDirContext(env);
            Attributes attr = context.getAttributes(notifierDN);

            this.endPoint = attr.get("ds-cfg-referrals-url").get(0).toString();
            this.username = attr.get("ds-cfg-midpoint-username").get(0).toString();
            this.password = attr.get("ds-cfg-midpoint-password").get(0).toString();
            this.pwdChangeDirectory = attr.get("ds-cfg-midpoint-passwordcachedir").get(0).toString();
        } catch (NamingException ne) {
            throw new InitializationException(
                    ERR_MIDPOINT_PWDSYNC_READING_CONFIG_FROM_LDAP.get(ne.getMessage()), ne);
        }
    } catch (ConfigurationException ce) {
        throw new InitializationException(ERR_MIDPOINT_PWDSYNC_PARSING_XML_CONFIG.get(ce.getMessage()), ce);
    }
}

From source file:org.mule.providers.ldap.util.DSManager.java

/**
 * Sets the contexts for this base class. Values of user and password used
 * to set the respective JNDI properties. These values can be overriden by
 * the overrides properties.//from  w  w  w.  j a v  a  2 s .c  o  m
 * 
 * @param user
 *            the username for authenticating as this user
 * @param passwd
 *            the password of the user
 * @throws NamingException
 *             if there is a failure of any kind
 */
protected void setContexts(String user, String passwd) throws NamingException {
    Hashtable env = new Hashtable(configuration.toJndiEnvironment());
    env.put(Context.SECURITY_PRINCIPAL, user);
    env.put(Context.SECURITY_CREDENTIALS, passwd);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.INITIAL_CONTEXT_FACTORY, ServerContextFactory.class.getName());
    setContexts(env);
}

From source file:iplatform.admin.ui.server.auth.ad.ActiveDirectoryLdapAuthenticationProvider.java

private DirContext bindAsUser(String username, String password) {
    // TODO. add DNS lookup based on domain
    final String bindUrl = url;

    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    String bindPrincipal = createBindPrincipal(username);
    env.put(Context.SECURITY_PRINCIPAL, bindPrincipal);
    env.put(Context.PROVIDER_URL, bindUrl);
    env.put(Context.SECURITY_CREDENTIALS, password);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.OBJECT_FACTORIES, DefaultDirObjectFactory.class.getName());

    try {//from ww w .  jav a2 s  . c  o  m
        return contextFactory.createContext(env);
    } catch (NamingException e) {
        if ((e instanceof AuthenticationException) || (e instanceof OperationNotSupportedException)) {
            handleBindException(bindPrincipal, e);
            throw badCredentials(e);
        } else {
            throw LdapUtils.convertLdapException(e);
        }
    }
}

From source file:py.una.pol.karaku.security.KarakuUserService.java

private InitialDirContext getInitialDirContext(String user, String pass) throws NamingException {

    Map<Object, String> env = new HashMap<Object, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, getServerLocation());

    env.put(Context.SECURITY_PRINCIPAL, user);
    env.put(Context.SECURITY_CREDENTIALS, pass);
    return new InitialDirContext(new Hashtable<Object, String>(env));
}

From source file:org.nuxeo.ecm.directory.ldap.LDAPDirectory.java

/**
 * @return connection parameters to use for all LDAP queries
 *///w ww  .jav  a 2s  . co m
protected Properties computeContextProperties() throws DirectoryException {
    LDAPDirectoryDescriptor ldapDirectoryDesc = getDescriptor();
    // Initialization of LDAP connection parameters from parameters
    // registered in the LDAP "server" extension point
    Properties props = new Properties();
    LDAPServerDescriptor serverConfig = getServer();

    if (null == serverConfig) {
        throw new DirectoryException(
                "LDAP server configuration not found: " + ldapDirectoryDesc.getServerName());
    }

    props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

    /*
     * Get initial connection URLs, dynamic URLs may cause the list to be updated when creating the session
     */
    String ldapUrls = serverConfig.getLdapUrls();
    if (ldapUrls == null) {
        throw new DirectoryException("Server LDAP URL configuration is missing for directory " + getName());
    }
    props.put(Context.PROVIDER_URL, ldapUrls);

    // define how referrals are handled
    if (!getDescriptor().getFollowReferrals()) {
        props.put(Context.REFERRAL, "ignore");
    } else {
        // this is the default mode
        props.put(Context.REFERRAL, "follow");
    }

    /*
     * SSL Connections do not work with connection timeout property
     */
    if (serverConfig.getConnectionTimeout() > -1) {
        if (!serverConfig.useSsl()) {
            props.put("com.sun.jndi.ldap.connect.timeout",
                    Integer.toString(serverConfig.getConnectionTimeout()));
        } else {
            log.warn("SSL connections do not operate correctly"
                    + " when used with the connection timeout parameter, disabling timout");
        }
    }

    String bindDn = serverConfig.getBindDn();
    if (bindDn != null) {
        // Authenticated connection
        props.put(Context.SECURITY_PRINCIPAL, bindDn);
        props.put(Context.SECURITY_CREDENTIALS, serverConfig.getBindPassword());
    }

    if (serverConfig.isPoolingEnabled()) {
        // Enable connection pooling
        props.put("com.sun.jndi.ldap.connect.pool", "true");
        props.put("com.sun.jndi.ldap.connect.pool.protocol", "plain ssl");
        props.put("com.sun.jndi.ldap.connect.pool.authentication", "none simple DIGEST-MD5");
        props.put("com.sun.jndi.ldap.connect.pool.timeout", "1800000"); // 30
        // min
    }

    if (!serverConfig.isVerifyServerCert() && serverConfig.useSsl) {
        props.put("java.naming.ldap.factory.socket",
                "org.nuxeo.ecm.directory.ldap.LDAPDirectory$TrustingSSLSocketFactory");
    }

    return props;
}