Skip to content

Commit 16f49fe

Browse files
committed
refactor SecureEntry into standard Security
1 parent d56b886 commit 16f49fe

File tree

2 files changed

+80
-60
lines changed

2 files changed

+80
-60
lines changed

tiled/server/dependencies.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33

44
import pydantic_settings
55
from fastapi import Depends, HTTPException, Query, Request, Security
6+
from fastapi.security import SecurityScopes
67
from starlette.status import HTTP_403_FORBIDDEN, HTTP_404_NOT_FOUND
78

9+
from tiled.adapters.mapping import MapAdapter
10+
from tiled.structures.core import StructureFamily
11+
812
from ..media_type_registration import (
913
default_deserialization_registry,
1014
default_serialization_registry,
1115
)
1216
from ..query_registration import default_query_registry
1317
from ..validation_registration import default_validation_registry
14-
from .authentication import get_current_principal, get_session_state
18+
from .authentication import check_scopes, get_current_principal, get_session_state
1519
from .core import NoEntry
1620
from .utils import filter_for_access, record_timing
1721

@@ -53,14 +57,16 @@ def get_root_tree():
5357
)
5458

5559

56-
def SecureEntry(scopes, structure_families=None):
60+
def get_entry(structure_families: Optional[set[StructureFamily]] = None):
5761
async def inner(
5862
path: str,
5963
request: Request,
64+
security_scopes: SecurityScopes,
6065
principal: str = Depends(get_current_principal),
6166
root_tree: pydantic_settings.BaseSettings = Depends(get_root_tree),
6267
session_state: dict = Depends(get_session_state),
63-
):
68+
_ = Security(check_scopes)
69+
) -> MapAdapter:
6470
"""
6571
Obtain a node in the tree from its path.
6672
@@ -131,7 +137,7 @@ async def inner(
131137
allowed_scopes = await access_policy.allowed_scopes(
132138
entry_with_access_policy, principal, path_parts_relative
133139
)
134-
if not set(scopes).issubset(allowed_scopes):
140+
if not set(security_scopes.scopes).issubset(allowed_scopes):
135141
if "read:metadata" not in allowed_scopes:
136142
# If you can't read metadata, it does not exist for you.
137143
raise NoEntry(path_parts)
@@ -142,7 +148,7 @@ async def inner(
142148
status_code=HTTP_403_FORBIDDEN,
143149
detail=(
144150
"Not enough permissions to perform this action on this node. "
145-
f"Requires scopes {scopes}. "
151+
f"Requires scopes {security_scopes.scopes}. "
146152
f"Principal had scopes {list(allowed_scopes)} on this node."
147153
),
148154
)
@@ -164,7 +170,7 @@ async def inner(
164170
),
165171
)
166172

167-
return Security(inner, scopes=scopes)
173+
return inner
168174

169175

170176
def block(

0 commit comments

Comments
 (0)