<commit_before>package org.wyona.yanel.servlet.security.impl;

import org.wyona.yanel.core.map.Map;
import org.wyona.yanel.core.map.Realm;
import org.wyona.yanel.servlet.IdentityMap;
import org.wyona.yanel.servlet.YanelServlet;
import org.wyona.yanel.core.api.security.WebAuthenticator;

import org.wyona.security.core.api.AccessManagementException;
import org.wyona.security.core.ExpiredIdentityException;
import org.wyona.security.core.api.Identity;
import org.wyona.security.core.api.User;

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

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;

import java.net.URL;

import org.w3c.dom.Element;

import org.apache.log4j.Category;

import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;

import org.verisign.joid.consumer.OpenIdFilter;
import org.verisign.joid.util.UrlUtils;

/**
 *
 */
public class DefaultWebAuthenticatorImpl implements WebAuthenticator {

    private static Category log = Category.getInstance(DefaultWebAuthenticatorImpl.class);

    /**
     *
     */
    public void init(org.w3c.dom.Document configuration, javax.xml.transform.URIResolver resolver) throws Exception {
        log.info("Do nothing ...");
    }

    /**
     *
     */
    public HttpServletResponse doAuthenticate(HttpServletRequest request, HttpServletResponse response, Map map, String reservedPrefix, String xsltLoginScreenDefault, String servletContextRealPath, String sslPort) throws ServletException, IOException {
        try {
            Realm realm = map.getRealm(request.getServletPath());
            String path = map.getPath(realm, request.getServletPath());
            //Realm realm = map.getRealm(new Path(request.getServletPath()));
            if (log.isDebugEnabled()) log.debug("Generic WebAuthenticator called for realm path " + path);




            // HTML Form based authentication
            String loginUsername = request.getParameter("yanel.login.username");
            String openID = request.getParameter("yanel.login.openid");
            String openIDSignature = request.getParameter("openid.sig");
            if(loginUsername != null) {
                HttpSession session = request.getSession(true);
                try {
                    User user = realm.getIdentityManager().getUserManager().getUser(loginUsername, true);
                    if (user != null && user.authenticate(request.getParameter("yanel.login.password"))) {
                        log.debug("Realm: " + realm);
                        IdentityMap identityMap = (IdentityMap)session.getAttribute(YanelServlet.IDENTITY_MAP_KEY);
                        if (identityMap == null) {
                            identityMap = new IdentityMap();
                            session.setAttribute(YanelServlet.IDENTITY_MAP_KEY, identityMap);
                        }
                        identityMap.put(realm.getID(), new Identity(user));
                        return null;
                    } else {
                        log.warn("Login failed: " + loginUsername);
                        getXHTMLAuthenticationForm(request, response, realm, "Login failed!", reservedPrefix, xsltLoginScreenDefault, servletContextRealPath, sslPort, map);
                        return response;
                    }
                } catch (ExpiredIdentityException e) {
                    log.warn("Login failed: [" + loginUsername + "] " + e);
                    getXHTMLAuthenticationForm(request, response, realm, "The account has expired!", reservedPrefix, xsltLoginScreenDefault, servletContextRealPath, sslPort, map);
                    return response;
                } catch (AccessManagementException e) {
                    log.warn("Login failed: [" + loginUsername + "] " + e);
                    getXHTMLAuthenticationForm(request, response, realm, "Login failed!", reservedPrefix, xsltLoginScreenDefault, servletContextRealPath, sslPort, map);
                    return response;
                }
            } else if (openID != null) {
                // Append http scheme if missing
                if (!openID.startsWith("http://")) {
                     openID = "http://" + openID;
                }

                String returnToUrlString = UrlUtils.getFullUrl(request);
                log.debug("After successful authentication return to: " + returnToUrlString);
                String redirectUrlString = OpenIdFilter.joid().getAuthUrl(openID, returnToUrlString, returnToUrlString);
                log.debug("OpenID Provider URL: " + redirectUrlString);
                response.sendRedirect(redirectUrlString);
                return response;
            } else if (openIDSignature != null) {
                log.warn("OpenID signature implementation not finished yet: [" + openIDSignature + "]");
                // TODO: src/org/verisign/joid/consumer/JoidConsumer.java
                getXHTMLAuthenticationForm(request, response, realm, "Login failed because OpenID signature implementation is not finished yet!", reservedPrefix, xsltLoginScreenDefault, servletContextRealPath, sslPort, map);
                return response;
            } else {
                if (log.isDebugEnabled()) log.debug("No form based authentication request.");
            }
<commit_msg>TODO refined<commit_after>package org.wyona.yanel.servlet.security.impl;

import org.wyona.yanel.core.map.Map;
import org.wyona.yanel.core.map.Realm;
import org.wyona.yanel.servlet.IdentityMap;
import org.wyona.yanel.servlet.YanelServlet;
import org.wyona.yanel.core.api.security.WebAuthenticator;

import org.wyona.security.core.api.AccessManagementException;
import org.wyona.security.core.ExpiredIdentityException;
import org.wyona.security.core.api.Identity;
import org.wyona.security.core.api.User;

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

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;

import java.net.URL;

import org.w3c.dom.Element;

import org.apache.log4j.Category;

import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;

import org.verisign.joid.consumer.OpenIdFilter;
import org.verisign.joid.util.UrlUtils;

/**
 *
 */
public class DefaultWebAuthenticatorImpl implements WebAuthenticator {

    private static Category log = Category.getInstance(DefaultWebAuthenticatorImpl.class);

    /**
     *
     */
    public void init(org.w3c.dom.Document configuration, javax.xml.transform.URIResolver resolver) throws Exception {
        log.info("Do nothing ...");
    }

    /**
     *
     */
    public HttpServletResponse doAuthenticate(HttpServletRequest request, HttpServletResponse response, Map map, String reservedPrefix, String xsltLoginScreenDefault, String servletContextRealPath, String sslPort) throws ServletException, IOException {
        try {
            Realm realm = map.getRealm(request.getServletPath());
            String path = map.getPath(realm, request.getServletPath());
            //Realm realm = map.getRealm(new Path(request.getServletPath()));
            if (log.isDebugEnabled()) log.debug("Generic WebAuthenticator called for realm path " + path);




            // HTML Form based authentication
            String loginUsername = request.getParameter("yanel.login.username");
            String openID = request.getParameter("yanel.login.openid");
            String openIDSignature = request.getParameter("openid.sig");
            if(loginUsername != null) {
                HttpSession session = request.getSession(true);
                try {
                    User user = realm.getIdentityManager().getUserManager().getUser(loginUsername, true);
                    if (user != null && user.authenticate(request.getParameter("yanel.login.password"))) {
                        log.debug("Realm: " + realm);
                        IdentityMap identityMap = (IdentityMap)session.getAttribute(YanelServlet.IDENTITY_MAP_KEY);
                        if (identityMap == null) {
                            identityMap = new IdentityMap();
                            session.setAttribute(YanelServlet.IDENTITY_MAP_KEY, identityMap);
                        }
                        identityMap.put(realm.getID(), new Identity(user));
                        return null;
                    } else {
                        log.warn("Login failed: " + loginUsername);
                        getXHTMLAuthenticationForm(request, response, realm, "Login failed!", reservedPrefix, xsltLoginScreenDefault, servletContextRealPath, sslPort, map);
                        return response;
                    }
                } catch (ExpiredIdentityException e) {
                    log.warn("Login failed: [" + loginUsername + "] " + e);
                    getXHTMLAuthenticationForm(request, response, realm, "The account has expired!", reservedPrefix, xsltLoginScreenDefault, servletContextRealPath, sslPort, map);
                    return response;
                } catch (AccessManagementException e) {
                    log.warn("Login failed: [" + loginUsername + "] " + e);
                    getXHTMLAuthenticationForm(request, response, realm, "Login failed!", reservedPrefix, xsltLoginScreenDefault, servletContextRealPath, sslPort, map);
                    return response;
                }
            } else if (openID != null) {
                // Append http scheme if missing
                if (!openID.startsWith("http://")) {
                     openID = "http://" + openID;
                }

                String returnToUrlString = UrlUtils.getFullUrl(request);
                log.debug("After successful authentication return to: " + returnToUrlString);
                String redirectUrlString = OpenIdFilter.joid().getAuthUrl(openID, returnToUrlString, returnToUrlString);
                log.debug("OpenID Provider URL: " + redirectUrlString);
                response.sendRedirect(redirectUrlString);
                return response;
            } else if (openIDSignature != null) {
                log.warn("OpenID signature implementation not finished yet: [" + openIDSignature + "]");
                // TODO: src/org/verisign/joid/consumer/JoidConsumer.java
                // see AuthenticationResult result = joid.authenticate(convertToStringValueMap(servletRequest.getParameterMap())); (src/org/verisign/joid/consumer/OpenIdFilter.java)
                // https://127.0.0.1:8443/yanel/foaf/login.html?openid.sig=2%2FjpOdpJpEMfibrb9v9OHuzm0kg%3D&amp;openid.mode=id_res&amp;openid.return_to=https%3A%2F%2F127.0.0.1%3A8443%2Fyanel%2Ffoaf%2Flogin.html&amp;openid.identity=http%3A%2F%2Fopenid.claimid.com%2Fmichi&amp;openid.signed=identity%2Creturn_to%2Cmode&amp;openid.assoc_handle=%7BHMAC-SHA1%7D%7B47967654%7D%7BB8gYrw%3D%3D%7D

                getXHTMLAuthenticationForm(request, response, realm, "Login failed because OpenID signature implementation is not finished yet!", reservedPrefix, xsltLoginScreenDefault, servletContextRealPath, sslPort, map);
                return response;
            } else {
                if (log.isDebugEnabled()) log.debug("No form based authentication request.");
            }

{'fraction_non_alphanumeric': 0.10112454553141118, 'fraction_numerical': 0.0053267946224740004, 'mean_word_length': 2.621555419473362, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 2, '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': '6444718', 'n_tokens_mistral': 3176, 'n_tokens_neox': 3068, 'n_words': 589}