mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
Merge branch 'fix-clean-auth-docs' into pu
This commit is contained in:
commit
522177e813
1 changed files with 25 additions and 21 deletions
|
@ -120,35 +120,39 @@ class BasicAuthBackend(AuthenticationBackend):
|
||||||
return (AuthCredentials(["authenticated"]), user)
|
return (AuthCredentials(["authenticated"]), user)
|
||||||
|
|
||||||
|
|
||||||
def auth_required(is_required: bool = True):
|
def auth_required(auth_goal: bool = True):
|
||||||
""" Authentication route decorator.
|
""" Enforce a user's authentication status, bringing them to the login page
|
||||||
|
or homepage if their authentication status does not match the goal.
|
||||||
|
|
||||||
:param is_required: A boolean indicating whether the function requires auth
|
:param auth_goal: Whether authentication is required or entirely disallowed
|
||||||
:param status_code: An optional status_code for template render.
|
for a user to perform this request.
|
||||||
Redirects are always SEE_OTHER.
|
:return: Return the FastAPI function this decorator wraps.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def decorator(func):
|
def decorator(func):
|
||||||
@functools.wraps(func)
|
@functools.wraps(func)
|
||||||
async def wrapper(request, *args, **kwargs):
|
async def wrapper(request, *args, **kwargs):
|
||||||
if request.user.is_authenticated() != is_required:
|
if request.user.is_authenticated() == auth_goal:
|
||||||
url = "/"
|
return await func(request, *args, **kwargs)
|
||||||
|
|
||||||
if is_required:
|
url = "/"
|
||||||
if request.method == "GET":
|
if auth_goal is False:
|
||||||
url = request.url.path
|
return RedirectResponse(url, status_code=int(HTTPStatus.SEE_OTHER))
|
||||||
elif request.method == "POST" and (referer := request.headers.get("Referer")):
|
|
||||||
aur = aurweb.config.get("options", "aur_location") + "/"
|
|
||||||
if not referer.startswith(aur):
|
|
||||||
_ = l10n.get_translator_for_request(request)
|
|
||||||
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST,
|
|
||||||
detail=_("Bad Referer header."))
|
|
||||||
url = referer[len(aur) - 1:]
|
|
||||||
|
|
||||||
url = "/login?" + util.urlencode({"next": url})
|
# Use the request path when the user can visit a page directly but
|
||||||
return RedirectResponse(url,
|
# is not authenticated and use the Referer header if visiting the
|
||||||
status_code=int(HTTPStatus.SEE_OTHER))
|
# page itself is not directly possible (e.g. submitting a form).
|
||||||
return await func(request, *args, **kwargs)
|
if request.method in ("GET", "HEAD"):
|
||||||
|
url = request.url.path
|
||||||
|
elif (referer := request.headers.get("Referer")):
|
||||||
|
aur = aurweb.config.get("options", "aur_location") + "/"
|
||||||
|
if not referer.startswith(aur):
|
||||||
|
_ = l10n.get_translator_for_request(request)
|
||||||
|
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST,
|
||||||
|
detail=_("Bad Referer header."))
|
||||||
|
url = referer[len(aur) - 1:]
|
||||||
|
url = "/login?" + util.urlencode({"next": url})
|
||||||
|
return RedirectResponse(url, status_code=int(HTTPStatus.SEE_OTHER))
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
return decorator
|
return decorator
|
||||||
|
|
Loading…
Add table
Reference in a new issue