<commit_before>import hashlib
import hmac
import logging
import time
from urlparse import urlsplit, urlunsplit

from flask import jsonify, redirect, request, url_for
from flask_login import LoginManager, login_user, logout_user, user_logged_in
from redash import models, settings
from redash.authentication import jwt_auth
from redash.authentication.org_resolving import current_org
from redash.settings.organization import settings as org_settings
from redash.tasks import record_event
from sqlalchemy.orm.exc import NoResultFound
from werkzeug.exceptions import Unauthorized

login_manager = LoginManager()
logger = logging.getLogger('authentication')


def get_login_url(external=False, next="/"):
    if settings.MULTI_ORG and current_org == None:
        login_url = '/'
    elif settings.MULTI_ORG:
        login_url = url_for('redash.login', org_slug=current_org.slug, next=next, _external=external)
    else:
        login_url = url_for('redash.login', next=next, _external=external)

    return login_url


def sign(key, path, expires):
    if not key:
        return None

    h = hmac.new(str(key), msg=path, digestmod=hashlib.sha1)
    h.update(str(expires))

    return h.hexdigest()


@login_manager.user_loader
def load_user(user_id_with_identity):
    org = current_org._get_current_object()

    '''
    Users who logged in prior to https://github.com/getredash/redash/pull/3174 going live are going
    to have their (integer) user_id as their session user identifier.
    These session user identifiers will be updated the first time they visit any page so we add special
    logic to allow a frictionless transition.
    This logic will be removed 2-4 weeks after going live, and users who haven't
    visited any page during that time will simply have to log in again.
    '''

    is_legacy_session_identifier = user_id_with_identity.find('-') < 0

    if is_legacy_session_identifier:
        user_id = user_id_with_identity
    else:
        user_id, _ = user_id_with_identity.split("-")

    try:
        user = models.User.get_by_id_and_org(user_id, org)
        if user.is_disabled:
            return None

        if not is_legacy_session_identifier and user.get_id() != user_id_with_identity:
            return None

        return user
    except models.NoResultFound:
        return None


def request_loader(request):
    user = None<commit_msg>transform legacy session identifiers into new session identifiers<commit_after>import hashlib
import hmac
import logging
import time
from urlparse import urlsplit, urlunsplit

from flask import jsonify, redirect, request, url_for
from flask_login import LoginManager, login_user, logout_user, user_logged_in
from redash import models, settings
from redash.authentication import jwt_auth
from redash.authentication.org_resolving import current_org
from redash.settings.organization import settings as org_settings
from redash.tasks import record_event
from sqlalchemy.orm.exc import NoResultFound
from werkzeug.exceptions import Unauthorized

login_manager = LoginManager()
logger = logging.getLogger('authentication')


def get_login_url(external=False, next="/"):
    if settings.MULTI_ORG and current_org == None:
        login_url = '/'
    elif settings.MULTI_ORG:
        login_url = url_for('redash.login', org_slug=current_org.slug, next=next, _external=external)
    else:
        login_url = url_for('redash.login', next=next, _external=external)

    return login_url


def sign(key, path, expires):
    if not key:
        return None

    h = hmac.new(str(key), msg=path, digestmod=hashlib.sha1)
    h.update(str(expires))

    return h.hexdigest()


@login_manager.user_loader
def load_user(user_id_with_identity):
    org = current_org._get_current_object()

    '''
    Users who logged in prior to https://github.com/getredash/redash/pull/3174 going live are going
    to have their (integer) user_id as their session user identifier.
    These session user identifiers will be updated the first time they visit any page so we add special
    logic to allow a frictionless transition.
    This logic will be removed 2-4 weeks after going live, and users who haven't
    visited any page during that time will simply have to log in again.
    '''

    is_legacy_session_identifier = user_id_with_identity.find('-') < 0

    if is_legacy_session_identifier:
        user_id = user_id_with_identity
    else:
        user_id, _ = user_id_with_identity.split("-")

    try:
        user = models.User.get_by_id_and_org(user_id, org)
        if user.is_disabled:
            return None

        if is_legacy_session_identifier:
            login_user(user, remember=True)
        elif user.get_id() != user_id_with_identity:
            return None

        return user
    except models.NoResultFound:
        return None


def request_loader(request):
    user = None
{'fraction_non_alphanumeric': 0.07016460905349794, 'fraction_numerical': 0.0032921810699588477, 'mean_word_length': 3.742439024390244, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '6698729', 'n_tokens_mistral': 1464, 'n_tokens_neox': 1423, 'n_words': 435}