<commit_before>/*
 * Copyright 2009 Kantega AS
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package no.kantega.publishing.security;

import no.kantega.commons.exception.ConfigurationException;
import no.kantega.commons.exception.SystemException;
import no.kantega.commons.log.Log;
import no.kantega.publishing.common.Aksess;
import no.kantega.publishing.common.cache.SiteCache;
import no.kantega.publishing.common.data.BaseObject;
import no.kantega.publishing.common.data.Content;
import no.kantega.publishing.common.data.Site;
import no.kantega.publishing.org.OrganizationManager;
import no.kantega.publishing.security.data.CachedBaseObject;
import no.kantega.publishing.security.data.Role;
import no.kantega.publishing.security.data.User;
import no.kantega.publishing.security.login.PostLoginHandler;
import no.kantega.publishing.security.login.PostLoginHandlerFactory;
import no.kantega.publishing.security.realm.SecurityRealm;
import no.kantega.publishing.security.realm.SecurityRealmFactory;
import no.kantega.publishing.security.service.SecurityService;
import no.kantega.publishing.security.util.SecurityHelper;
import no.kantega.publishing.spring.RootContext;
import no.kantega.publishing.topicmaps.ao.TopicAO;
import no.kantega.publishing.topicmaps.data.Topic;
import no.kantega.security.api.identity.*;
import no.kantega.security.api.profile.Profile;
import no.kantega.security.api.profile.ProfileManager;
import no.kantega.security.api.password.PasswordManager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

public class SecuritySession {
    private static String SOURCE = "SecuritySession";

    private User user = null;
    private Identity identity = null;
    private SecurityRealm realm = null;

    // Husker sist tilgangssjekk
    private CachedBaseObject prevObject = null;
    private int prevPrivilege = -1;
    private boolean prevResult = false;


    /**
     * Henter en sikkerhetssesjon - Sjekker om en bruker er innlogget, setter i s�fall User og Identity objektet
     * @param request
     * @return
     * @throws SystemException
     */
    public static SecuritySession getInstance(HttpServletRequest request) throws SystemException {

        SecuritySession session = (SecuritySession) request.getSession(true).getAttribute("aksess.securitySession");
        if (session == null) {
            session = createNewInstance();
            request.getSession(true).setAttribute("aksess.securitySession", session);
        }

        IdentityResolver resolver = session.realm.getIdentityResolver();
        Identity identity = null;
        try {
            identity = resolver.getIdentity(request);
        } catch (IdentificationFailedException e) {
            throw new SystemException(SOURCE, "IdentificationFailedException", e);
        }
        Identity currentIdentity = session.identity;

        // If your is now logged in or has changed identity, must create new session
        if (identity != null && (currentIdentity == null || !identity.getUserId().equals(currentIdentity.getUserId()) || !identity.getDomain().equals(currentIdentity.getDomain()))) {
            session = createNewUserInstance(identity);
            try {
                session.handlePostLogin(request);
            } catch (ConfigurationException e) {
                throw new SystemException(SOURCE, "Konfigurasjonsfeil", e);
            }
            // Innloggede brukere har lengre sesjonstimeout
            if (request.getSession().getMaxInactiveInterval() < Aksess.getSecuritySessionTimeout()) {
                request.getSession().setMaxInactiveInterval(Aksess.getSecuritySessionTimeout());
            }
            request.getSession(true).setAttribute("aksess.securitySession", session);

        } else if (identity == null && currentIdentity != null) {
            // Bruker er utlogget via ekstern tjeneste - lag blank sesjon           
            session = createNewInstance();
            request.getSession(true).setAttribute("aksess.securitySession", session);
            request.getSession(true).removeAttribute("adminMode");
        }

        return session;
    }<commit_msg>Allow "fake" logins by setting "fakeUsername" and "fakeUserDomain" context params<commit_after>/*
 * Copyright 2009 Kantega AS
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package no.kantega.publishing.security;

import no.kantega.commons.exception.ConfigurationException;
import no.kantega.commons.exception.SystemException;
import no.kantega.commons.log.Log;
import no.kantega.publishing.common.Aksess;
import no.kantega.publishing.common.cache.SiteCache;
import no.kantega.publishing.common.data.BaseObject;
import no.kantega.publishing.common.data.Content;
import no.kantega.publishing.common.data.Site;
import no.kantega.publishing.org.OrganizationManager;
import no.kantega.publishing.security.data.CachedBaseObject;
import no.kantega.publishing.security.data.Role;
import no.kantega.publishing.security.data.User;
import no.kantega.publishing.security.login.PostLoginHandler;
import no.kantega.publishing.security.login.PostLoginHandlerFactory;
import no.kantega.publishing.security.realm.SecurityRealm;
import no.kantega.publishing.security.realm.SecurityRealmFactory;
import no.kantega.publishing.security.service.SecurityService;
import no.kantega.publishing.security.util.SecurityHelper;
import no.kantega.publishing.spring.RootContext;
import no.kantega.publishing.topicmaps.ao.TopicAO;
import no.kantega.publishing.topicmaps.data.Topic;
import no.kantega.security.api.identity.*;
import no.kantega.security.api.password.PasswordManager;
import no.kantega.security.api.profile.Profile;
import no.kantega.security.api.profile.ProfileManager;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

public class SecuritySession {
    private static String SOURCE = "SecuritySession";

    private User user = null;
    private Identity identity = null;
    private SecurityRealm realm = null;

    // Husker sist tilgangssjekk
    private CachedBaseObject prevObject = null;
    private int prevPrivilege = -1;
    private boolean prevResult = false;


    /**
     * Henter en sikkerhetssesjon - Sjekker om en bruker er innlogget, setter i s�fall User og Identity objektet
     * @param request
     * @return
     * @throws SystemException
     */
    public static SecuritySession getInstance(HttpServletRequest request) throws SystemException {

        SecuritySession session = (SecuritySession) request.getSession(true).getAttribute("aksess.securitySession");
        if (session == null) {
            session = createNewInstance();
            request.getSession(true).setAttribute("aksess.securitySession", session);
        }

        IdentityResolver resolver = session.realm.getIdentityResolver();
        Identity identity = null;
        try {
            Identity fakeIdentity = getFakeIdentity();
            if(fakeIdentity != null) {
                identity = fakeIdentity;

            } else {
                identity = resolver.getIdentity(request);
            }
        } catch (IdentificationFailedException e) {
            throw new SystemException(SOURCE, "IdentificationFailedException", e);
        }
        Identity currentIdentity = session.identity;

        // If your is now logged in or has changed identity, must create new session
        if (identity != null && (currentIdentity == null || !identity.getUserId().equals(currentIdentity.getUserId()) || !identity.getDomain().equals(currentIdentity.getDomain()))) {
            session = createNewUserInstance(identity);
            try {
                session.handlePostLogin(request);
            } catch (ConfigurationException e) {
                throw new SystemException(SOURCE, "Konfigurasjonsfeil", e);
            }
            // Innloggede brukere har lengre sesjonstimeout
            if (request.getSession().getMaxInactiveInterval() < Aksess.getSecuritySessionTimeout()) {
                request.getSession().setMaxInactiveInterval(Aksess.getSecuritySessionTimeout());
            }
            request.getSession(true).setAttribute("aksess.securitySession", session);

        } else if (identity == null && currentIdentity != null) {
            // Bruker er utlogget via ekstern tjeneste - lag blank sesjon           
            session = createNewInstance();
            request.getSession(true).setAttribute("aksess.securitySession", session);
            request.getSession(true).removeAttribute("adminMode");
        }

        return session;
    }

    private static Identity getFakeIdentity() {
        WebApplicationContext root = (WebApplicationContext) RootContext.getInstance();
        final String fakeUsername = root.getServletContext().getInitParameter("fakeUsername");
        final String fakeUserDomain= root.getServletContext().getInitParameter("fakeUserDomain");
        if(fakeUsername != null && fakeUserDomain != null) {
            DefaultIdentity id = new DefaultIdentity();
            id.setUserId(fakeUsername);
            id.setDomain(fakeUserDomain);
            return id;
        } else {
            return null;
        }
    }
{'fraction_non_alphanumeric': 0.09369097091046467, 'fraction_numerical': 0.0017000377786173025, 'mean_word_length': 3.5174914675767917, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3022399', 'n_tokens_mistral': 2821, 'n_tokens_neox': 2794, 'n_words': 670}