<commit_before># -*- coding: utf-8 -*-

"""
byceps.services.authentication.session.service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2016 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

from uuid import uuid4

from ..exceptions import AuthenticationFailed

from .models import SessionToken


def create_session_token(user_id, created_at):
    """Create, but do not persist, a session token entity."""
    token = _generate_auth_token()

    return SessionToken(token, user_id, created_at)


def update_session_token(session_token, updated_at):
    """Update, but do not persist, the session token entity."""
    session_token.token = _generate_auth_token()
    session_token.created_at = updated_at


def _generate_auth_token():
    """Generate an authentication token."""
    return uuid4()


def find_session_token(token):
    """Return the session token with that ID, or `None` if not found."""
    return SessionToken.query.get(token)


def find_session_token_for_user(user_id):
    """Return the session token for the user with that ID, or `None` if
    not found.
    """
    return SessionToken.query \
        .filter_by(user_id=user_id) \
        .one_or_none()


def authenticate_session(user_id, auth_token):
    """Check the client session's validity.

    Return the nothing on success, or raise an exception on failure.
    """
    if user_id is None:
        # User ID must not be empty.
        raise AuthenticationFailed()

    if not auth_token:<commit_msg>Fix docstring<commit_after># -*- coding: utf-8 -*-

"""
byceps.services.authentication.session.service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2016 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

from uuid import uuid4

from ..exceptions import AuthenticationFailed

from .models import SessionToken


def create_session_token(user_id, created_at):
    """Create, but do not persist, a session token entity."""
    token = _generate_auth_token()

    return SessionToken(token, user_id, created_at)


def update_session_token(session_token, updated_at):
    """Update, but do not persist, the session token entity."""
    session_token.token = _generate_auth_token()
    session_token.created_at = updated_at


def _generate_auth_token():
    """Generate an authentication token."""
    return uuid4()


def find_session_token(token):
    """Return the session token with that ID, or `None` if not found."""
    return SessionToken.query.get(token)


def find_session_token_for_user(user_id):
    """Return the session token for the user with that ID, or `None` if
    not found.
    """
    return SessionToken.query \
        .filter_by(user_id=user_id) \
        .one_or_none()


def authenticate_session(user_id, auth_token):
    """Check the client session's validity.

    Return nothing on success, or raise an exception on failure.
    """
    if user_id is None:
        # User ID must not be empty.
        raise AuthenticationFailed()

    if not auth_token:
{'fraction_non_alphanumeric': 0.1265738899933731, 'fraction_numerical': 0.00728959575878065, 'mean_word_length': 3.861513687600644, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4257430', 'n_tokens_mistral': 906, 'n_tokens_neox': 863, 'n_words': 277}