<commit_before>from flask import request
from frasco.ext import *
from frasco.models import db
from frasco.users import get_current_user, is_user_logged_in
import base64
import datetime
import hashlib
import uuid
import functools
import psycopg2
from .service import ApiAuthentificationRequiredError


class ApiKeyModelMixin(object):
    value = db.Column(db.String, index=True)
    last_accessed_at = db.Column(db.DateTime)
    last_accessed_from = db.Column(db.String)
    expires_at = db.Column(db.DateTime)


class FrascoApiKeyAuthentification(Extension):
    name = 'frasco_api_key_auth'

    def _init_app(self, app, state):
        require_extension('frasco_users', app)
        state.Model = state.import_option('model')

        @app.extensions.frasco_users.manager.request_loader
        def load_user_from_request(request):
            api_key = None
            if 'Authorization' in request.headers:
                authz = request.headers['Authorization']
                if authz.startswith('Basic '):
                    try:
                        api_key = base64.b64decode(authz[6:]).split(':')[0]
                    except:
                        return
                elif authz.startswith('Bearer '):
                    api_key = authz[7:]
            elif 'X-Api-Key' in request.headers:
                api_key = request.headers['X-Api-Key']
            if not api_key:
                return

            try:
                key = state.Model.query.filter_by(value=api_key).first()
            except:<commit_msg>Missign decode after b64encode()<commit_after>from flask import request
from frasco.ext import *
from frasco.models import db
from frasco.users import get_current_user, is_user_logged_in
import base64
import datetime
import hashlib
import uuid
import functools
import psycopg2
from .service import ApiAuthentificationRequiredError


class ApiKeyModelMixin(object):
    value = db.Column(db.String, index=True)
    last_accessed_at = db.Column(db.DateTime)
    last_accessed_from = db.Column(db.String)
    expires_at = db.Column(db.DateTime)


class FrascoApiKeyAuthentification(Extension):
    name = 'frasco_api_key_auth'

    def _init_app(self, app, state):
        require_extension('frasco_users', app)
        state.Model = state.import_option('model')

        @app.extensions.frasco_users.manager.request_loader
        def load_user_from_request(request):
            api_key = None
            if 'Authorization' in request.headers:
                authz = request.headers['Authorization']
                if authz.startswith('Basic '):
                    try:
                        api_key = base64.b64decode(authz[6:]).decode('utf-8').split(':')[0]
                    except:
                        return
                elif authz.startswith('Bearer '):
                    api_key = authz[7:]
            elif 'X-Api-Key' in request.headers:
                api_key = request.headers['X-Api-Key']
            if not api_key:
                return

            try:
                key = state.Model.query.filter_by(value=api_key).first()
            except:
{'fraction_non_alphanumeric': 0.08965738072366314, 'fraction_numerical': 0.007364713416586615, 'mean_word_length': 2.2474012474012475, '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': '2712179', 'n_tokens_mistral': 943, 'n_tokens_neox': 911, 'n_words': 184}