Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions piccolo_admin/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
from starlette.requests import Request
from starlette.responses import HTMLResponse, JSONResponse
from starlette.staticfiles import StaticFiles
from starlette.authentication import AuthCredentials, AuthenticationBackend, AuthenticationError, SimpleUser
from piccolo_api.shared.auth import UnauthenticatedUser, User

from .translations.data import TRANSLATIONS
from .translations.models import (
Expand Down Expand Up @@ -358,6 +360,7 @@ def __init__(
translations: t.List[Translation] = None,
allowed_hosts: t.Sequence[str] = [],
debug: bool = False,
auth_backend: t.Optional[AuthenticationBackend] = None,
) -> None:
super().__init__(
title=site_name,
Expand Down Expand Up @@ -653,16 +656,25 @@ def __init__(
app=StaticFiles(directory=os.path.join(ASSET_PATH, "js")),
)

auth_middleware = partial(
AuthenticationMiddleware,
backend=SessionsAuthBackend(
auth_table=auth_table,
session_table=session_table,
admin_only=True,
increase_expiry=increase_expiry,
),
on_error=handle_auth_exception,
)


if auth_backend:
auth_middleware = partial(
AuthenticationMiddleware,
backend=auth_backend,
on_error=handle_auth_exception,
)
else:
auth_middleware = partial(
AuthenticationMiddleware,
backend=SessionsAuthBackend(
auth_table=auth_table,
session_table=session_table,
admin_only=True,
increase_expiry=increase_expiry,
),
on_error=handle_auth_exception,
)

self.mount(path="/api", app=auth_middleware(private_app))
self.mount(path="/public", app=public_app)
Expand Down Expand Up @@ -952,6 +964,7 @@ def create_admin(
auto_include_related: bool = True,
allowed_hosts: t.Sequence[str] = [],
debug: bool = False,
auth_backend: t.Optional[AuthenticationBackend] = None,
):
"""
:param tables:
Expand Down Expand Up @@ -1099,4 +1112,5 @@ def create_admin(
translations=translations,
allowed_hosts=allowed_hosts,
debug=debug,
auth_backend=auth_backend
)