Decorate a HTTP verb method and check the request is authenticated

    :param method: a tornado.web.RequestHandler method
    :param validator: a token validation coroutine, that should return
    True/False depending if token is or is not valid

def _auth_required(method, validator):
    """Decorate a HTTP verb method and check the request is authenticated

    :param method: a tornado.web.RequestHandler method
    :param validator: a token validation coroutine, that should return
    True/False depending if token is or is not valid
    """
    @gen.coroutine
    @functools.wraps(method)
    def wrapper(self, *args, **kwargs):
        token = _get_token(self.request)
        valid_token = yield validator(token)
        if not valid_token:
            message = 'Invalid token: {}'.format(token)
            logging.warning(message)
            raise HTTPError(401, message)
        result = method(self, *args, **kwargs)

        if isinstance(result, Future):
            result = yield result

        raise gen.Return(result)

    return wrapper