Search in sources :

Example 6 with BindResult

use of com.unboundid.ldap.sdk.BindResult in project spring-boot by spring-projects.

the class EmbeddedLdapAutoConfigurationTests method testSetCredentials.

@Test
public void testSetCredentials() throws LDAPException {
    load("spring.ldap.embedded.base-dn:dc=spring,dc=org", "spring.ldap.embedded.credential.username:uid=root", "spring.ldap.embedded.credential.password:boot");
    InMemoryDirectoryServer server = this.context.getBean(InMemoryDirectoryServer.class);
    BindResult result = server.bind("uid=root", "boot");
    assertThat(result).isNotNull();
}
Also used : InMemoryDirectoryServer(com.unboundid.ldap.listener.InMemoryDirectoryServer) BindResult(com.unboundid.ldap.sdk.BindResult) Test(org.junit.Test)

Example 7 with BindResult

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

the class LdapConnection method bind.

/**
	 * Bind using the given credentials, by filling in the username in the given {@code bindPattern} to
	 * create the DN.
	 * @return A bind result, or null if binding failed.
	 */
public BindResult bind(String bindPattern, String simpleUsername, String password) {
    BindResult result = null;
    try {
        String bindUser = StringUtils.replace(bindPattern, "${username}", escapeLDAPSearchFilter(simpleUsername));
        SimpleBindRequest request = new SimpleBindRequest(bindUser, password);
        result = conn.bind(request);
        userBindRequest = request;
        currentBindRequest = userBindRequest;
    } catch (LDAPException e) {
        logger.error("Error authenticating to LDAP with user account to search the directory.");
        logger.error("  Please check your settings for realm.ldap.bindpattern.");
        logger.debug("  Received exception when binding to LDAP", e);
        return null;
    }
    return result;
}
Also used : SimpleBindRequest(com.unboundid.ldap.sdk.SimpleBindRequest) LDAPException(com.unboundid.ldap.sdk.LDAPException) BindResult(com.unboundid.ldap.sdk.BindResult)

Example 8 with BindResult

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

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

the class LdapConnectionTest method testBindAsAdmin.

@Test
public void testBindAsAdmin() {
    // This test tests for anonymous bind, so run only in authentication mode DS_MANAGER.
    assumeTrue(authMode == AuthMode.DS_MANAGER);
    LdapConnection conn = new LdapConnection(settings);
    try {
        assertTrue(conn.connect());
        BindResult br = conn.bind();
        assertNotNull(br);
        assertEquals(ResultCode.SUCCESS, br.getResultCode());
        assertEquals(settings.getString(Keys.realm.ldap.username, "UNSET"), authMode.getBindTracker().getLastSuccessfulBindDN(br.getMessageID()));
    } finally {
        conn.close();
    }
}
Also used : BindResult(com.unboundid.ldap.sdk.BindResult) LdapConnection(com.gitblit.ldap.LdapConnection) Test(org.junit.Test)

Example 10 with BindResult

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

the class LdapConnectionTest method testBindAnonymous.

@Test
public void testBindAnonymous() {
    // This test tests for anonymous bind, so run only in authentication mode ANONYMOUS.
    assumeTrue(authMode == AuthMode.ANONYMOUS);
    LdapConnection conn = new LdapConnection(settings);
    try {
        assertTrue(conn.connect());
        BindResult br = conn.bind();
        assertNotNull(br);
        assertEquals(ResultCode.SUCCESS, br.getResultCode());
        assertEquals("", authMode.getBindTracker().getLastSuccessfulBindDN(br.getMessageID()));
    } finally {
        conn.close();
    }
}
Also used : BindResult(com.unboundid.ldap.sdk.BindResult) LdapConnection(com.gitblit.ldap.LdapConnection) Test(org.junit.Test)

Aggregations

BindResult (com.unboundid.ldap.sdk.BindResult)14 LdapConnection (com.gitblit.ldap.LdapConnection)9 Test (org.junit.Test)8 SearchResult (com.unboundid.ldap.sdk.SearchResult)5 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)5 LDAPException (com.unboundid.ldap.sdk.LDAPException)3 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)2 ArrayList (java.util.ArrayList)2 TeamModel (com.gitblit.models.TeamModel)1 UserModel (com.gitblit.models.UserModel)1 InMemoryDirectoryServer (com.unboundid.ldap.listener.InMemoryDirectoryServer)1 ExtendedResult (com.unboundid.ldap.sdk.ExtendedResult)1 SearchRequest (com.unboundid.ldap.sdk.SearchRequest)1 SimpleBindRequest (com.unboundid.ldap.sdk.SimpleBindRequest)1 StartTLSExtendedRequest (com.unboundid.ldap.sdk.extensions.StartTLSExtendedRequest)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 Matcher (java.util.regex.Matcher)1 SSLContext (javax.net.ssl.SSLContext)1