コード例 #1
0
    def decorated(self, *args, **kwargs):
        """
        """

        # Fetch auth options from args
        auth_options = {}
        nipap_args = {}

        # validate function arguments
        if len(args) == 1:
            nipap_args = args[0]
        else:
            #logger.info("Malformed request: got %d parameters" % len(args))
            raise Fault(
                1000,
                ("NIPAP API functions take exactly 1 argument (%d given)") %
                len(args))

        if type(nipap_args) != dict:
            raise Fault(1000, (
                "Function argument must be XML-RPC struct/Python dict (Python %s given)."
                % type(nipap_args).__name__))

        # fetch auth options
        try:
            auth_options = nipap_args['auth']
            if type(auth_options) is not dict:
                raise ValueError()
        except (KeyError, ValueError):
            raise Fault(1000,
                        ("Missing/invalid authentication options in request."))

        # fetch authoritative source
        try:
            auth_source = auth_options['authoritative_source']
        except KeyError:
            raise Fault(1000,
                        ("Missing authoritative source in auth options."))

        if not request.authorization:
            return authenticate()

        # init AuthFacory()
        af = AuthFactory()
        auth = af.get_auth(request.authorization.username,
                           request.authorization.password, auth_source,
                           auth_options or {})

        # authenticated?
        if not auth.authenticate():
            raise Fault(1510, ("Incorrect username or password."))

        # Replace auth options in API call arguments with auth object
        new_args = dict(args[0])
        new_args['auth'] = auth

        return f(self, *(new_args, ), **kwargs)
コード例 #2
0
    def decorated(self, *args, **kwargs):
        """
        """
        # small hack
        args = args + (combine_request_args(), )

        # Fetch auth options from args
        auth_options = {}
        nipap_args = {}

        # validate function arguments
        if len(args) == 1:
            nipap_args = args[0]
        else:
            self.logger.debug("Malformed request: got %d parameters" %
                              len(args))
            abort(401,
                  error={
                      "code": 401,
                      "message":
                      "Malformed request: got %d parameters" % len(args)
                  })

        if type(nipap_args) != dict:
            self.logger.debug("Function argument is not struct")
            abort(401,
                  error={
                      "code": 401,
                      "message": "Function argument is not struct"
                  })

        # fetch auth options
        try:
            auth_options = nipap_args['auth']
            if type(auth_options) is not dict:
                raise ValueError()
        except (KeyError, ValueError):
            self.logger.debug(
                "Missing/invalid authentication options in request.")
            abort(401,
                  error={
                      "code":
                      401,
                      "message":
                      "Missing/invalid authentication options in request."
                  })

        # fetch authoritative source
        try:
            auth_source = auth_options['authoritative_source']
        except KeyError:
            self.logger.debug("Missing authoritative source in auth options.")
            abort(401,
                  error={
                      "code": 401,
                      "message":
                      "Missing authoritative source in auth options."
                  })

        if not request.authorization:
            return authenticate()

        # init AuthFacory()
        af = AuthFactory()
        auth = af.get_auth(request.authorization.username,
                           request.authorization.password, auth_source,
                           auth_options or {})

        # authenticated?
        if not auth.authenticate():
            self.logger.debug("Incorrect username or password.")
            abort(401,
                  error={
                      "code": 401,
                      "message": "Incorrect username or password"
                  })

        # Replace auth options in API call arguments with auth object
        new_args = dict(args[0])
        new_args['auth'] = auth

        return f(self, *(new_args, ), **kwargs)