mirror of
https://github.com/lnbits/lnbits.git
synced 2025-10-09 20:12:34 +02:00
feat: show content by accepted type
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
from typing import List, Tuple
|
from typing import Any, List, Tuple, Union
|
||||||
from urllib.parse import parse_qs
|
from urllib.parse import parse_qs
|
||||||
|
|
||||||
from fastapi.responses import HTMLResponse, JSONResponse
|
from fastapi.responses import HTMLResponse, JSONResponse
|
||||||
@@ -29,23 +29,19 @@ class InstalledExtensionMiddleware:
|
|||||||
_, path_name = path_elements
|
_, path_name = path_elements
|
||||||
path_type = None
|
path_type = None
|
||||||
|
|
||||||
|
headers = scope.get("headers", [])
|
||||||
|
|
||||||
# block path for all users if the extension is disabled
|
# block path for all users if the extension is disabled
|
||||||
if path_name in settings.lnbits_deactivated_extensions:
|
if path_name in settings.lnbits_deactivated_extensions:
|
||||||
response = JSONResponse(
|
response = self._response_by_accepted_type(
|
||||||
status_code=HTTPStatus.NOT_FOUND,
|
headers, f"Extension '{path_name}' disabled", HTTPStatus.NOT_FOUND
|
||||||
content={"detail": f"Extension '{path_name}' disabled"},
|
|
||||||
)
|
)
|
||||||
await response(scope, receive, send)
|
await response(scope, receive, send)
|
||||||
return
|
return
|
||||||
|
|
||||||
if not self._user_allowed_to_extension(path_name, scope):
|
if not self._user_allowed_to_extension(path_name, scope):
|
||||||
response = HTMLResponse(
|
response = self._response_by_accepted_type(
|
||||||
status_code=HTTPStatus.FORBIDDEN,
|
headers, "User not authorized.", HTTPStatus.FORBIDDEN
|
||||||
content=template_renderer()
|
|
||||||
.TemplateResponse(
|
|
||||||
"error.html", {"request": {}, "err": "User not authorized."}
|
|
||||||
)
|
|
||||||
.body,
|
|
||||||
)
|
)
|
||||||
await response(scope, receive, send)
|
await response(scope, receive, send)
|
||||||
return
|
return
|
||||||
@@ -81,6 +77,31 @@ class InstalledExtensionMiddleware:
|
|||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def _response_by_accepted_type(
|
||||||
|
self, headers: List[Any], msg: str, status_code: HTTPStatus
|
||||||
|
) -> Union[HTMLResponse, JSONResponse]:
|
||||||
|
accept_header: str = next(
|
||||||
|
(
|
||||||
|
h[1].decode("UTF-8")
|
||||||
|
for h in headers
|
||||||
|
if len(h) >= 2 and h[0].decode("UTF-8") == "accept"
|
||||||
|
),
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
|
||||||
|
if "text/html" in [a for a in accept_header.split(",")]:
|
||||||
|
return HTMLResponse(
|
||||||
|
status_code=status_code,
|
||||||
|
content=template_renderer()
|
||||||
|
.TemplateResponse("error.html", {"request": {}, "err": msg})
|
||||||
|
.body,
|
||||||
|
)
|
||||||
|
|
||||||
|
return JSONResponse(
|
||||||
|
status_code=status_code,
|
||||||
|
content={"detail": msg},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class ExtensionsRedirectMiddleware:
|
class ExtensionsRedirectMiddleware:
|
||||||
# Extensions are allowed to specify redirect paths.
|
# Extensions are allowed to specify redirect paths.
|
||||||
|
Reference in New Issue
Block a user