import functools


def RequireAuth(handler):
  """Decorator for webapp2 request handler methods.

  Only use on webapp2.RequestHandler methods (e.g. get, post, put),
  and only after using a 'Check____Auth' decorator.

  Expects the handler's self.request.authenticated to be not False-ish.
  If it doesn't exist or evaluates to False, 403s. Otherwise, passes
  control to the wrapped handler.
  """
  @functools.wraps(handler)
  def wrapper(self, *args, **kwargs):
    """Does the real legwork and calls the wrapped handler."""
    if not getattr(self.request, 'authenticated', None):
      self.abort(403)
    else:
      handler(self, *args, **kwargs)

  return wrapper