<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 *
 */

package com.xpn.xwiki.user.impl.xwiki;

import java.io.IOException;
import java.security.Principal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.codec.binary.Base64;
import org.securityfilter.authenticator.BasicAuthenticator;
import org.securityfilter.filter.SecurityFilter;
import org.securityfilter.filter.SecurityRequestWrapper;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;

public class MyBasicAuthenticator extends BasicAuthenticator implements XWikiAuthenticator
{
    public boolean processLogin(SecurityRequestWrapper request, HttpServletResponse response)
        throws Exception
    {
        return processLogin(request, response, null);
    }

    public boolean processLogin(SecurityRequestWrapper request, HttpServletResponse response,
        XWikiContext context) throws Exception
    {
        Principal principal = checkLogin(request, response, context);

        if (principal == null) {
            // login failed
            // show the basic authentication window again.
            showLogin(request.getCurrentRequest(), response);
            return true;
        }

        return false;
    }

    public boolean processLogin(String username, String password, String rememberme,
        SecurityRequestWrapper request, HttpServletResponse response, XWikiContext context)
        throws Exception
    {
        Principal principal = authenticate(username, password, context);
        if (principal != null) {
            // login successful
            request.getSession().removeAttribute(LOGIN_ATTEMPTS);
            request.setUserPrincipal(principal);
            return false;
        } else {
            // login failed
            // show the basic authentication window again.
            showLogin(request.getCurrentRequest(), response);
            return true;
        }
    }

    private static String convertUsername(String username, XWikiContext context)
    {
        return context.getWiki().convertUsername(username, context);
    }

    public static Principal checkLogin(SecurityRequestWrapper request,
        HttpServletResponse response, XWikiContext context) throws Exception
    {
        // Always verify authentication
        String authorizationHeader = request.getHeader("Authorization");
        HttpSession session = request.getSession();
        if (authorizationHeader != null) {
            String decoded = decodeBasicAuthorizationString(authorizationHeader);
            String username = convertUsername(parseUsername(decoded), context);
            String password = parsePassword(decoded);

            Principal principal = authenticate(username, password, context);

            if (principal != null) {
                // login successful
                request.getSession().removeAttribute(LOGIN_ATTEMPTS);
                request.setUserPrincipal(principal);
                return principal;
            }
        }
        return null;
    }

    /**
     * Parse the username out of the BASIC authorization header string.<commit_msg>[cleanup] Fix compilation warning<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 *
 */

package com.xpn.xwiki.user.impl.xwiki;

import java.io.IOException;
import java.security.Principal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.Base64;
import org.securityfilter.authenticator.BasicAuthenticator;
import org.securityfilter.filter.SecurityFilter;
import org.securityfilter.filter.SecurityRequestWrapper;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;

public class MyBasicAuthenticator extends BasicAuthenticator implements XWikiAuthenticator
{
    public boolean processLogin(SecurityRequestWrapper request, HttpServletResponse response)
        throws Exception
    {
        return processLogin(request, response, null);
    }

    public boolean processLogin(SecurityRequestWrapper request, HttpServletResponse response,
        XWikiContext context) throws Exception
    {
        Principal principal = checkLogin(request, response, context);

        if (principal == null) {
            // login failed
            // show the basic authentication window again.
            showLogin(request.getCurrentRequest(), response);
            return true;
        }

        return false;
    }

    public boolean processLogin(String username, String password, String rememberme,
        SecurityRequestWrapper request, HttpServletResponse response, XWikiContext context)
        throws Exception
    {
        Principal principal = authenticate(username, password, context);
        if (principal != null) {
            // login successful
            request.getSession().removeAttribute(LOGIN_ATTEMPTS);
            request.setUserPrincipal(principal);
            return false;
        } else {
            // login failed
            // show the basic authentication window again.
            showLogin(request.getCurrentRequest(), response);
            return true;
        }
    }

    private static String convertUsername(String username, XWikiContext context)
    {
        return context.getWiki().convertUsername(username, context);
    }

    public static Principal checkLogin(SecurityRequestWrapper request,
        HttpServletResponse response, XWikiContext context) throws Exception
    {
        // Always verify authentication
        String authorizationHeader = request.getHeader("Authorization");
        if (authorizationHeader != null) {
            String decoded = decodeBasicAuthorizationString(authorizationHeader);
            String username = convertUsername(parseUsername(decoded), context);
            String password = parsePassword(decoded);

            Principal principal = authenticate(username, password, context);

            if (principal != null) {
                // login successful
                request.getSession().removeAttribute(LOGIN_ATTEMPTS);
                request.setUserPrincipal(principal);
                return principal;
            }
        }
        return null;
    }

    /**
     * Parse the username out of the BASIC authorization header string.
{'fraction_non_alphanumeric': 0.06959434313360625, 'fraction_numerical': 0.003721622627465575, 'mean_word_length': 2.991089108910891, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2280355', 'n_tokens_mistral': 1909, 'n_tokens_neox': 1845, 'n_words': 643}