Search in sources :

Example 11 with LdapContext

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

the class LdapGroupHasUsersAuthPlugin method authenticateAndAuthorize.

@Override
public AuthInfo authenticateAndAuthorize(AuthToken authToken) throws AuthenticationException {
    try {
        String username = authToken.principal();
        char[] password = authToken.credentials();
        LdapContext ctx = authenticate(username, password);
        Set<String> roles = authorize(ctx, username);
        return AuthInfo.of(username, roles);
    } catch (NamingException e) {
        throw new AuthenticationException(e.getMessage());
    }
}
Also used : AuthenticationException(org.neo4j.server.security.enterprise.auth.plugin.api.AuthenticationException) NamingException(javax.naming.NamingException) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LdapContext(javax.naming.ldap.LdapContext)

Example 12 with LdapContext

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

the class LdapRealm method queryForAuthorizationInfo.

@Override
protected AuthorizationInfo queryForAuthorizationInfo(PrincipalCollection principals, LdapContextFactory ldapContextFactory) throws NamingException {
    if (authorizationEnabled) {
        String username = getUsername(principals);
        if (username == null) {
            return null;
        }
        if (useSystemAccountForAuthorization) {
            // Perform context search using the system context
            LdapContext ldapContext = useStartTls ? getSystemLdapContextUsingStartTls(ldapContextFactory) : ldapContextFactory.getSystemLdapContext();
            Set<String> roleNames;
            try {
                roleNames = findRoleNamesForUser(username, ldapContext);
            } finally {
                LdapUtils.closeContext(ldapContext);
            }
            return new SimpleAuthorizationInfo(roleNames);
        } else {
            // Authorization info is cached during authentication
            Cache<Object, AuthorizationInfo> authorizationCache = getAuthorizationCache();
            AuthorizationInfo authorizationInfo = authorizationCache.get(username);
            if (authorizationInfo == null) {
                // so that the client can react by re-authenticating.
                throw new AuthorizationExpiredException("LDAP authorization info expired.");
            }
            return authorizationInfo;
        }
    }
    return null;
}
Also used : SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) AuthorizationExpiredException(org.neo4j.graphdb.security.AuthorizationExpiredException) AuthorizationInfo(org.apache.shiro.authz.AuthorizationInfo) SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LdapContext(javax.naming.ldap.LdapContext)

Example 13 with LdapContext

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

the class LdapRealm method getLdapContextUsingStartTls.

private LdapContext getLdapContextUsingStartTls(LdapContextFactory ldapContextFactory, Object principal, Object credentials) throws NamingException {
    JndiLdapContextFactory jndiLdapContextFactory = (JndiLdapContextFactory) ldapContextFactory;
    Hashtable<String, Object> env = new Hashtable<>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, jndiLdapContextFactory.getContextFactoryClassName());
    env.put(Context.PROVIDER_URL, jndiLdapContextFactory.getUrl());
    LdapContext ctx = null;
    try {
        ctx = new InitialLdapContext(env, null);
        StartTlsRequest startTlsRequest = new StartTlsRequest();
        StartTlsResponse tls = (StartTlsResponse) ctx.extendedOperation(startTlsRequest);
        tls.negotiate();
        ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, jndiLdapContextFactory.getAuthenticationMechanism());
        ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, principal);
        ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, credentials);
        ctx.reconnect(ctx.getConnectControls());
        return ctx;
    } catch (IOException e) {
        LdapUtils.closeContext(ctx);
        securityLog.error(withRealm("Failed to negotiate TLS connection with '%s': ", server(jndiLdapContextFactory), e));
        throw new CommunicationException(e.getMessage());
    } catch (Throwable t) {
        LdapUtils.closeContext(ctx);
        securityLog.error(withRealm("Unexpected failure to negotiate TLS connection with '%s': ", server(jndiLdapContextFactory), t));
        throw t;
    }
}
Also used : StartTlsResponse(javax.naming.ldap.StartTlsResponse) CommunicationException(javax.naming.CommunicationException) Hashtable(java.util.Hashtable) InitialLdapContext(javax.naming.ldap.InitialLdapContext) IOException(java.io.IOException) StartTlsRequest(javax.naming.ldap.StartTlsRequest) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LdapContext(javax.naming.ldap.LdapContext) JndiLdapContextFactory(org.apache.shiro.realm.ldap.JndiLdapContextFactory)

Example 14 with LdapContext

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

the class LdapRealmTest method shouldWarnAboutAmbiguousUserSearch.

@Test
public void shouldWarnAboutAmbiguousUserSearch() throws NamingException {
    when(config.get(SecuritySettings.ldap_authorization_user_search_filter)).thenReturn("{0}");
    LdapContext ldapContext = mock(LdapContext.class);
    NamingEnumeration result = mock(NamingEnumeration.class);
    SearchResult searchResult = mock(SearchResult.class);
    when(ldapContext.search(anyString(), anyString(), anyObject(), anyObject())).thenReturn(result);
    when(result.hasMoreElements()).thenReturn(true);
    when(result.next()).thenReturn(searchResult);
    when(searchResult.toString()).thenReturn("<ldap search result>");
    LdapRealm realm = new LdapRealm(config, securityLog, secureHasher);
    realm.findRoleNamesForUser("username", ldapContext);
    verify(securityLog).warn(contains("LDAP user search for user principal 'username' is ambiguous"));
}
Also used : NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) LdapContext(javax.naming.ldap.LdapContext) Test(org.junit.Test)

Example 15 with LdapContext

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

the class LdapRealmTest method shouldWarnAboutUserSearchBaseBeingEmpty.

@Test
public void shouldWarnAboutUserSearchBaseBeingEmpty() throws Exception {
    when(config.get(SecuritySettings.ldap_authorization_user_search_base)).thenReturn("");
    LdapContext ldapContext = mock(LdapContext.class);
    NamingEnumeration result = mock(NamingEnumeration.class);
    when(ldapContext.search(anyString(), anyString(), anyObject(), anyObject())).thenReturn(result);
    when(result.hasMoreElements()).thenReturn(false);
    assertException(this::makeAndInit, IllegalArgumentException.class, "Illegal LDAP user search settings, see security log for details.");
    verify(securityLog).error(contains("LDAP user search base is empty."));
}
Also used : NamingEnumeration(javax.naming.NamingEnumeration) LdapContext(javax.naming.ldap.LdapContext) Test(org.junit.Test)

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