Example usage for org.apache.shiro.realm.ldap LdapContextFactory getLdapContext

List of usage examples for org.apache.shiro.realm.ldap LdapContextFactory getLdapContext

Introduction

In this page you can find the example usage for org.apache.shiro.realm.ldap LdapContextFactory getLdapContext.

Prototype

LdapContext getLdapContext(Object principal, Object credentials) throws NamingException;

Source Link

Document

Creates (or retrieves from a pool) an LdapContext connection bound using the specified principal and credentials.

Usage

From source file:org.apache.zeppelin.realm.ActiveDirectoryGroupRealm.java

License:Apache License

/**
 * Builds an {@link AuthenticationInfo} object by querying the active directory LDAP context for
 * the specified username.  This method binds to the LDAP server using the provided username
 * and password - which if successful, indicates that the password is correct.
 * <p/>//from  w  ww .j av a 2 s.co m
 * This method can be overridden by subclasses to query the LDAP server in a more complex way.
 *
 * @param token              the authentication token provided by the user.
 * @param ldapContextFactory the factory used to build connections to the LDAP server.
 * @return an {@link AuthenticationInfo} instance containing information retrieved from LDAP.
 * @throws NamingException if any LDAP errors occur during the search.
 */
protected AuthenticationInfo queryForAuthenticationInfo(AuthenticationToken token,
        LdapContextFactory ldapContextFactory) throws NamingException {
    UsernamePasswordToken upToken = (UsernamePasswordToken) token;

    // Binds using the username and password provided by the user.
    LdapContext ctx = null;
    try {
        String userPrincipalName = upToken.getUsername();
        if (!isValidPrincipalName(userPrincipalName)) {
            return null;
        }
        if (this.principalSuffix != null && userPrincipalName.indexOf('@') < 0) {
            userPrincipalName = upToken.getUsername() + this.principalSuffix;
        }
        ctx = ldapContextFactory.getLdapContext(userPrincipalName, upToken.getPassword());
    } finally {
        LdapUtils.closeContext(ctx);
    }

    return buildAuthenticationInfo(upToken.getUsername(), upToken.getPassword());
}

From source file:org.apache.zeppelin.server.ActiveDirectoryGroupRealm.java

License:Apache License

/**
 * Builds an {@link AuthenticationInfo} object by querying the active directory LDAP context for
 * the specified username.  This method binds to the LDAP server using the provided username
 * and password - which if successful, indicates that the password is correct.
 * <p/>//from   w  w w  .j ava 2s  . c  o m
 * This method can be overridden by subclasses to query the LDAP server in a more complex way.
 *
 * @param token              the authentication token provided by the user.
 * @param ldapContextFactory the factory used to build connections to the LDAP server.
 * @return an {@link AuthenticationInfo} instance containing information retrieved from LDAP.
 * @throws NamingException if any LDAP errors occur during the search.
 */
protected AuthenticationInfo queryForAuthenticationInfo(AuthenticationToken token,
        LdapContextFactory ldapContextFactory) throws NamingException {

    UsernamePasswordToken upToken = (UsernamePasswordToken) token;

    // Binds using the username and password provided by the user.
    LdapContext ctx = null;
    try {
        String userPrincipalName = upToken.getUsername();
        if (userPrincipalName == null) {
            return null;
        }
        if (this.principalSuffix != null) {
            userPrincipalName = upToken.getUsername() + this.principalSuffix;
        }
        ctx = ldapContextFactory.getLdapContext(userPrincipalName, upToken.getPassword());
    } finally {
        LdapUtils.closeContext(ctx);
    }

    return buildAuthenticationInfo(upToken.getUsername(), upToken.getPassword());
}

From source file:org.ow2.proactive.iam.core.realms.LdapRealm.java

License:Open Source License

/**
 * This implementation opens an LDAP connection using the token's
 * {@link #getLdapPrincipal(org.apache.shiro.authc.AuthenticationToken) discovered principal} and provided
 * {@link AuthenticationToken#getCredentials() credentials}.  If the connection opens successfully, the
 * authentication attempt is immediately considered successful and a new
 * {@link AuthenticationInfo} instance is
 * {@link #createAuthenticationInfo(org.apache.shiro.authc.AuthenticationToken, Object, Object, javax.naming.ldap.LdapContext) created}
 * and returned.  If the connection cannot be opened, either because LDAP authentication failed or some other
 * JNDI problem, an {@link NamingException} will be thrown.
 *
 * @param token              the submitted authentication token that triggered the authentication attempt.
 * @param ldapContextFactory factory used to retrieve LDAP connections.
 * @return an {@link AuthenticationInfo} instance representing the authenticated user's information.
 * @throws NamingException if any LDAP errors occur.
 *///from  ww  w  .  ja  v  a 2 s  . c  om
protected AuthenticationInfo queryForAuthenticationInfo(AuthenticationToken token,
        LdapContextFactory ldapContextFactory) throws NamingException {

    Object principal = token.getPrincipal();
    Object credentials = token.getCredentials();

    log.debug("Authenticating user '{}' through LDAP", principal);

    principal = getLdapPrincipal(token);

    LdapContext ctx = null;
    try {
        ctx = ldapContextFactory.getLdapContext(principal, credentials);
        //context was opened successfully, which means their credentials were valid.  Return the AuthenticationInfo:
        return createAuthenticationInfo(token, principal, credentials, ctx);
    } finally {
        LdapUtils.closeContext(ctx);
    }
}

From source file:org.owasp.dependencytrack.auth.ActiveDirectoryAuthenticationRealm.java

License:Open Source License

/**
 * Builds an {@link org.apache.shiro.authc.AuthenticationInfo} object by querying the active directory LDAP context for the
 * specified username.  This method binds to the LDAP server using the provided username and password -
 * which if successful, indicates that the password is correct.
 * <p/>//from ww w  .  j  a v a  2  s  .c om
 *
 * @param token              the authentication token provided by the user.
 * @param ldapContextFactory the factory used to build connections to the LDAP server.
 * @return an {@link org.apache.shiro.authc.AuthenticationInfo} instance containing information retrieved from LDAP.
 * @throws javax.naming.NamingException if any LDAP errors occur during the search.
 */
protected AuthenticationInfo queryForAuthenticationInfo(AuthenticationToken token,
        LdapContextFactory ldapContextFactory) throws NamingException {
    final UsernamePasswordToken upToken = (UsernamePasswordToken) token;
    LdapContext ctx = null;
    try {
        ctx = ldapContextFactory.getLdapContext(upToken.getUsername(), String.valueOf(upToken.getPassword()));
    } finally {
        LdapUtils.closeContext(ctx);
    }
    return new SimpleAuthenticationInfo(upToken.getUsername(), upToken.getPassword(), getName());
}

From source file:org.sonatype.nexus.ldap.internal.realms.LdapAuthenticator.java

License:Open Source License

private void checkPasswordUsingBind(LdapContextFactory ldapContextFactory, String user, String pass)
        throws AuthenticationException {
    LdapContext ctx = null;/*from   www. j  ava 2 s .  c o m*/
    try {
        ctx = ldapContextFactory.getLdapContext(user, pass);
    } catch (javax.naming.AuthenticationException e) {
        throw new AuthenticationException("User '" + user + "' cannot be authenticated.", e);
    } catch (NamingException e) {
        throw new AuthenticationException("User '" + user + "' cannot be authenticated.", e);
    } finally {
        LdapUtils.closeContext(ctx);
    }
}