<commit_before>package no.kantega.security.api.impl.kerberos;

import no.kantega.security.api.identity.*;

import javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.Properties;

import org.simplericity.serberuhs.filter.KerberosFilter;

/**
 * Created by IntelliJ IDEA.
 * User: bjorsnos
 * Date: May 12, 2009
 * Time: 12:58:23 PM
 * To change this template use File | Settings | File Templates.
 */
public class KerberosIdentityResolver implements IdentityResolver {

    private static final String TARGET_URI_PARAM = "targetUri";

    private String authenticationContext = "kerberos";
    private String kerberosUrl;

    public AuthenticatedIdentity getIdentity(HttpServletRequest request) throws IdentificationFailedException {

        final String authorizedPrincipal = (String) request.getSession().getAttribute(KerberosFilter.AUTORIZED_PRINCIPAL_SESSION_ATTRIBUTE);
        return authorizedPrincipal == null ? null : new KerberosIdentity(this, authorizedPrincipal);
    }

    public void initateLogin(LoginContext loginContext) {
        String targetUri = loginContext.getTargetUri().toString();
        if(targetUri == null) {
            HttpServletRequest request = loginContext.getRequest();
            targetUri = request.getRequestURL().toString();
        }
        String authenticationService = kerberosUrl.startsWith("/") ? loginContext.getRequest().getContextPath() + kerberosUrl : kerberosUrl;

        String redirectUrl = null;
        try {
            redirectUrl = authenticationService +"?" + TARGET_URI_PARAM +"=" + URLEncoder.encode(targetUri, "utf-8");
            loginContext.getResponse().sendRedirect(redirectUrl);
        } catch (IOException e) {
            throw new RuntimeException("Exception redirecting to uri " + redirectUrl, e);
        }
    }

    public void initiateLogout(LogoutContext logoutContext) {
        logoutContext.getRequest().getSession().removeAttribute(KerberosFilter.AUTORIZED_PRINCIPAL_SESSION_ATTRIBUTE);
        String targetUrl = "/";
        if (logoutContext.getTargetUri() != null) {
            targetUrl = logoutContext.getTargetUri().toASCIIString();
            targetUrl = targetUrl.replaceAll("<", "");
            targetUrl = targetUrl.replaceAll(">", "");
        }

        try {
            logoutContext.getResponse().sendRedirect(targetUrl);
        } catch (IOException e) {

        }
    }

    public String getAuthenticationContext() {
        return authenticationContext;
    }

    public String getAuthenticationContextDescription() {
        return "";
    }

    public String getAuthenticationContextIconUrl() {
        return "";
    }

    public void setAuthenticationContext(String authenticationContext) {
        this.authenticationContext = authenticationContext;
    }

    public void setKerberosUrl(String kerberosUrl) {
        this.kerberosUrl = kerberosUrl;
    }

    private class KerberosIdentity implements AuthenticatedIdentity {
        private final IdentityResolver identityResolver;
        private final String authorizedPrincipal;

        public KerberosIdentity(KerberosIdentityResolver kerberosIdentityResolver, String authorizedPrincipal) {
            this.identityResolver = kerberosIdentityResolver;
            this.authorizedPrincipal = authorizedPrincipal;
        }

        public String getLanguage() {
            throw new IllegalStateException("Language not implemented");<commit_msg>Make target redirect param configurable<commit_after>package no.kantega.security.api.impl.kerberos;

import no.kantega.security.api.identity.*;

import javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.Properties;

import org.simplericity.serberuhs.filter.KerberosFilter;

/**
 * Created by IntelliJ IDEA.
 * User: bjorsnos
 * Date: May 12, 2009
 * Time: 12:58:23 PM
 * To change this template use File | Settings | File Templates.
 */
public class KerberosIdentityResolver implements IdentityResolver {

    private static final String DEFAULT_TARGET_URI_PARAM = "targetUri";

    private String authenticationContext = "kerberos";
    private String kerberosUrl;

    private String targetUrlParam = DEFAULT_TARGET_URI_PARAM;

    public AuthenticatedIdentity getIdentity(HttpServletRequest request) throws IdentificationFailedException {

        final String authorizedPrincipal = (String) request.getSession().getAttribute(KerberosFilter.AUTORIZED_PRINCIPAL_SESSION_ATTRIBUTE);
        return authorizedPrincipal == null ? null : new KerberosIdentity(this, authorizedPrincipal);
    }

    public void initateLogin(LoginContext loginContext) {
        String targetUri = loginContext.getTargetUri().toString();
        if(targetUri == null) {
            HttpServletRequest request = loginContext.getRequest();
            targetUri = request.getRequestURL().toString();
        }
        String authenticationService = kerberosUrl.startsWith("/") ? loginContext.getRequest().getContextPath() + kerberosUrl : kerberosUrl;

        String redirectUrl = null;
        try {
            redirectUrl = authenticationService +"?" + targetUrlParam +"=" + URLEncoder.encode(targetUri, "utf-8");
            loginContext.getResponse().sendRedirect(redirectUrl);
        } catch (IOException e) {
            throw new RuntimeException("Exception redirecting to uri " + redirectUrl, e);
        }
    }

    public void initiateLogout(LogoutContext logoutContext) {
        logoutContext.getRequest().getSession().removeAttribute(KerberosFilter.AUTORIZED_PRINCIPAL_SESSION_ATTRIBUTE);
        String targetUrl = "/";
        if (logoutContext.getTargetUri() != null) {
            targetUrl = logoutContext.getTargetUri().toASCIIString();
            targetUrl = targetUrl.replaceAll("<", "");
            targetUrl = targetUrl.replaceAll(">", "");
        }

        try {
            logoutContext.getResponse().sendRedirect(targetUrl);
        } catch (IOException e) {

        }
    }

    public String getAuthenticationContext() {
        return authenticationContext;
    }

    public String getAuthenticationContextDescription() {
        return "";
    }

    public String getAuthenticationContextIconUrl() {
        return "";
    }

    public void setAuthenticationContext(String authenticationContext) {
        this.authenticationContext = authenticationContext;
    }

    public void setKerberosUrl(String kerberosUrl) {
        this.kerberosUrl = kerberosUrl;
    }

    public void setTargetUrlParam(String targetUrlParam) {
        this.targetUrlParam = targetUrlParam;
    }

    private class KerberosIdentity implements AuthenticatedIdentity {
        private final IdentityResolver identityResolver;
        private final String authorizedPrincipal;

        public KerberosIdentity(KerberosIdentityResolver kerberosIdentityResolver, String authorizedPrincipal) {
            this.identityResolver = kerberosIdentityResolver;
            this.authorizedPrincipal = authorizedPrincipal;
        }

        public String getLanguage() {
            throw new IllegalStateException("Language not implemented");
{'fraction_non_alphanumeric': 0.08571428571428572, 'fraction_numerical': 0.0036236933797909408, 'mean_word_length': 3.4132841328413286, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7065069', 'n_tokens_mistral': 1856, 'n_tokens_neox': 1781, 'n_words': 391}