Search in sources :

Example 26 with AuthenticationException

use of org.exist.security.AuthenticationException in project exist by eXist-db.

the class LDAPRealm method getGroup.

private synchronized Group getGroup(final LdapContext ctx, final DBBroker broker, final String name) {
    if (name == null) {
        return null;
    }
    final String gName = ensureCase(name);
    final Group grp = getGroup(gName);
    if (grp != null) {
        return grp;
    } else {
        // if the group is not cached, we should try and find it in LDAP and cache it if it exists
        try {
            // do the lookup
            final SearchResult ldapGroup = findGroupByGroupName(ctx, removeDomainPostfix(gName));
            if (ldapGroup == null) {
                return null;
            } else {
                // found a group from ldap so cache them and return
                try {
                    return createGroupInDatabase(broker, gName);
                // registerGroup(grp); //TODO do we need to do this?
                } catch (final AuthenticationException ae) {
                    LOG.error(ae.getMessage(), ae);
                    return null;
                }
            }
        } catch (final NamingException ne) {
            LOG.error(new AuthenticationException(AuthenticationException.UNNOWN_EXCEPTION, ne.getMessage()));
            return null;
        }
    }
}
Also used : Group(org.exist.security.Group) AuthenticationException(org.exist.security.AuthenticationException) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException)

Example 27 with AuthenticationException

use of org.exist.security.AuthenticationException in project exist by eXist-db.

the class LDAPRealm method findAllGroupNames.

@Override
public List<String> findAllGroupNames() {
    final List<String> groupnames = new ArrayList<>();
    LdapContext ctx = null;
    try {
        ctx = getContext(getSecurityManager().getCurrentSubject());
        final LDAPSearchContext search = ensureContextFactory().getSearch();
        final SearchAttribute sa = new SearchAttribute(null, null);
        final String searchFilter = buildSearchFilter(search.getSearchGroup().getSearchFilterPrefix(), sa);
        final SearchControls searchControls = new SearchControls();
        searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        searchControls.setReturningAttributes(new String[] { search.getSearchGroup().getSearchAttribute(LDAPSearchAttributeKey.NAME) });
        final NamingEnumeration<SearchResult> results = ctx.search(search.getBase(), searchFilter, searchControls);
        while (results.hasMoreElements()) {
            final SearchResult searchResult = results.nextElement();
            final String groupname = ensureCase(addDomainPostfix((String) searchResult.getAttributes().get(search.getSearchGroup().getSearchAttribute(LDAPSearchAttributeKey.NAME)).get()));
            if (checkGroupRestrictionList(groupname)) {
                groupnames.add(groupname);
            }
        }
    } catch (final NamingException ne) {
        LOG.error(new AuthenticationException(AuthenticationException.UNNOWN_EXCEPTION, ne.getMessage()));
    } finally {
        if (ctx != null) {
            LdapUtils.closeContext(ctx);
        }
    }
    return groupnames;
}
Also used : AuthenticationException(org.exist.security.AuthenticationException) ArrayList(java.util.ArrayList) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) LdapContext(javax.naming.ldap.LdapContext)

Example 28 with AuthenticationException

use of org.exist.security.AuthenticationException in project exist by eXist-db.

the class LDAPRealm method authenticate.

@Override
public Subject authenticate(final String username, final Object credentials) throws AuthenticationException {
    final String name = ensureCase(username);
    // Binds using the username and password provided by the user.
    LdapContext ctx = null;
    try {
        ctx = getContextWithCredentials(Optional.of(Tuple(name, String.valueOf(credentials))));
        final AbstractAccount account = (AbstractAccount) getAccount(ctx, name);
        if (account == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Account '{}' can not be found.", name);
            }
            throw new AuthenticationException(AuthenticationException.ACCOUNT_NOT_FOUND, "Account '" + name + "' can not be found.");
        }
        return new AuthenticatedLdapSubjectAccreditedImpl(account, ctx, String.valueOf(credentials));
    } catch (final NamingException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(e.getMessage(), e);
        }
        if (e instanceof javax.naming.AuthenticationException) {
            throw new AuthenticationException(AuthenticationException.ACCOUNT_NOT_FOUND, e.getMessage());
        } else {
            throw new AuthenticationException(AuthenticationException.UNNOWN_EXCEPTION, e.getMessage());
        }
    } finally {
        LdapUtils.closeContext(ctx);
    }
}
Also used : AbstractAccount(org.exist.security.AbstractAccount) AuthenticationException(org.exist.security.AuthenticationException) NamingException(javax.naming.NamingException) LdapContext(javax.naming.ldap.LdapContext)

