Decorate a RequestHandler or method to require that a request is authorized

    If decorating a coroutine make sure coroutine decorator is first.
    eg.::

        class Handler(tornado.web.RequestHandler):

            @authorized(validator)
            @coroutine
            def get(self):
                pass

    :param validator: a coroutine that will authorize the user associated with the token and return True/False

def authorized(validator):
    """Decorate a RequestHandler or method to require that a request is authorized

    If decorating a coroutine make sure coroutine decorator is first.
    eg.::

        class Handler(tornado.web.RequestHandler):

            @authorized(validator)
            @coroutine
            def get(self):
                pass

    :param validator: a coroutine that will authorize the user associated with the token and return True/False
    """
    def _authorized_decorator(method):
        @gen.coroutine
        def wrapper(self, *args, **kwargs):
            token = _get_token(self.request)
            authorized = yield validator(token, **kwargs)
            if not authorized:
                message = 'Token is not authorised for this action: {}'.format(token)
                logging.warning(message)
                raise HTTPError(403, message)

            result = method(self, *args, **kwargs)

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

            raise gen.Return(result)

        return wrapper
    return _authorized_decorator