mirror of
https://github.com/lnbits/lnbits.git
synced 2025-05-29 17:19:22 +02:00
fix: redirect for all paths of the extension (not only for /api
) (#2120)
* fix: redirect for all paths of the extension (not only for `/api`) * refactor: rename variable * fix: corner case for root path * refactor: rename var * doc: update comment * fix: do not redirect for static resources
This commit is contained in:
parent
4c4f0922a4
commit
3b0024bcf2
@ -25,47 +25,46 @@ class InstalledExtensionMiddleware:
|
|||||||
self.app = app
|
self.app = app
|
||||||
|
|
||||||
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
||||||
if "path" not in scope:
|
full_path = scope.get("path", "/")
|
||||||
|
if full_path == "/":
|
||||||
await self.app(scope, receive, send)
|
await self.app(scope, receive, send)
|
||||||
return
|
return
|
||||||
|
|
||||||
path_elements = scope["path"].split("/")
|
top_path, *rest = [p for p in full_path.split("/") if p]
|
||||||
if len(path_elements) > 2:
|
|
||||||
_, path_name, path_type, *rest = path_elements
|
|
||||||
else:
|
|
||||||
_, path_name = path_elements
|
|
||||||
path_type = None
|
|
||||||
rest = []
|
|
||||||
|
|
||||||
headers = scope.get("headers", [])
|
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 top_path in settings.lnbits_deactivated_extensions:
|
||||||
response = self._response_by_accepted_type(
|
response = self._response_by_accepted_type(
|
||||||
headers, f"Extension '{path_name}' disabled", HTTPStatus.NOT_FOUND
|
headers, f"Extension '{top_path}' disabled", HTTPStatus.NOT_FOUND
|
||||||
)
|
)
|
||||||
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(top_path, scope):
|
||||||
response = self._response_by_accepted_type(
|
response = self._response_by_accepted_type(
|
||||||
headers, "User not authorized.", HTTPStatus.FORBIDDEN
|
headers, "User not authorized.", HTTPStatus.FORBIDDEN
|
||||||
)
|
)
|
||||||
await response(scope, receive, send)
|
await response(scope, receive, send)
|
||||||
return
|
return
|
||||||
|
|
||||||
# re-route API trafic if the extension has been upgraded
|
# static resources do not require redirect
|
||||||
if path_type == "api":
|
if rest[0:1] == ["static"]:
|
||||||
upgraded_extensions = list(
|
await self.app(scope, receive, send)
|
||||||
filter(
|
return
|
||||||
lambda ext: ext.endswith(f"/{path_name}"),
|
|
||||||
settings.lnbits_upgraded_extensions,
|
upgrade_path = next(
|
||||||
)
|
(
|
||||||
)
|
e
|
||||||
if len(upgraded_extensions) != 0:
|
for e in settings.lnbits_upgraded_extensions
|
||||||
upgrade_path = upgraded_extensions[0]
|
if e.endswith(f"/{top_path}")
|
||||||
tail = "/".join(rest)
|
),
|
||||||
scope["path"] = f"/upgrades/{upgrade_path}/{path_type}/{tail}"
|
None,
|
||||||
|
)
|
||||||
|
# re-route all trafic if the extension has been upgraded
|
||||||
|
if upgrade_path:
|
||||||
|
tail = "/".join(rest)
|
||||||
|
scope["path"] = f"/upgrades/{upgrade_path}/{tail}"
|
||||||
|
|
||||||
await self.app(scope, receive, send)
|
await self.app(scope, receive, send)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user