<commit_before>/*
 * oxAuth is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

package org.xdi.oxauth.service;

import com.unboundid.ldap.sdk.Filter;
import com.unboundid.util.StaticUtils;
import org.apache.commons.lang.StringUtils;
import org.gluu.site.ldap.persistence.LdapEntryManager;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.contexts.Lifecycle;
import org.jboss.seam.log.Log;
import org.xdi.oxauth.model.common.Prompt;
import org.xdi.oxauth.model.common.SessionId;
import org.xdi.oxauth.model.common.SessionIdState;
import org.xdi.oxauth.model.config.ConfigurationFactory;
import org.xdi.oxauth.model.util.Util;
import org.xdi.util.StringHelper;

import javax.faces.context.FacesContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author <NAME>
 * @author <NAME>
 * @version 0.9, 20/12/2012
 */

@Scope(ScopeType.STATELESS)
@Name("sessionIdService")
@AutoCreate
public class SessionIdService {

    private static final String SESSION_ID_COOKIE_NAME = "session_id";
    private static final String STORED_ORIGIN_PARAMETERS = "stored_origin_parameters";

    @Logger
    private Log log;

    @In
    private LdapEntryManager ldapEntryManager;

    public static SessionIdService instance() {
        if (!Contexts.isEventContextActive() && !Contexts.isApplicationContextActive()) {
            Lifecycle.beginCall();
        }
        return (SessionIdService) Component.getInstance(SessionIdService.class);
    }


    // #34 - update session attributes with each request
    // https://github.com/GluuFederation/oxAuth/issues/34
    public SessionId updateSessionIfNeeded(SessionId session, String scope, String clientId, String redirectUri, String acrValuesStr, HttpServletResponse httpResponse) {
        if (session != null && !session.getSessionAttributes().isEmpty()) {

            final Map<String, String> sessionAttributes = session.getSessionAttributes();

            boolean isAcrChanged = acrValuesStr !=null && !acrValuesStr.equals(sessionAttributes.get("acr_values"));
            boolean isClientChanged = clientId != null && !clientId.equals(sessionAttributes.get("client_id"));
            boolean isScopeChanged = scope !=null && !scope.equals(sessionAttributes.get("scope"));
            if (isAcrChanged || isClientChanged || isScopeChanged) {
                removeSessionIdCookie(httpResponse);
                return null; // kill session to force recreation
            }

            sessionAttributes.put("redirect_uri", redirectUri);
            session.setSessionAttributes(sessionAttributes);

            boolean updateResult = updateSessionId(session, true, true);
            if (!updateResult) {
                log.debug("Failed to update session entry: '{0}'", session.getId());
            }
        }
        return session;
    }

    public String getSessionIdFromCookie(HttpServletRequest request) {
        try {
            final Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equals(SESSION_ID_COOKIE_NAME) /*&& cookie.getSecure()*/) {
                        log.trace("Found session_id cookie: '{0}'", cookie.getValue());
                        return cookie.getValue();
                    }<commit_msg>oxauth : #34 kill session only if acr was changed. Client id change is handled via permission map for clients.<commit_after>/*
 * oxAuth is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

package org.xdi.oxauth.service;

import com.unboundid.ldap.sdk.Filter;
import com.unboundid.util.StaticUtils;
import org.apache.commons.lang.StringUtils;
import org.gluu.site.ldap.persistence.LdapEntryManager;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.contexts.Lifecycle;
import org.jboss.seam.log.Log;
import org.xdi.oxauth.model.common.Prompt;
import org.xdi.oxauth.model.common.SessionId;
import org.xdi.oxauth.model.common.SessionIdState;
import org.xdi.oxauth.model.config.ConfigurationFactory;
import org.xdi.oxauth.model.util.Util;
import org.xdi.util.StringHelper;

import javax.faces.context.FacesContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author <NAME>
 * @author <NAME>
 * @version 0.9, 20/12/2012
 */

@Scope(ScopeType.STATELESS)
@Name("sessionIdService")
@AutoCreate
public class SessionIdService {

    private static final String SESSION_ID_COOKIE_NAME = "session_id";
    private static final String STORED_ORIGIN_PARAMETERS = "stored_origin_parameters";

    @Logger
    private Log log;

    @In
    private LdapEntryManager ldapEntryManager;

    public static SessionIdService instance() {
        if (!Contexts.isEventContextActive() && !Contexts.isApplicationContextActive()) {
            Lifecycle.beginCall();
        }
        return (SessionIdService) Component.getInstance(SessionIdService.class);
    }


    // #34 - update session attributes with each request
    // https://github.com/GluuFederation/oxAuth/issues/34
    public SessionId updateSessionIfNeeded(SessionId session, String scope, String clientId, String redirectUri, String acrValuesStr, HttpServletResponse httpResponse) {
        if (session != null && !session.getSessionAttributes().isEmpty()) {

            final Map<String, String> sessionAttributes = session.getSessionAttributes();

            boolean isAcrChanged = acrValuesStr !=null && !acrValuesStr.equals(sessionAttributes.get("acr_values"));
//            boolean isClientChanged = clientId != null && !clientId.equals(sessionAttributes.get("client_id"));
//            boolean isScopeChanged = scope !=null && !scope.equals(sessionAttributes.get("scope"));
            if (isAcrChanged /*|| isClientChanged || isScopeChanged()*/) {
                removeSessionIdCookie(httpResponse);
                remove(session);
                return null; // kill session to force recreation
            }

            sessionAttributes.put("redirect_uri", redirectUri);
            session.setSessionAttributes(sessionAttributes);

            boolean updateResult = updateSessionId(session, true, true);
            if (!updateResult) {
                log.debug("Failed to update session entry: '{0}'", session.getId());
            }
        }
        return session;
    }

    public String getSessionIdFromCookie(HttpServletRequest request) {
        try {
            final Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equals(SESSION_ID_COOKIE_NAME) /*&& cookie.getSecure()*/) {
                        log.trace("Found session_id cookie: '{0}'", cookie.getValue());
                        return cookie.getValue();
                    }
{'fraction_non_alphanumeric': 0.11184703824043989, 'fraction_numerical': 0.006248437890527368, 'mean_word_length': 3.337669376693767, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '4939704', 'n_tokens_mistral': 2329, 'n_tokens_neox': 2247, 'n_words': 477}