Skip to content

Commit 07f69da

Browse files
DiamondJosephdanielballan
authored andcommitted
Remove get_authenticators
1 parent 4c54b4f commit 07f69da

File tree

4 files changed

+14
-30
lines changed

4 files changed

+14
-30
lines changed

tiled/server/app.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from contextlib import asynccontextmanager
1111
from functools import cache, partial
1212
from pathlib import Path
13-
from typing import Optional
13+
from typing import Optional, Union
1414

1515
import anyio
1616
import packaging.version
@@ -52,13 +52,7 @@
5252
from .dependencies import get_root_tree
5353
from .router import get_router
5454
from .settings import Settings, get_settings
55-
from .utils import (
56-
API_KEY_COOKIE_NAME,
57-
CSRF_COOKIE_NAME,
58-
get_authenticators,
59-
get_root_url,
60-
record_timing,
61-
)
55+
from .utils import API_KEY_COOKIE_NAME, CSRF_COOKIE_NAME, get_root_url, record_timing
6256

6357
SAFE_METHODS = {"GET", "HEAD", "OPTIONS", "TRACE"}
6458
SENSITIVE_COOKIES = {
@@ -134,7 +128,7 @@ def build_app(
134128
Dict of other server configuration.
135129
"""
136130
authentication = authentication or {}
137-
authenticators = {
131+
authenticators: dict[str, Union[ExternalAuthenticator, InternalAuthenticator]] = {
138132
spec["provider"]: spec["authenticator"]
139133
for spec in authentication.get("providers", [])
140134
}
@@ -354,6 +348,7 @@ async def unhandled_exception_handler(
354348
serialization_registry,
355349
deserialization_registry,
356350
validation_registry,
351+
authenticators,
357352
)
358353
app.include_router(router, prefix="/api/v1")
359354

@@ -404,10 +399,6 @@ async def unhandled_exception_handler(
404399
else:
405400
app.state.authenticated = False
406401

407-
@cache
408-
def override_get_authenticators():
409-
return authenticators
410-
411402
@cache
412403
def override_get_root_tree():
413404
return tree
@@ -733,7 +724,6 @@ async def set_cookies(request: Request, call_next):
733724
return response
734725

735726
app.openapi = partial(custom_openapi, app)
736-
app.dependency_overrides[get_authenticators] = override_get_authenticators
737727
app.dependency_overrides[get_root_tree] = override_get_root_tree
738728
app.dependency_overrides[get_settings] = override_get_settings
739729

tiled/server/authentication.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
from .core import DEFAULT_PAGE_SIZE, MAX_PAGE_SIZE, json_or_msgpack
6565
from .protocols import ExternalAuthenticator, InternalAuthenticator, UserSessionState
6666
from .settings import Settings, get_settings
67-
from .utils import API_KEY_COOKIE_NAME, get_authenticators, get_base_url
67+
from .utils import API_KEY_COOKIE_NAME, get_base_url
6868

6969
ALGORITHM = "HS256"
7070
UNIT_SECOND = timedelta(seconds=1)
@@ -237,9 +237,9 @@ async def move_api_key(request: Request, api_key: Optional[str] = Depends(get_ap
237237

238238

239239
async def get_scopes_from_api_key(
240-
api_key: str, settings: Settings, authenticators, db
240+
api_key: str, settings: Settings, authenticated: bool, db: Optional[AsyncSession]
241241
) -> Sequence[str]:
242-
if not authenticators:
242+
if not authenticated:
243243
# Tiled is in a "single user" mode with only one API key.
244244
return (
245245
USER_SCOPES
@@ -274,14 +274,16 @@ async def get_scopes_from_api_key(
274274

275275

276276
async def get_current_scopes(
277+
request: Request,
277278
decoded_access_token: Optional[dict[str, Any]] = Depends(get_decoded_access_token),
278279
api_key: Optional[str] = Depends(get_api_key),
279280
settings: Settings = Depends(get_settings),
280-
authenticators=Depends(get_authenticators),
281281
db: Optional[AsyncSession] = Depends(get_database_session),
282282
) -> set[str]:
283283
if api_key is not None:
284-
return await get_scopes_from_api_key(api_key, settings, authenticators, db)
284+
return await get_scopes_from_api_key(
285+
api_key, settings, request.app.state.authenticated, db
286+
)
285287
elif decoded_access_token is not None:
286288
return decoded_access_token["scp"]
287289
else:
@@ -311,7 +313,6 @@ async def get_current_principal(
311313
decoded_access_token: str = Depends(get_decoded_access_token),
312314
api_key: str = Depends(get_api_key),
313315
settings: Settings = Depends(get_settings),
314-
authenticators=Depends(get_authenticators),
315316
db: Optional[AsyncSession] = Depends(get_database_session),
316317
# TODO: https://github.com/bluesky/tiled/issues/923
317318
# Remove non-Principal return types
@@ -329,7 +330,7 @@ async def get_current_principal(
329330
"""
330331

331332
if api_key is not None:
332-
if authenticators:
333+
if request.app.state.authenticated:
333334
# Tiled is in a multi-user configuration with authentication providers.
334335
# We store the hashed value of the API key secret.
335336
# By comparing hashes we protect against timing attacks.

tiled/server/router.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from ..utils import SpecialUsers, ensure_awaitable, patch_mimetypes, path_from_uri
3838
from ..validation_registration import ValidationError, ValidationRegistry
3939
from . import schemas
40-
from .authentication import get_authenticators, get_current_principal
40+
from .authentication import get_current_principal
4141
from .core import (
4242
DEFAULT_PAGE_SIZE,
4343
DEPTH_LIMIT,
@@ -142,14 +142,14 @@ def get_router(
142142
serialization_registry: SerializationRegistry,
143143
deserialization_registry: SerializationRegistry,
144144
validation_registry: ValidationRegistry,
145+
authenticators: dict[str, Union[ExternalAuthenticator, InternalAuthenticator]],
145146
) -> APIRouter:
146147
router = APIRouter()
147148

148149
@router.get("/", response_model=About)
149150
async def about(
150151
request: Request,
151152
settings: Settings = Depends(get_settings),
152-
authenticators=Depends(get_authenticators),
153153
):
154154
# TODO The lazy import of entry modules and serializers means that the
155155
# lists of formats are not populated until they are first used. Not very

tiled/server/utils.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@
1010
CSRF_COOKIE_NAME = "tiled_csrf"
1111

1212

13-
def get_authenticators():
14-
raise NotImplementedError(
15-
"This should be overridden via dependency_overrides. "
16-
"See tiled.server.app.build_app()."
17-
)
18-
19-
2013
@contextlib.contextmanager
2114
def record_timing(metrics, key):
2215
"""

0 commit comments

Comments
 (0)