Search in sources :

Example 21 with SearchResult

use of com.unboundid.ldap.sdk.SearchResult in project cas by apereo.

the class BaseLdapConsentRepositoryTests method verifyConsentDecisionIsDeleted.

@Test
public void verifyConsentDecisionIsDeleted() throws Exception {
    final ConsentDecision decision = BUILDER.build(SVC, REG_SVC, USER_CN, ATTR);
    decision.setId(1);
    final Modification mod = new Modification(ModificationType.ADD, ATTR_NAME, MAPPER.writeValueAsString(decision));
    assertEquals(ResultCode.SUCCESS, getConnection().modify(USER_DN, mod).getResultCode());
    final ConsentDecision decision2 = BUILDER.build(SVC2, REG_SVC2, USER_CN, ATTR);
    decision2.setId(2);
    final Modification mod2 = new Modification(ModificationType.ADD, ATTR_NAME, MAPPER.writeValueAsString(decision2));
    assertEquals(ResultCode.SUCCESS, getConnection().modify(USER_DN, mod2).getResultCode());
    final ConsentDecision decision3 = BUILDER.build(SVC, REG_SVC, USER2_CN, ATTR);
    decision3.setId(3);
    final Modification mod3 = new Modification(ModificationType.ADD, ATTR_NAME, MAPPER.writeValueAsString(decision3));
    assertEquals(ResultCode.SUCCESS, getConnection().modify(USER2_DN, mod3).getResultCode());
    assertTrue(this.repository.deleteConsentDecision(decision2.getId(), USER_CN));
    final SearchResult r = getConnection().search(USER_DN, SearchScope.SUB, DEF_FILTER, ATTR_NAME);
    assertTrue(r.getEntryCount() > 0);
    assertEquals(1, r.getSearchEntry(USER_DN).getAttributeValues(ATTR_NAME).length);
}
Also used : Modification(com.unboundid.ldap.sdk.Modification) SearchResult(com.unboundid.ldap.sdk.SearchResult) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 22 with SearchResult

use of com.unboundid.ldap.sdk.SearchResult in project cas by apereo.

the class BaseLdapConsentRepositoryTests method cleanDecisions.

@After
public void cleanDecisions() throws Exception {
    final LDAPConnection conn = getConnection();
    final SearchResult res = conn.search(USER_DN, SearchScope.SUB, DEF_FILTER, ATTR_NAME);
    if (res.getEntryCount() != 0 && res.getSearchEntry(USER_DN).hasAttribute(ATTR_NAME)) {
        conn.modify(USER_DN, new Modification(ModificationType.DELETE, ATTR_NAME));
    }
    final SearchResult res2 = conn.search(USER2_DN, SearchScope.SUB, DEF_FILTER, ATTR_NAME);
    if (res2.getEntryCount() != 0 && res2.getSearchEntry(USER2_DN).hasAttribute(ATTR_NAME)) {
        conn.modify(USER2_DN, new Modification(ModificationType.DELETE, ATTR_NAME));
    }
}
Also used : Modification(com.unboundid.ldap.sdk.Modification) SearchResult(com.unboundid.ldap.sdk.SearchResult) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) After(org.junit.After)

Example 23 with SearchResult

use of com.unboundid.ldap.sdk.SearchResult 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 24 with SearchResult

use of com.unboundid.ldap.sdk.SearchResult 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 25 with SearchResult

use of com.unboundid.ldap.sdk.SearchResult 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)

Aggregations

SearchResult (com.unboundid.ldap.sdk.SearchResult)39 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)24 LDAPException (com.unboundid.ldap.sdk.LDAPException)16 SearchRequest (com.unboundid.ldap.sdk.SearchRequest)14 SearchException (org.gluu.persist.exception.operation.SearchException)10 ArrayList (java.util.ArrayList)9 MappingException (org.gluu.persist.exception.mapping.MappingException)9 ConnectionException (org.gluu.persist.exception.operation.ConnectionException)8 Test (org.junit.Test)7 LdapConnection (com.gitblit.ldap.LdapConnection)6 EntryPersistenceException (org.gluu.persist.exception.mapping.EntryPersistenceException)6 BindResult (com.unboundid.ldap.sdk.BindResult)5 LDAPSearchException (com.unboundid.ldap.sdk.LDAPSearchException)5 TeamModel (com.gitblit.models.TeamModel)4 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)4 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)4 SimplePagedResultsControl (com.unboundid.ldap.sdk.controls.SimplePagedResultsControl)4 ParseException (java.text.ParseException)4 AuthenticationException (org.gluu.persist.exception.operation.AuthenticationException)4 SearchScopeException (org.gluu.persist.exception.operation.SearchScopeException)4