Search in sources :

Example 26 with LdapContext

use of javax.naming.ldap.LdapContext in project jackrabbit-oak by apache.

the class InternalLdapServer method removeMember.

public void removeMember(String groupDN, String memberDN) throws Exception {
    LdapContext ctxt = getWiredContext();
    BasicAttributes attrs = new BasicAttributes();
    attrs.put("member", memberDN);
    ctxt.modifyAttributes(groupDN, DirContext.REMOVE_ATTRIBUTE, attrs);
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) LdapContext(javax.naming.ldap.LdapContext)

Example 27 with LdapContext

use of javax.naming.ldap.LdapContext in project wildfly by wildfly.

the class LdapUrlTestServlet method runSearch.

/**
     * Try to search in LDAP with search base containing URL. Also try to retrieve RequestControls from LdapContext.
     *
     * @param hostname
     * @return
     * @throws Exception
     */
public static String runSearch(final String hostname, boolean testLdapCtx) throws Exception {
    final StringBuilder result = new StringBuilder();
    final String ldapUrl = "ldap://" + (hostname == null ? "localhost" : hostname) + ":10389";
    final Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.PROVIDER_URL, ldapUrl);
    env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
    env.put(Context.SECURITY_CREDENTIALS, "secret");
    final SearchControls ctl = new SearchControls();
    ctl.setReturningAttributes(new String[] { "cn" });
    DirContext dirCtx = null;
    if (testLdapCtx) {
        // LdapContext must also work
        LdapContext ldapCtx = new InitialLdapContext(env, null);
        // next line tests if the LdapContext works
        ldapCtx.getRequestControls();
        dirCtx = ldapCtx;
    } else {
        dirCtx = new InitialDirContext(env);
    }
    final NamingEnumeration<SearchResult> nenum = dirCtx.search(ldapUrl + "/dc=jboss,dc=org", "(uid=jduke)", ctl);
    while (nenum.hasMore()) {
        SearchResult sr = nenum.next();
        Attributes attrs = sr.getAttributes();
        result.append("cn=").append(attrs.get("cn").get());
    }
    dirCtx.close();
    return result.toString();
}
Also used : Hashtable(java.util.Hashtable) InitialLdapContext(javax.naming.ldap.InitialLdapContext) Attributes(javax.naming.directory.Attributes) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LdapContext(javax.naming.ldap.LdapContext)

Example 28 with LdapContext

use of javax.naming.ldap.LdapContext in project zeppelin by apache.

the class ActiveDirectoryGroupRealm method queryForAuthorizationInfo.

/**
   * Builds an {@link org.apache.shiro.authz.AuthorizationInfo} object by querying the active
   * directory LDAP context for the groups that a user is a member of.  The groups are then
   * translated to role names by using the configured {@link #groupRolesMap}.
   * <p/>
   * This implementation expects the <tt>principal</tt> argument to be a String username.
   * <p/>
   * Subclasses can override this method to determine authorization data (roles, permissions, etc)
   * in a more complex way.  Note that this default implementation does not support permissions,
   * only roles.
   *
   * @param principals         the principal of the Subject whose account is being retrieved.
   * @param ldapContextFactory the factory used to create LDAP connections.
   * @return the AuthorizationInfo for the given Subject principal.
   * @throws NamingException if an error occurs when searching the LDAP server.
   */
protected AuthorizationInfo queryForAuthorizationInfo(PrincipalCollection principals, LdapContextFactory ldapContextFactory) throws NamingException {
    String username = (String) getAvailablePrincipal(principals);
    // Perform context search
    LdapContext ldapContext = ldapContextFactory.getSystemLdapContext();
    Set<String> roleNames;
    try {
        roleNames = getRoleNamesForUser(username, ldapContext);
    } finally {
        LdapUtils.closeContext(ldapContext);
    }
    return buildAuthorizationInfo(roleNames);
}
Also used : LdapContext(javax.naming.ldap.LdapContext)

Example 29 with LdapContext

use of javax.naming.ldap.LdapContext in project zeppelin by apache.

the class ActiveDirectoryGroupRealm method queryForAuthenticationInfo.

/**
   * 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/>
   * 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.indexOf('@') < 0) {
            userPrincipalName = upToken.getUsername() + this.principalSuffix;
        }
        ctx = ldapContextFactory.getLdapContext(userPrincipalName, upToken.getPassword());
    } finally {
        LdapUtils.closeContext(ctx);
    }
    return buildAuthenticationInfo(upToken.getUsername(), upToken.getPassword());
}
Also used : LdapContext(javax.naming.ldap.LdapContext) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken)

Example 30 with LdapContext

use of javax.naming.ldap.LdapContext in project zeppelin by apache.

the class LdapGroupRealm method queryForAuthorizationInfo.

public AuthorizationInfo queryForAuthorizationInfo(PrincipalCollection principals, LdapContextFactory ldapContextFactory) throws NamingException {
    String username = (String) getAvailablePrincipal(principals);
    LdapContext ldapContext = ldapContextFactory.getSystemLdapContext();
    Set<String> roleNames = getRoleNamesForUser(username, ldapContext, getUserDnTemplate());
    return new SimpleAuthorizationInfo(roleNames);
}
Also used : SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) LdapContext(javax.naming.ldap.LdapContext)

Aggregations

LdapContext (javax.naming.ldap.LdapContext)43 NamingException (javax.naming.NamingException)14 SearchResult (javax.naming.directory.SearchResult)13 NamingEnumeration (javax.naming.NamingEnumeration)10 SearchControls (javax.naming.directory.SearchControls)9 InitialLdapContext (javax.naming.ldap.InitialLdapContext)9 IOException (java.io.IOException)8 Attributes (javax.naming.directory.Attributes)8 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)6 Control (javax.naming.ldap.Control)6 Hashtable (java.util.Hashtable)5 SortControl (javax.naming.ldap.SortControl)4 JndiLdapContextFactory (org.apache.shiro.realm.ldap.JndiLdapContextFactory)4 Attribute (javax.naming.directory.Attribute)3 BasicAttribute (javax.naming.directory.BasicAttribute)3 BasicAttributes (javax.naming.directory.BasicAttributes)3 DirContext (javax.naming.directory.DirContext)3 StartTlsRequest (javax.naming.ldap.StartTlsRequest)3 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)3