Example usage for org.springframework.security.ldap.ppolicy PasswordPolicyControlExtractor extractControl

List of usage examples for org.springframework.security.ldap.ppolicy PasswordPolicyControlExtractor extractControl

Introduction

In this page you can find the example usage for org.springframework.security.ldap.ppolicy PasswordPolicyControlExtractor extractControl.

Prototype

public static PasswordPolicyResponseControl extractControl(DirContext dirCtx) 

Source Link

Usage

From source file:de.thm.arsnova.security.CustomBindAuthenticator.java

private DirContextOperations bindWithDn(String userDnStr, String username, String password) {
    BaseLdapPathContextSource ctxSource = (BaseLdapPathContextSource) getContextSource();
    DistinguishedName userDn = new DistinguishedName(userDnStr);
    DistinguishedName fullDn = new DistinguishedName(userDn);
    fullDn.prepend(ctxSource.getBaseLdapPath());

    logger.debug("Attempting to bind as " + fullDn);

    DirContext ctx = null;/*from   w w w.  ja  v  a2 s .c  o m*/
    try {
        ctx = getContextSource().getContext(fullDn.toString(), password);
        // Check for password policy control
        PasswordPolicyControl ppolicy = PasswordPolicyControlExtractor.extractControl(ctx);

        logger.debug("Retrieving attributes for " + userDn + ": " + Arrays.toString(getUserAttributes()));

        //Attributes attrs = ctx.getAttributes(userDn, getUserAttributes());

        //logger.debug("Attributes retrieved: " + attrs.size());

        DirContextAdapter result = new DirContextAdapter(null, userDn, ctxSource.getBaseLdapPath());

        if (ppolicy != null) {
            result.setAttributeValue(ppolicy.getID(), ppolicy);
        }

        return result;
    } catch (Exception e) {
        // This will be thrown if an invalid user name is used and the method may
        // be called multiple times to try different names, so we trap the exception
        // unless a subclass wishes to implement more specialized behaviour.
        if ((e instanceof org.springframework.ldap.AuthenticationException)
                || (e instanceof org.springframework.ldap.OperationNotSupportedException)) {
            handleBindException(userDnStr, username, e);
        } else {
            throw e;
        }
    } finally {
        LdapUtils.closeContext(ctx);
    }

    return null;
}

From source file:sk.lazyman.gizmo.security.SimpleBindAunthenticator.java

private DirContextOperations bindWithDn(String userDnStr, String username, String password) {
    BaseLdapPathContextSource ctxSource = (BaseLdapPathContextSource) getContextSource();
    DistinguishedName userDn = new DistinguishedName(userDnStr);
    DistinguishedName fullDn = new DistinguishedName(userDn);
    fullDn.prepend(ctxSource.getBaseLdapPath());

    LOG.debug("Attempting to bind as " + fullDn);

    DirContext ctx = null;// w  w  w .  j  av  a  2s. c  o  m
    try {
        ctx = getContextSource().getContext(fullDn.toString(), password);
        // Check for password policy control
        PasswordPolicyControl ppolicy = PasswordPolicyControlExtractor.extractControl(ctx);

        LOG.debug("Retrieving attributes...");
        DirContext readOnlyCtx = getContextSource().getReadOnlyContext();
        Attributes attrs = readOnlyCtx.getAttributes(userDn, getUserAttributes());

        DirContextAdapter result = new DirContextAdapter(attrs, userDn, ctxSource.getBaseLdapPath());
        if (ppolicy != null) {
            result.setAttributeValue(ppolicy.getID(), ppolicy);
        }

        return result;
    } catch (NamingException e) {
        // This will be thrown if an invalid user name is used and the method may
        // be called multiple times to try different names, so we trap the exception
        // unless a subclass wishes to implement more specialized behaviour.
        if ((e instanceof org.springframework.ldap.AuthenticationException)
                || (e instanceof org.springframework.ldap.OperationNotSupportedException)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Failed to bind as " + userDn + ": " + e);
            }
        } else {
            throw e;
        }
    } catch (javax.naming.NamingException e) {
        throw LdapUtils.convertLdapException(e);
    } finally {
        LdapUtils.closeContext(ctx);
    }

    return null;
}

From source file:org.geoserver.security.ldap.GeoserverLdapBindAuthenticator.java

