Search in sources :

Example 16 with SearchResultEntry

use of com.unboundid.ldap.sdk.SearchResultEntry in project gitblit by gitblit.

the class LdapAuthProvider method authenticate.

@Override
public UserModel authenticate(String username, char[] password) {
    String simpleUsername = getSimpleUsername(username);
    LdapConnection ldapConnection = new LdapConnection(settings);
    if (ldapConnection.connect()) {
        // Try to bind either to the "manager" account,
        // or directly to the DN of the user logging in, if realm.ldap.bindpattern is configured.
        String passwd = new String(password);
        BindResult bindResult = null;
        String bindPattern = settings.getString(Keys.realm.ldap.bindpattern, "");
        if (!StringUtils.isEmpty(bindPattern)) {
            bindResult = ldapConnection.bind(bindPattern, simpleUsername, passwd);
        } else {
            bindResult = ldapConnection.bind();
        }
        if (bindResult == null) {
            ldapConnection.close();
            return null;
        }
        try {
            // Find the logging in user's DN
            SearchResult result = ldapConnection.searchUser(simpleUsername);
            if (result != null && result.getEntryCount() == 1) {
                SearchResultEntry loggingInUser = result.getSearchEntries().get(0);
                String loggingInUserDN = loggingInUser.getDN();
                if (ldapConnection.isAuthenticated(loggingInUserDN, passwd)) {
                    logger.debug("LDAP authenticated: " + username);
                    UserModel user = null;
                    synchronized (this) {
                        user = userManager.getUserModel(simpleUsername);
                        if (user == null) {
                            // create user object for new authenticated user
                            user = new UserModel(simpleUsername);
                        }
                        // create a user cookie
                        setCookie(user);
                        if (!supportsTeamMembershipChanges()) {
                            getTeamsFromLdap(ldapConnection, simpleUsername, loggingInUser, user);
                        }
                        // Get User Attributes
                        setUserAttributes(user, loggingInUser);
                        // Push the ldap looked up values to backing file
                        updateUser(user);
                        if (!supportsTeamMembershipChanges()) {
                            for (TeamModel userTeam : user.teams) {
                                // Is this an administrative team?
                                setAdminAttribute(userTeam);
                                updateTeam(userTeam);
                            }
                        }
                    }
                    return user;
                }
            }
        } finally {
            ldapConnection.close();
        }
    }
    return null;
}
Also used : UserModel(com.gitblit.models.UserModel) TeamModel(com.gitblit.models.TeamModel) BindResult(com.unboundid.ldap.sdk.BindResult) SearchResult(com.unboundid.ldap.sdk.SearchResult) LdapConnection(com.gitblit.ldap.LdapConnection) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 17 with SearchResultEntry

use of com.unboundid.ldap.sdk.SearchResultEntry in project gitblit by gitblit.

the class LdapAuthProvider method getTeamsFromLdap.

private void getTeamsFromLdap(LdapConnection ldapConnection, String simpleUsername, SearchResultEntry loggingInUser, UserModel user) {
    String loggingInUserDN = loggingInUser.getDN();
    // Clear the users team memberships - we're going to get them from LDAP
    user.teams.clear();
    String groupBase = settings.getString(Keys.realm.ldap.groupBase, "");
    String groupMemberPattern = settings.getString(Keys.realm.ldap.groupMemberPattern, "(&(objectClass=group)(member=${dn}))");
    groupMemberPattern = StringUtils.replace(groupMemberPattern, "${dn}", LdapConnection.escapeLDAPSearchFilter(loggingInUserDN));
    groupMemberPattern = StringUtils.replace(groupMemberPattern, "${username}", LdapConnection.escapeLDAPSearchFilter(simpleUsername));
    // Fill in attributes into groupMemberPattern
    for (Attribute userAttribute : loggingInUser.getAttributes()) {
        groupMemberPattern = StringUtils.replace(groupMemberPattern, "${" + userAttribute.getName() + "}", LdapConnection.escapeLDAPSearchFilter(userAttribute.getValue()));
    }
    SearchResult teamMembershipResult = searchTeamsInLdap(ldapConnection, groupBase, true, groupMemberPattern, Arrays.asList("cn"));
    if (teamMembershipResult != null && teamMembershipResult.getEntryCount() > 0) {
        for (int i = 0; i < teamMembershipResult.getEntryCount(); i++) {
            SearchResultEntry teamEntry = teamMembershipResult.getSearchEntries().get(i);
            String teamName = teamEntry.getAttribute("cn").getValue();
            TeamModel teamModel = userManager.getTeamModel(teamName);
            if (teamModel == null) {
                teamModel = createTeamFromLdap(teamEntry);
            }
            user.teams.add(teamModel);
            teamModel.addUser(user.getName());
        }
    }
}
Also used : TeamModel(com.gitblit.models.TeamModel) Attribute(com.unboundid.ldap.sdk.Attribute) SearchResult(com.unboundid.ldap.sdk.SearchResult) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 18 with SearchResultEntry

