<commit_before>""" Cornice services.
"""
from cornice import Service
from corniceapp.models import User, DBSession, APIKey
from corniceapp.validators import validate_generic
from pyramid.security import (
    authenticated_userid,
    remember,
    forget,
)
from webob import Response
import hashlib
import json
import uuid


login = Service(name='users', path='/users/login', description='User login endpoints')


def gen_apikey():
    """
        Generate a unique api key
    """
    for _ in range(10):
        newkey = str(uuid.uuid5()).replace('-', '')
        if DBSession.query(APIKey).filter(APIKey.apikey==newkey).count() == 0:
            return newkey
    raise Exception("can't make a unique key... wat")


@login.post(validators=validate_generic)
def login_user(request):
    """
        login a user
        privs: none
        {"username": "name", "password": "<PASSWORD>"}
    """
    password = <PASSWORD>(request.validated['password']).<PASSWORD>()
    username = request.validated['username']
    try:
        user = DBSession.query(User).filter(User.name==username).one()
    except:
        return {"success": False}
    if user and user.password == password:
        headers = remember(request, user.email)
        resp = Response(json.dumps({"success": True}))
        resp.headerlist.extend(headers)<commit_msg>fixed the call to uuid.uuid5<commit_after>""" Cornice services.
"""
from cornice import Service
from corniceapp.models import User, DBSession, APIKey
from corniceapp.validators import validate_generic
from pyramid.security import (
    authenticated_userid,
    remember,
    forget,
)
from webob import Response
import hashlib
import json
import uuid


login = Service(name='users', path='/users/login', description='User login endpoints')


def gen_apikey():
    """
        Generate a unique api key
    """
    for _ in range(10):
        newkey = str(uuid.uuid5(uuid.NAMESPACE_DNS, 'spacehub.org')).replace('-', '')
        if DBSession.query(APIKey).filter(APIKey.apikey==newkey).count() == 0:
            return newkey
    raise Exception("can't make a unique key... wat")


@login.post(validators=validate_generic)
def login_user(request):
    """
        login a user
        privs: none
        {"username": "name", "password": "<PASSWORD>"}
    """
    password = hashlib.sha512(request.validated['password']).hexdigest()
    username = request.validated['username']
    try:
        user = DBSession.query(User).filter(User.name==username).one()
    except:
        return {"success": False}
    if user and user.password == password:
        headers = remember(request, user.email)
        resp = Response(json.dumps({"success": True}))
        resp.headerlist.extend(headers)
{'fraction_non_alphanumeric': 0.1338958180484226, 'fraction_numerical': 0.004402054292002935, 'mean_word_length': 3.3842443729903535, 'pattern_counts': {'":': 8, '<': 7, '<?xml version=': 0, '>': 7, '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': '3424505', 'n_tokens_mistral': 802, 'n_tokens_neox': 790, 'n_words': 200}