feat(auth): add requires_{auth,guest} decorators

These new decorators are meant to be used without any arguments
and provide aliases to auth_required:
- `auth_required(True) -> requires_auth`
- `auth_required(False) -> requires_guest`

These decorators should be used without arguments, e.g.:

    @router.get("/")
    @requires_guest
    async def my_route(request: Request):
        return HTMLResponse()

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2022-01-02 16:14:15 -08:00
parent 3e048e9675
commit 51b60f4210
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
8 changed files with 82 additions and 58 deletions

View file

@ -7,7 +7,7 @@ from fastapi import HTTPException
from sqlalchemy.exc import IntegrityError
from aurweb import config, db
from aurweb.auth import AnonymousUser, BasicAuthBackend, account_type_required, auth_required
from aurweb.auth import AnonymousUser, BasicAuthBackend, _auth_required, account_type_required
from aurweb.models.account_type import USER, USER_ID
from aurweb.models.session import Session
from aurweb.models.user import User
@ -105,7 +105,7 @@ async def test_auth_required_redirection_bad_referrer():
pass
# Get down to the nitty gritty internal wrapper.
bad_referrer_route = auth_required()(bad_referrer_route)
bad_referrer_route = _auth_required()(bad_referrer_route)
# Execute the route with a "./blahblahblah" Referer, which does not
# match aur_location; `./` has been used as a prefix to attempt to