

def prepare(self):
    api_key = self.request.headers.get('X-Api-Key', None)
    require_key = self.settings.get('require_key', False)
    self.auth_mgr = self.settings.get('auth_mgr')
    key_mgr = self.key_mgr = self.settings.get('key_mgr')
    self.api_key = DummyKey()
    self.key_username = None
    if require_key:
        if (api_key is not None):
            try:
                self.api_key = Key.load(key_mgr.get_key(api_key))
            except KeyNotFound:
                raise HTTPError(403, "key %s doesn't exist", api_key)
            try:
                user_obj = self.auth_mgr.user_by_key(api_key)
                self.key_username = user_obj['username']
            except UserNotFound:
                pass
        else:
            raise HTTPError(401)

dataset/ETHPy150Open benoitc/gaffer/gaffer/gafferd/http_handlers/util.py CorsHandlerWithAuth.prepare/original