<commit_before>import sys
import logging
import hashlib
import cherrypy
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy import Column, Integer, String
from repoze.who.middleware import PluggableAuthenticationMiddleware
from repoze.who.plugins.auth_tkt import AuthTktCookiePlugin
from repoze.who.plugins.redirector import RedirectorPlugin
from repoze.who.classifiers import default_request_classifier
from repoze.who.classifiers import default_challenge_decider
from opmuse.jinja import env
from opmuse.database import Base

class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    login = Column(String(128), index=True, unique=True)
    password = Column(String(128))
    salt = Column(String(64))

    def __init__(self, login, password, salt):
        self.login = login
        self.password = password
        self.salt = salt

class AuthenticatedTool(cherrypy.Tool):
    def __init__(self):
        cherrypy.Tool.__init__(self, 'on_start_resource',
                               self.start, priority=20)

    def start(self):
        if ('repoze.who.identity' not in cherrypy.request.wsgi_environ or
            not cherrypy.request.wsgi_environ.get('repoze.who.identity')):
            raise cherrypy.HTTPError(401)

class JinjaAuthenticatedTool(cherrypy.Tool):
    def __init__(self):
        cherrypy.Tool.__init__(self, 'on_start_resource',
                               self.start, priority=20)

    def start(self):
        env.globals['authenticated'] = ('repoze.who.identity' in cherrypy.request.wsgi_environ and
            cherrypy.request.wsgi_environ.get('repoze.who.identity'))

class DatabaseAuthenticator(object):

    def authenticate(self, environ, identity):
        try:
            login = identity['login']
            password = identity['password']
        except KeyError:
            return None


        try:
            user = (cherrypy.request.database.query(User)
                .filter_by(login=login).one())

            hashed = hash_password(password, user.salt)

            if hashed == user.password:
                return user.login

        except NoResultFound:
            pass

        return None

def hash_password(password, salt):
    hashed = password

    # 4556 iterations of sha512
    for i in range(0, 4556):
        hashed = hashlib.sha512(("%s%s" % (hashed, salt)).encode()).hexdigest()

    return hashed

def repozewho_pipeline(app):
<commit_msg>store user.id in cherrypy session upon auth<commit_after>import sys
import logging
import hashlib
import cherrypy
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy import Column, Integer, String
from repoze.who.middleware import PluggableAuthenticationMiddleware
from repoze.who.plugins.auth_tkt import AuthTktCookiePlugin
from repoze.who.plugins.redirector import RedirectorPlugin
from repoze.who.classifiers import default_request_classifier
from repoze.who.classifiers import default_challenge_decider
from opmuse.jinja import env
from opmuse.database import Base

class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    login = Column(String(128), index=True, unique=True)
    password = Column(String(128))
    salt = Column(String(64))

    def __init__(self, login, password, salt):
        self.login = login
        self.password = password
        self.salt = salt

class AuthenticatedTool(cherrypy.Tool):
    def __init__(self):
        cherrypy.Tool.__init__(self, 'on_start_resource',
                               self.start, priority=20)

    def start(self):
        if ('repoze.who.identity' not in cherrypy.request.wsgi_environ or
            not cherrypy.request.wsgi_environ.get('repoze.who.identity')):
            raise cherrypy.HTTPError(401)

class JinjaAuthenticatedTool(cherrypy.Tool):
    def __init__(self):
        cherrypy.Tool.__init__(self, 'on_start_resource',
                               self.start, priority=20)

    def start(self):
        env.globals['authenticated'] = ('repoze.who.identity' in cherrypy.request.wsgi_environ and
            cherrypy.request.wsgi_environ.get('repoze.who.identity'))

class DatabaseAuthenticator(object):

    def authenticate(self, environ, identity):
        try:
            login = identity['login']
            password = identity['password']
        except KeyError:
            return None

        try:
            user = (cherrypy.request.database.query(User)
                .filter_by(login=login).one())

            hashed = hash_password(password, user.salt)

            if hashed == user.password:
                self.set_session(user)
                return user.login

        except NoResultFound:
            pass

        return None

    def set_session(self, user):
        cherrypy.session.acquire_lock()
        cherrypy.session['user_id'] = user.id
        cherrypy.session.release_lock()

def hash_password(password, salt):
    hashed = password

    # 4556 iterations of sha512
    for i in range(0, 4556):
        hashed = hashlib.sha512(("%s%s" % (hashed, salt)).encode()).hexdigest()

    return hashed

def repozewho_pipeline(app):

{'fraction_non_alphanumeric': 0.09339805825242718, 'fraction_numerical': 0.011650485436893204, 'mean_word_length': 2.8497757847533634, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2586121', 'n_tokens_mistral': 1572, 'n_tokens_neox': 1477, 'n_words': 338}