/**
 * If userFilter is defined we extract user data using the filter and
 * dnPattern (if defined) to transform username for authentication.
 * //from  w  ww  . j a  va2  s .co m
 * @param authentication
 * @return
 */
protected DirContextOperations authenticateUsingFilter(Authentication authentication) {
    DirContextOperations user = null;
    Assert.isInstanceOf(UsernamePasswordAuthenticationToken.class, authentication,
            "Can only process UsernamePasswordAuthenticationToken objects");

    String username = authentication.getName();
    String password = (String) authentication.getCredentials();
    // format given username if required
    if (userFormat != null && !userFormat.equals("")) {
        username = MessageFormat.format(userFormat, username);
    }
    if (!StringUtils.hasLength(password)) {
        logger.debug("Rejecting empty password for user " + username);
        throw new BadCredentialsException(
                messages.getMessage("BindAuthenticator.emptyPassword", "Empty Password"));
    }

    DirContext ctx = null;
    String userDnStr = "";
    try {
        ctx = getContextSource().getContext(username, password);

        // Check for password policy control
        PasswordPolicyControl ppolicy = PasswordPolicyControlExtractor.extractControl(ctx);

        logger.debug("Retrieving user object using filter...");
        SearchControls searchCtls = new SearchControls();
        searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

        user = SpringSecurityLdapTemplate.searchForSingleEntryInternal(ctx, searchCtls, "", userFilter,
                new Object[] { username });
        userDnStr = user.getDn().toString();
        if (ppolicy != null) {
            user.setAttributeValue(ppolicy.getID(), ppolicy);
        }

    } catch (NamingException e) {
        // This will be thrown if an invalid user name is used and the
        // method may
        // be called multiple times to try different names, so we trap the
        // exception
        // unless a subclass wishes to implement more specialized behaviour.
        if ((e instanceof org.springframework.ldap.AuthenticationException)
                || (e instanceof org.springframework.ldap.OperationNotSupportedException)) {
            handleBindException(userDnStr, username, e);
        } else {
            throw e;
        }
    } catch (javax.naming.NamingException e) {
        throw LdapUtils.convertLdapException(e);
    } finally {
        LdapUtils.closeContext(ctx);
    }

    if (user == null) {
        throw new BadCredentialsException(
                messages.getMessage("BindAuthenticator.badCredentials", "Bad credentials"));
    }

    return user;
}

From source file:org.springframework.security.ldap.authentication.BindAuthenticator.java

private DirContextOperations bindWithDn(String userDnStr, String username, String password, Attributes attrs) {
    BaseLdapPathContextSource ctxSource = (BaseLdapPathContextSource) getContextSource();
    DistinguishedName userDn = new DistinguishedName(userDnStr);
    DistinguishedName fullDn = new DistinguishedName(userDn);
    fullDn.prepend(ctxSource.getBaseLdapPath());

    logger.debug("Attempting to bind as " + fullDn);

    DirContext ctx = null;/*from ww  w.j  a  v  a 2 s  .c  om*/
    try {
        ctx = getContextSource().getContext(fullDn.toString(), password);
        // Check for password policy control
        PasswordPolicyControl ppolicy = PasswordPolicyControlExtractor.extractControl(ctx);

        logger.debug("Retrieving attributes...");
        if (attrs == null || attrs.size() == 0) {
            attrs = ctx.getAttributes(userDn, getUserAttributes());
        }

        DirContextAdapter result = new DirContextAdapter(attrs, userDn, ctxSource.getBaseLdapPath());

        if (ppolicy != null) {
            result.setAttributeValue(ppolicy.getID(), ppolicy);
        }

        return result;
    } catch (NamingException e) {
        // This will be thrown if an invalid user name is used and the method may
        // be called multiple times to try different names, so we trap the exception
        // unless a subclass wishes to implement more specialized behaviour.
        if ((e instanceof org.springframework.ldap.AuthenticationException)
                || (e instanceof org.springframework.ldap.OperationNotSupportedException)) {
            handleBindException(userDnStr, username, e);
        } else {
            throw e;
        }
    } catch (javax.naming.NamingException e) {
        throw LdapUtils.convertLdapException(e);
    } finally {
        LdapUtils.closeContext(ctx);
    }

    return null;
}