<commit_before>import re
import jwt
import functools
from datetime import timedelta
from flask import current_app, request
from .exceptions import AuthenticationFailure
from featuring.common.utils import now, dt_from_ts


def parse_jwt_header(header):
    m = re.match('^Bearer ([^ ]+)$', header)
    if not m:
        return None
    return m.groups()[0]


def jwt_protected(func):

    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        if 'Authorization' not in request.headers:
            raise AuthenticationFailure('missing authentication data')

        token = parse_jwt_header(request.headers['Authorization'])
        if not token:
            raise AuthenticationFailure('unaccepted authentication data')

        jwt_payload = decode_token(token)
        if not jwt_payload:
            raise AuthenticationFailure('authentication rejected')

        if 'exp' in jwt_payload:
            exp = dt_from_ts(jwt_payload['exp'])
            if exp < now():
                raise AuthenticationFailure('authentication token expired')

        request.session = jwt_payload
        return func(*args, **kwargs)

    return wrapper


def generate_token(info):
    secret = current_app.config['SECRET_KEY']
    token_duration = current_app.config['TOKEN_SECONDS']
    token_expires_on = now() + timedelta(seconds=token_duration)
    payload = {'exp': token_expires_on}
    payload.update(info)
    return jwt.encode(payload, secret, algorithm='HS256')


def decode_token(encoded):
    secret = current_app.config['SECRET_KEY']
    try:
        return jwt.decode(encoded, secret)<commit_msg>fix(api/flask/security): convert production settings type<commit_after>import re
import jwt
import functools
from datetime import timedelta
from flask import current_app, request
from .exceptions import AuthenticationFailure
from featuring.common.utils import now, dt_from_ts


def parse_jwt_header(header):
    m = re.match('^Bearer ([^ ]+)$', header)
    if not m:
        return None
    return m.groups()[0]


def jwt_protected(func):

    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        if 'Authorization' not in request.headers:
            raise AuthenticationFailure('missing authentication data')

        token = parse_jwt_header(request.headers['Authorization'])
        if not token:
            raise AuthenticationFailure('unaccepted authentication data')

        jwt_payload = decode_token(token)
        if not jwt_payload:
            raise AuthenticationFailure('authentication rejected')

        if 'exp' in jwt_payload:
            exp = dt_from_ts(jwt_payload['exp'])
            if exp < now():
                raise AuthenticationFailure('authentication token expired')

        request.session = jwt_payload
        return func(*args, **kwargs)

    return wrapper


def generate_token(info):
    secret = current_app.config['SECRET_KEY']
    token_duration = current_app.config['TOKEN_SECONDS']
    token_expires_on = now() + timedelta(seconds=int(token_duration))
    payload = {'exp': token_expires_on}
    payload.update(info)
    return jwt.encode(payload, secret, algorithm='HS256')


def decode_token(encoded):
    secret = current_app.config['SECRET_KEY']
    try:
        return jwt.decode(encoded, secret)
{'fraction_non_alphanumeric': 0.09641873278236915, 'fraction_numerical': 0.002448729721456994, 'mean_word_length': 3.41025641025641, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2014297', 'n_tokens_mistral': 949, 'n_tokens_neox': 917, 'n_words': 229}