def endpoints_method(request_message=message_types.VoidMessage,
                     response_message=message_types.VoidMessage,
                     **kwargs):
    """Same as @endpoints.method but also adds auth state initialization code.

  Also forbids changing changing auth parameters on per-method basis, since it
  unnecessary complicates authentication code. All methods inherit properties
  set on the service level.
  """
    assert 'audiences' not in kwargs, 'Not supported'
    assert 'allowed_client_ids' not in kwargs, 'Not supported'

    orig_decorator = endpoints.method(request_message, response_message,
                                      **kwargs)

    def new_decorator(func):
        @functools.wraps(func)
        def wrapper(service, *args, **kwargs):
            try:
                initialize_request_auth(service.request_state.remote_address,
                                        service.request_state.headers)
                return func(service, *args, **kwargs)
            except api.AuthenticationError:
                raise endpoints.UnauthorizedException()
            except api.AuthorizationError:
                raise endpoints.ForbiddenException()

        return orig_decorator(wrapper)

    return new_decorator
Exemple #2
0
def endpoints_method(
    request_message=message_types.VoidMessage,
    response_message=message_types.VoidMessage,
    **kwargs):
  """Same as @endpoints.method but also adds auth state initialization code.

  Also forbids changing changing auth parameters on per-method basis, since it
  unnecessary complicates authentication code. All methods inherit properties
  set on the service level.
  """
  assert 'audiences' not in kwargs, 'Not supported'
  assert 'allowed_client_ids' not in kwargs, 'Not supported'

  orig_decorator = endpoints.method(request_message, response_message, **kwargs)
  def new_decorator(func):
    @functools.wraps(func)
    def wrapper(service, *args, **kwargs):
      try:
        initialize_request_auth(
            service.request_state.remote_address, service.request_state.headers)
        return func(service, *args, **kwargs)
      except api.AuthenticationError:
        raise endpoints.UnauthorizedException()
      except api.AuthorizationError:
        raise endpoints.ForbiddenException()
    return orig_decorator(wrapper)
  return new_decorator
def ApiMethod(request_type, response_type, **kwargs):
  """API method decorator."""
  endpoints_wrapper = endpoints.method(request_type, response_type, **kwargs)
  def _Decorator(method):
    # Wraps execution in an NDB context
    api_method = api_common.with_ndb_context(method)
    # Configures endpoint
    api_method = endpoints_wrapper(api_method)
    # Add method summary and description
    descriptor_wrapper = openapi.ApiMethodDescriptor(
        summary=method.__doc__.splitlines()[0] if method.__doc__ else None,
        description=method.__doc__)
    api_method = descriptor_wrapper(api_method)
    # Handles any uncaught exceptions
    api_method = ConvertExceptions(api_method)
    return api_method
  return _Decorator
Exemple #4
0
    def auto_api_decr(func):
        func_name = func.__name__ if not name else name
        path_name = func_name if not path else path
        func = annotated(returns=returns)(func)
        f_annotations = func.__annotations__
        f_args, f_varargs, f_kwargs, f_defaults = inspect.getargspec(func)

        if f_defaults:
            f_args_to_defaults = {f_args[len(f_args) - len(f_defaults) + n]: x for n, x in enumerate(f_defaults)}
        else:
            f_args_to_defaults = {}

        RequestMessage = f_annotations.pop('request', message_types.VoidMessage)
        ResponseMessage = f_annotations.pop('return', message_types.VoidMessage)

        RequestMessageOrContainer, request_args = annotations_to_resource_container(f_annotations, f_args_to_defaults, RequestMessage)

        sanity_check_request_message(func_name, RequestMessageOrContainer)

        ep_dec = endpoints.method(
            RequestMessageOrContainer,
            ResponseMessage,
            http_method=http_method,
            name=func_name,
            path=path_name,  #TODO: include required params,
            **kwargs
        )

        def inner(self, request):
            kwargs = {}
            for arg_name in request_args:
                if hasattr(request, arg_name):
                    kwargs[arg_name] = getattr(request, arg_name)
                if arg_name in f_args_to_defaults and kwargs.get(arg_name) is None:
                    kwargs[arg_name] = f_args_to_defaults[arg_name]

            return_val = func(self, request, **kwargs)

            # return voidmessage if the return val is none
            if ResponseMessage == message_types.VoidMessage and return_val is None:
                return message_types.VoidMessage()

            return return_val

        return ep_dec(inner)
Exemple #5
0
    def auto_api_decr(func):
        func_name = func.__name__ if not name else name
        path_name = func_name if not path else path
        func = annotated(returns=returns)(func)
        f_annotations = func.__annotations__
        f_args, f_varargs, f_kwargs, f_defaults = inspect.getargspec(func)

        if f_defaults:
            f_args_to_defaults = {f_args[len(f_args) - len(f_defaults) + n]: x for n, x in enumerate(f_defaults)}
        else:
            f_args_to_defaults = {}

        RequestMessage = f_annotations.pop('request', message_types.VoidMessage)
        ResponseMessage = f_annotations.pop('return', message_types.VoidMessage)

        RequestMessageOrContainer, request_args = annotations_to_resource_container(f_annotations, f_args_to_defaults, RequestMessage)

        sanity_check_request_message(func_name, RequestMessageOrContainer)

        ep_dec = endpoints.method(
            RequestMessageOrContainer,
            ResponseMessage,
            http_method=http_method,
            name=func_name,
            path=path_name,
            **kwargs
        )

        def inner(self, request):
            kwargs = {}
            for arg_name in request_args:
                if hasattr(request, arg_name):
                    kwargs[arg_name] = getattr(request, arg_name)
                if arg_name in f_args_to_defaults and kwargs.get(arg_name) is None:
                    kwargs[arg_name] = f_args_to_defaults[arg_name]

            return_val = func(self, request, **kwargs)

            # return voidmessage if the return val is none
            if ResponseMessage == message_types.VoidMessage and return_val is None:
                return message_types.VoidMessage()

            return return_val

        return ep_dec(inner)
 def auth_method_decorator(auth_function):
   """Decorator for auth_method."""
   kwarg_auth = None
   kwarg_permission = None
   for key in kwargs:
     if key is 'permission':
       kwarg_permission = kwargs.pop('permission')
       auth_function = permissions.check_auth(
           permission=kwarg_permission)(auth_function)
       break
     elif key is 'user_auth_only':
       kwarg_auth = kwargs.pop('user_auth_only')
       auth_function = permissions.check_auth(
           user_auth_only=kwarg_auth)(auth_function)
       break
   if not kwarg_auth and not kwarg_permission:
     raise AuthCheckNotPresent(
         'No permission or user_auth_only was passed. Authentication on this '
         'method cannot run.')
   # Always apply the standard `endpoints.method` decorator.
   return endpoints.method(*args, **kwargs)(auth_function)
Exemple #7
0
    def auth_method_decorator(auth_function):
        """Decorator for auth_method."""
        permission = kwargs.pop('permission', None)

        auth_function = _check_auth(permission)(auth_function)
        return endpoints.method(*args, **kwargs)(auth_function)