Search in sources :

Example 1 with SecurityRequestWrapper

use of org.securityfilter.filter.SecurityRequestWrapper in project xwiki-platform by xwiki.

the class XWikiAuthServiceImpl method checkAuth.

/**
 * Method to authenticate and set the cookie from a username and password passed as parameters
 *
 * @return null if the user is not authenticated properly
 */
@Override
public XWikiUser checkAuth(String username, String password, String rememberme, XWikiContext context) throws XWikiException {
    HttpServletRequest request = null;
    HttpServletResponse response = context.getResponse();
    if (context.getRequest() != null) {
        request = context.getRequest().getHttpServletRequest();
    }
    if (request == null) {
        return null;
    }
    XWikiAuthenticator auth = getAuthenticator(context);
    SecurityRequestWrapper wrappedRequest = new SecurityRequestWrapper(request, null, null, auth.getAuthMethod());
    try {
        if (!auth.processLogin(username, password, rememberme, wrappedRequest, response, context)) {
            return null;
        }
        Principal principal = wrappedRequest.getUserPrincipal();
        if (LOGGER.isInfoEnabled()) {
            if (principal != null) {
                LOGGER.info("User " + principal.getName() + " is authentified");
            }
        }
        if (principal == null) {
            return null;
        }
        return new XWikiUser(getContextUserName(principal, context));
    } catch (Exception e) {
        LOGGER.error("Failed to authenticate", e);
        return null;
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) XWikiUser(com.xpn.xwiki.user.api.XWikiUser) SecurityRequestWrapper(org.securityfilter.filter.SecurityRequestWrapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) Principal(java.security.Principal) SimplePrincipal(org.securityfilter.realm.SimplePrincipal) XWikiException(com.xpn.xwiki.XWikiException) IOException(java.io.IOException)

Example 2 with SecurityRequestWrapper

use of org.securityfilter.filter.SecurityRequestWrapper in project xwiki-platform by xwiki.

the class XWikiAuthServiceImpl method checkAuth.

@Override
public XWikiUser checkAuth(XWikiContext context) throws XWikiException {
    // Debug time taken.
    long time = System.currentTimeMillis();
    HttpServletRequest request = null;
    HttpServletResponse response = context.getResponse();
    if (context.getRequest() != null) {
        request = context.getRequest().getHttpServletRequest();
    }
    if (request == null) {
        return null;
    }
    XWikiAuthenticator auth = getAuthenticator(context);
    SecurityRequestWrapper wrappedRequest = new SecurityRequestWrapper(request, null, null, auth.getAuthMethod());
    try {
        if (auth.processLogin(wrappedRequest, response, context)) {
            return null;
        }
        // Process logout (this only works with Forms)
        if (auth.processLogout(wrappedRequest, response, new URLPatternMatcher())) {
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("User " + context.getUser() + " has been logged-out");
            }
            wrappedRequest.setUserPrincipal(null);
            return null;
        }
        final String userName = getContextUserName(wrappedRequest.getUserPrincipal(), context);
        if (LOGGER.isInfoEnabled()) {
            if (userName != null) {
                LOGGER.info("User " + userName + " is authentified");
            }
        }
        if (userName == null) {
            return null;
        }
        return new XWikiUser(userName);
    } catch (Exception e) {
        LOGGER.error("Failed to authenticate", e);
        return null;
    } finally {
        LOGGER.debug("XWikiAuthServiceImpl.checkAuth(XWikiContext) took " + (System.currentTimeMillis() - time) + " milliseconds to run.");
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) URLPatternMatcher(org.securityfilter.filter.URLPatternMatcher) XWikiUser(com.xpn.xwiki.user.api.XWikiUser) SecurityRequestWrapper(org.securityfilter.filter.SecurityRequestWrapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) XWikiException(com.xpn.xwiki.XWikiException) IOException(java.io.IOException)

Aggregations

XWikiException (com.xpn.xwiki.XWikiException)2 XWikiUser (com.xpn.xwiki.user.api.XWikiUser)2 IOException (java.io.IOException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 SecurityRequestWrapper (org.securityfilter.filter.SecurityRequestWrapper)2 Principal (java.security.Principal)1 URLPatternMatcher (org.securityfilter.filter.URLPatternMatcher)1 SimplePrincipal (org.securityfilter.realm.SimplePrincipal)1