use of com.unboundid.ldap.sdk.SearchResultEntry in project gitblit by gitblit.

the class LdapAuthProvider method getEmptyTeamsFromLdap.

private void getEmptyTeamsFromLdap(LdapConnection ldapConnection) {
    logger.info("Start fetching empty teams from ldap.");
    String groupBase = settings.getString(Keys.realm.ldap.groupBase, "");
    String groupMemberPattern = settings.getString(Keys.realm.ldap.groupEmptyMemberPattern, "(&(objectClass=group)(!(member=*)))");
    SearchResult teamMembershipResult = searchTeamsInLdap(ldapConnection, groupBase, true, groupMemberPattern, null);
    if (teamMembershipResult != null && teamMembershipResult.getEntryCount() > 0) {
        for (int i = 0; i < teamMembershipResult.getEntryCount(); i++) {
            SearchResultEntry teamEntry = teamMembershipResult.getSearchEntries().get(i);
            if (!teamEntry.hasAttribute("member")) {
                String teamName = teamEntry.getAttribute("cn").getValue();
                TeamModel teamModel = userManager.getTeamModel(teamName);
                if (teamModel == null) {
                    teamModel = createTeamFromLdap(teamEntry);
                    setAdminAttribute(teamModel);
                    userManager.updateTeamModel(teamModel);
                }
            }
        }
    }
    logger.info("Finished fetching empty teams from ldap.");
}
Also used : TeamModel(com.gitblit.models.TeamModel) SearchResult(com.unboundid.ldap.sdk.SearchResult) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 19 with SearchResultEntry

use of com.unboundid.ldap.sdk.SearchResultEntry in project gitblit by gitblit.

the class LdapConnectionTest method testSearch.

@Test
public void testSearch() throws LDAPException {
    LdapConnection conn = new LdapConnection(settings);
    try {
        assertTrue(conn.connect());
        BindResult br = conn.bind();
        assertNotNull(br);
        SearchResult result;
        SearchResultEntry entry;
        result = conn.search(ACCOUNT_BASE, false, "(CN=UserOne)", null);
        assertNotNull(result);
        assertEquals(1, result.getEntryCount());
        entry = result.getSearchEntries().get(0);
        assertEquals("CN=UserOne,OU=US," + ACCOUNT_BASE, entry.getDN());
        result = conn.search(ACCOUNT_BASE, true, "(&(CN=UserOne)(surname=One))", null);
        assertNotNull(result);
        assertEquals(1, result.getEntryCount());
        entry = result.getSearchEntries().get(0);
        assertEquals("CN=UserOne,OU=US," + ACCOUNT_BASE, entry.getDN());
        result = conn.search(ACCOUNT_BASE, true, "(&(CN=UserOne)(surname=Two))", null);
        assertNotNull(result);
        assertEquals(0, result.getEntryCount());
        result = conn.search(ACCOUNT_BASE, true, "(surname=Two)", Arrays.asList("givenName", "surname"));
        assertNotNull(result);
        assertEquals(1, result.getEntryCount());
        entry = result.getSearchEntries().get(0);
        assertEquals("CN=UserTwo,OU=US," + ACCOUNT_BASE, entry.getDN());
        assertEquals(2, entry.getAttributes().size());
        assertEquals("User", entry.getAttributeValue("givenName"));
        assertEquals("Two", entry.getAttributeValue("surname"));
        result = conn.search(ACCOUNT_BASE, true, "(personalTitle=Mr*)", null);
        assertNotNull(result);
        assertEquals(3, result.getEntryCount());
        ArrayList<String> names = new ArrayList<>(3);
        names.add(result.getSearchEntries().get(0).getAttributeValue("surname"));
        names.add(result.getSearchEntries().get(1).getAttributeValue("surname"));
        names.add(result.getSearchEntries().get(2).getAttributeValue("surname"));
        assertTrue(names.contains("One"));
        assertTrue(names.contains("Two"));
        assertTrue(names.contains("Three"));
    } finally {
        conn.close();
    }
}
Also used : ArrayList(java.util.ArrayList) BindResult(com.unboundid.ldap.sdk.BindResult) SearchResult(com.unboundid.ldap.sdk.SearchResult) LdapConnection(com.gitblit.ldap.LdapConnection) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry) Test(org.junit.Test)

