<commit_before>
from functools import wraps

from flask import request
from flask import Response

from slingrpm.utils import iam

class NoOpAuth(object):

    def __init__(self):
        pass

    def authenticate(self, wrapped):
        @wraps(wrapped)
        def wrapper(*args, **kwargs):
            return wrapped(*args, **kwargs)
        return wrapper

class IAMPassthroughAuth(object):

    def __init__(self, allowed_users=None, allowed_access_keys=None,
                 allowed_accounts=None):
        if allowed_users is None:
            allowed_users = []
        if allowed_access_keys is None:
            allowed_access_keys = []
        if allowed_accounts is None:
            allowed_accounts = []

        self.allowed_users = allowed_users
        self.allowed_access_keys = allowed_access_keys
        self.allowed_accounts = allowed_accounts

    def validate(self, query):
        try:
            identity = iam.execute_get_user_query(query)
            if len(self.allowed_accounts) > 0 and identity['account'] not in self.allowed_accounts:
                return False
            if len(self.allowed_access_keys) > 0 or len(self.allowed_users) > 0:
                key_is_authorized = self.allowed_access_keys.__contains__(identity['user_id'])
                user_is_authorized = self.allowed_users.__contains__(identity['user_name'])
                return key_is_authorized or user_is_authorized
        except (Exception) as exc:
            return False

    def authenticate(self, wrapped):
        @wraps(wrapped)
        def wrapper(*args, **kwargs):
            auth = request.headers.get('X-Slingrpm-Authorization')
            print auth
            if not self.validate(auth):
                print 'bogus'
                return Response('Not Authorized', 401, {'IAMPassthroughAuth': 'Invalid'})
            return wrapped(*args, **kwargs)
        return wrapper<commit_msg>jsonify all the things<commit_after>
from functools import wraps

from flask import request
from flask import jsonify

from slingrpm.utils import iam

class NoOpAuth(object):

    def __init__(self):
        pass

    def authenticate(self, wrapped):
        @wraps(wrapped)
        def wrapper(*args, **kwargs):
            return wrapped(*args, **kwargs)
        return wrapper

class IAMPassthroughAuth(object):

    def __init__(self, allowed_users=None, allowed_access_keys=None,
                 allowed_accounts=None):
        if allowed_users is None:
            allowed_users = []
        if allowed_access_keys is None:
            allowed_access_keys = []
        if allowed_accounts is None:
            allowed_accounts = []

        self.allowed_users = allowed_users
        self.allowed_access_keys = allowed_access_keys
        self.allowed_accounts = allowed_accounts

    def validate(self, query):
        try:
            identity = iam.execute_get_user_query(query)
            if len(self.allowed_accounts) > 0 and identity['account'] not in self.allowed_accounts:
                return False
            if len(self.allowed_access_keys) > 0 or len(self.allowed_users) > 0:
                key_is_authorized = self.allowed_access_keys.__contains__(identity['user_id'])
                user_is_authorized = self.allowed_users.__contains__(identity['user_name'])
                return key_is_authorized or user_is_authorized
        except (Exception) as exc:
            return False

    def authenticate(self, wrapped):
        @wraps(wrapped)
        def wrapper(*args, **kwargs):
            auth = request.headers.get('X-Slingrpm-Authorization')
            print auth
            if not self.validate(auth):
                resp = jsonify({'Not Authorized': 'IAMPassthroughAuth Invalid'})
                resp.status_code = 401
                return resp
            return wrapped(*args, **kwargs)
        return wrapper
{'fraction_non_alphanumeric': 0.08036175710594315, 'fraction_numerical': 0.0031007751937984496, 'mean_word_length': 2.354419410745234, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '7522410', 'n_tokens_mistral': 1073, 'n_tokens_neox': 1027, 'n_words': 253}