<commit_before>import base64

from restlib import response

import mint.client
import mint.config
import mint.shimclient

# Decorator for public (unauthenticated) methods/functions
def public(deco):
    deco.public = True
    return deco

class AuthenticationCallback(object):

    def __init__(self, cfg):
        self.cfg = cfg

    def getAuth(self, request):
        if not 'Authorization' in request.headers:
            return None
        type, user_pass = request.headers['Authorization'].split(' ', 1)
        user_name, password = base64.decodestring(user_pass).split(':', 1)
        return (user_name, password)

    def processRequest(self, request):
        auth = self.getAuth(request)
        request.auth = auth
        response = self.setMintClient(request, auth)
        # will be None if successful
        return response

    def processMethod(self, request, viewMethod, args, kwargs):
        if getattr(viewMethod, 'public', None) or request.mintAuth is not None:
            return
        if 'HTTP_X_FLASH_VERSION' in request.headers:
            return response.Response(status=403)
        return response.Response(status=401)

    def getMintConfig(self):
        return mint.config.getConfig()

    def setMintClient(self, request, auth):
        request.mintClient = None
        request.mintAuth = None

        if auth is None:
            # Not authenticated
            return

        if self.cfg.rBuilderUrl:
            mintClient = mint.client.MintClient(
                            self.cfg.rBuilderUrl % tuple(auth[:2]))
        else:
            mintCfg = self.getMintConfig()
            mintClient = mint.shimclient.ShimMintClient(mintCfg,
                                                        auth)
        mintAuth = mintClient.checkAuth()
        if not mintAuth.authorized:
            # Bad auth info<commit_msg>Force mint database connection to close at the end of each request.<commit_after>import base64

from restlib import response

import mint.client
import mint.config
import mint.shimclient

# Decorator for public (unauthenticated) methods/functions
def public(deco):
    deco.public = True
    return deco

class AuthenticationCallback(object):

    def __init__(self, cfg):
        self.cfg = cfg

    def getAuth(self, request):
        if not 'Authorization' in request.headers:
            return None
        type, user_pass = request.headers['Authorization'].split(' ', 1)
        user_name, password = base64.decodestring(user_pass).split(':', 1)
        return (user_name, password)

    def processRequest(self, request):
        auth = self.getAuth(request)
        request.auth = auth
        response = self.setMintClient(request, auth)
        # will be None if successful
        return response

    def processMethod(self, request, viewMethod, args, kwargs):
        if getattr(viewMethod, 'public', None) or request.mintAuth is not None:
            return
        if 'HTTP_X_FLASH_VERSION' in request.headers:
            return response.Response(status=403)
        return response.Response(status=401)

    def processResponse(self, request, response):
        """ Clean up resources on the way out. """
        if hasattr(self.mintClient, server):
            self.mintClient.server._server.db.close()

    def getMintConfig(self):
        return mint.config.getConfig()

    def setMintClient(self, request, auth):
        request.mintClient = None
        request.mintAuth = None

        if auth is None:
            # Not authenticated
            return

        if self.cfg.rBuilderUrl:
            mintClient = mint.client.MintClient(
                            self.cfg.rBuilderUrl % tuple(auth[:2]))
        else:
            mintCfg = self.getMintConfig()
            mintClient = mint.shimclient.ShimMintClient(mintCfg,
                                                        auth)
        mintAuth = mintClient.checkAuth()
        if not mintAuth.authorized:
            # Bad auth info
{'fraction_non_alphanumeric': 0.08414971112785732, 'fraction_numerical': 0.006531022356191912, 'mean_word_length': 2.206119162640902, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6314945', 'n_tokens_mistral': 1089, 'n_tokens_neox': 1063, 'n_words': 287}