Example 29 with AuthenticationException

use of org.exist.security.AuthenticationException in project exist by eXist-db.

the class AsUser method eval.

@Override
public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException {
    logger.debug("Entering the " + SystemModule.PREFIX + ":as-user XQuery function");
    final DBBroker broker = context.getBroker();
    final Sequence usernameResult = getArgument(0).eval(contextSequence, contextItem);
    if (usernameResult.isEmpty()) {
        final XPathException exception = new XPathException(this, "No user specified");
        logger.error("No user specified, throwing an exception!", exception);
        throw exception;
    }
    final Sequence password = getArgument(1).eval(contextSequence, contextItem);
    final String username = usernameResult.getStringValue();
    final SecurityManager sm = broker.getBrokerPool().getSecurityManager();
    Subject user;
    try {
        user = sm.authenticate(username, password.getStringValue());
    } catch (final AuthenticationException e) {
        final XPathException exception = new XPathException(this, "Authentication failed", e);
        logger.error("Authentication failed for [{}] because of [{}].", username, e.getMessage(), exception);
        throw exception;
    }
    logger.info("Setting the effective user to: [{}]", username);
    try {
        broker.pushSubject(user);
        return getArgument(2).eval(contextSequence, contextItem);
    } finally {
        broker.popSubject();
        logger.info("Returned the effective user to: [{}]", broker.getCurrentSubject());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) SecurityManager(org.exist.security.SecurityManager) AuthenticationException(org.exist.security.AuthenticationException) Sequence(org.exist.xquery.value.Sequence) Subject(org.exist.security.Subject)

Example 30 with AuthenticationException

use of org.exist.security.AuthenticationException in project exist by eXist-db.

the class DatabaseImpl method getUser.

/**
 * @param user
 * @param pool
 * @return the User object corresponding to the username in <code>user</code>
 * @throws XMLDBException
 */
private Subject getUser(String user, String password, final BrokerPool pool) throws XMLDBException {
    try {
        if (user == null) {
            user = SecurityManager.GUEST_USER;
            password = SecurityManager.GUEST_USER;
        }
        final SecurityManager securityManager = pool.getSecurityManager();
        return securityManager.authenticate(user, password);
    } catch (final AuthenticationException e) {
        throw new XMLDBException(ErrorCodes.PERMISSION_DENIED, e.getMessage(), e);
    }
}
Also used : SecurityManager(org.exist.security.SecurityManager) AuthenticationException(org.exist.security.AuthenticationException) XMLDBException(org.xmldb.api.base.XMLDBException)

Aggregations

AuthenticationException (org.exist.security.AuthenticationException)33 NamingException (javax.naming.NamingException)16 Subject (org.exist.security.Subject)13 SearchResult (javax.naming.directory.SearchResult)12 LdapContext (javax.naming.ldap.LdapContext)12 SearchControls (javax.naming.directory.SearchControls)9 ArrayList (java.util.ArrayList)8 EXistException (org.exist.EXistException)8 SecurityManager (org.exist.security.SecurityManager)8 AbstractAccount (org.exist.security.AbstractAccount)6 Account (org.exist.security.Account)6 PermissionDeniedException (org.exist.security.PermissionDeniedException)5 Group (org.exist.security.Group)4 DBBroker (org.exist.storage.DBBroker)4 HttpSession (javax.servlet.http.HttpSession)3 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 URISyntaxException (java.net.URISyntaxException)2 Properties (java.util.Properties)2 ServletException (javax.servlet.ServletException)2