Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend @exposed decorator to simplify and generalize recurring code checks #715

Closed
phorward opened this issue Apr 17, 2023 · 2 comments
Closed
Labels
feature New feature or request proposal

Comments

@phorward
Copy link
Member

This issue is a replacement for #618 to collect several scenarios and use-cases where an extended @exposed-decorator can be useful to configure a function's behavior, resulting in secure, less error prone and shorter code.

restrict

To avoid function definitions like this:

    @exposed
    def do_something(self, action: str):
        if not ((cuser := current.user.get()) and "root" in cuser["access"]):
            raise errors.Unauthorized()

        # ...

a restrict-attribute can be used like this:

    @exposed(restrict="root")
    def do_something(self, action: str):
        # ...

or when thinking further, using a lambda:

    @exposed(restrict=lambda user: "root" in user["access"] or "admin" in user["access"])
    def do_something(self, action: str):
        # ...

response

To avoid function definitions like this:

    @exposed
    def do_something(self, action: str):
        current.request.get().response.headers["Content-Type"] = "application/json"

        match action:
            # ...

            case _:
                raise errors.NotImplemented(f"Action {action!r} not implemented")

        return json.dumps("OKAY")

a response-attribute could be used like this.

    @exposed(response="json")
    def do_something(self, action: str):
        match action:
            # ...

            case _:
                raise errors.NotImplemented(f"Action {action!r} not implemented")

        return "OKAY"

skey

To avoid function definitions like this:

    @exposed
    def do_something(self, action: str, skey: str):
        if not securitykey.validate(skey):
            raise errors.PreconditionFailed()
        # ...

an skey-attribute can be used,

    @exposed(skey=True)
    def do_something(self, action: str):
        # ...

optionally with an lambda, evaluating the result of the securitykey.validate() function

    @exposed(skey=lambda skey: skey["user"] == current.user.get()["key"])
    def do_something(self, action: str):
        # ...
@phorward phorward added feature New feature or request proposal labels Apr 17, 2023
@phorward phorward added the viur-meeting Issues to discuss in the next ViUR meeting label Apr 25, 2023
@sveneberth
Copy link
Member

In the ViUR meeting we decided to use multiple single decorators in snake_case.

@phorward phorward removed the viur-meeting Issues to discuss in the next ViUR meeting label May 12, 2023
@sveneberth sveneberth linked a pull request Jul 7, 2023 that will close this issue
@phorward
Copy link
Member Author

Implemented by #800

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request proposal
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants