mirror of
https://github.com/rommapp/romm.git
synced 2026-06-30 07:45:52 +00:00
Instead of using just strings, this change converts the scopes to a `StrEnum`, to be compatible with places where a string is expected. This avoids typos when using these scopes, simplifies searching for usages, and improves type hints. An extra change was the fix to the Firmware download endpoint, which wasn't respecting the `DISABLE_DOWNLOAD_ENDPOINT_AUTH` flag.
47 lines
1.1 KiB
Python
47 lines
1.1 KiB
Python
from typing import Any
|
|
|
|
from fastapi import Security
|
|
from fastapi.security.http import HTTPBasic
|
|
from fastapi.security.oauth2 import OAuth2PasswordBearer
|
|
from fastapi.types import DecoratedCallable
|
|
from handler.auth.base_handler import (
|
|
DEFAULT_SCOPES_MAP,
|
|
FULL_SCOPES_MAP,
|
|
WRITE_SCOPES_MAP,
|
|
Scope,
|
|
)
|
|
from starlette.authentication import requires
|
|
|
|
oauth2_password_bearer = OAuth2PasswordBearer(
|
|
tokenUrl="/token",
|
|
auto_error=False,
|
|
scopes={
|
|
**DEFAULT_SCOPES_MAP,
|
|
**WRITE_SCOPES_MAP,
|
|
**FULL_SCOPES_MAP,
|
|
},
|
|
)
|
|
|
|
|
|
def protected_route(
|
|
method: Any,
|
|
path: str,
|
|
scopes: list[Scope] | None = None,
|
|
**kwargs,
|
|
):
|
|
def decorator(func: DecoratedCallable):
|
|
fn = requires(scopes or [])(func)
|
|
return method(
|
|
path,
|
|
dependencies=[
|
|
Security(
|
|
dependency=oauth2_password_bearer,
|
|
scopes=scopes or [],
|
|
),
|
|
Security(dependency=HTTPBasic(auto_error=False)),
|
|
],
|
|
**kwargs,
|
|
)(fn)
|
|
|
|
return decorator
|