mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
feat(FastAPI): support {named} fmt in auth_required redirect
Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
parent
e5299b5ed4
commit
75c49e4f8a
1 changed files with 11 additions and 1 deletions
|
@ -1,4 +1,5 @@
|
|||
import functools
|
||||
import re
|
||||
|
||||
from datetime import datetime
|
||||
from http import HTTPStatus
|
||||
|
@ -121,6 +122,7 @@ class BasicAuthBackend(AuthenticationBackend):
|
|||
|
||||
|
||||
def auth_required(is_required: bool = True,
|
||||
login: bool = False,
|
||||
redirect: str = "/",
|
||||
template: tuple = None,
|
||||
status_code: HTTPStatus = HTTPStatus.UNAUTHORIZED):
|
||||
|
@ -162,8 +164,16 @@ def auth_required(is_required: bool = True,
|
|||
async def wrapper(request, *args, **kwargs):
|
||||
if request.user.is_authenticated() != is_required:
|
||||
url = "/"
|
||||
|
||||
if redirect:
|
||||
url = redirect
|
||||
path_params_expr = re.compile(r'\{(\w+)\}')
|
||||
match = re.findall(path_params_expr, redirect)
|
||||
args = {k: request.path_params.get(k) for k in match}
|
||||
url = redirect.format(**args)
|
||||
|
||||
if login:
|
||||
url = "/login?" + util.urlencode({"next": url})
|
||||
|
||||
if template:
|
||||
# template=("template.html",
|
||||
# ["Some Title", "someFormatted {}"],
|
||||
|
|
Loading…
Add table
Reference in a new issue