Example 20 with SearchResultEntry

use of com.unboundid.ldap.sdk.SearchResultEntry in project gitblit by gitblit.

the class LdapConnectionTest method testSearchRequest.

@Test
public void testSearchRequest() throws LDAPException {
    LdapConnection conn = new LdapConnection(settings);
    try {
        assertTrue(conn.connect());
        BindResult br = conn.bind();
        assertNotNull(br);
        SearchRequest req;
        SearchResult result;
        SearchResultEntry entry;
        req = new SearchRequest(ACCOUNT_BASE, SearchScope.BASE, "(CN=UserOne)");
        result = conn.search(req);
        assertNotNull(result);
        assertEquals(0, result.getEntryCount());
        req = new SearchRequest(ACCOUNT_BASE, SearchScope.ONE, "(CN=UserTwo)");
        result = conn.search(req);
        assertNotNull(result);
        assertEquals(0, result.getEntryCount());
        req = new SearchRequest(ACCOUNT_BASE, SearchScope.SUB, "(CN=UserThree)");
        result = conn.search(req);
        assertNotNull(result);
        assertEquals(1, result.getEntryCount());
        entry = result.getSearchEntries().get(0);
        assertEquals("CN=UserThree,OU=Canada," + ACCOUNT_BASE, entry.getDN());
        req = new SearchRequest(ACCOUNT_BASE, SearchScope.SUBORDINATE_SUBTREE, "(CN=UserFour)");
        result = conn.search(req);
        assertNotNull(result);
        assertEquals(1, result.getEntryCount());
        entry = result.getSearchEntries().get(0);
        assertEquals("CN=UserFour,OU=Canada," + ACCOUNT_BASE, entry.getDN());
    } finally {
        conn.close();
    }
}
Also used : SearchRequest(com.unboundid.ldap.sdk.SearchRequest) BindResult(com.unboundid.ldap.sdk.BindResult) SearchResult(com.unboundid.ldap.sdk.SearchResult) LdapConnection(com.gitblit.ldap.LdapConnection) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry) Test(org.junit.Test)

Aggregations

SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)31 SearchResult (com.unboundid.ldap.sdk.SearchResult)22 ArrayList (java.util.ArrayList)13 LDAPException (com.unboundid.ldap.sdk.LDAPException)11 SearchException (org.gluu.persist.exception.operation.SearchException)10 MappingException (org.gluu.persist.exception.mapping.MappingException)9 ConnectionException (org.gluu.persist.exception.operation.ConnectionException)9 SearchRequest (com.unboundid.ldap.sdk.SearchRequest)7 EntryPersistenceException (org.gluu.persist.exception.mapping.EntryPersistenceException)7 LdapConnection (com.gitblit.ldap.LdapConnection)6 ParseException (java.text.ParseException)6 AuthenticationException (org.gluu.persist.exception.operation.AuthenticationException)6 SearchScopeException (org.gluu.persist.exception.operation.SearchScopeException)6 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)5 BindResult (com.unboundid.ldap.sdk.BindResult)5 LDAPSearchException (com.unboundid.ldap.sdk.LDAPSearchException)5 TeamModel (com.gitblit.models.TeamModel)4 Attribute (com.unboundid.ldap.sdk.Attribute)4 LinkedList (java.util.LinkedList)4 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)3