|
1 | 1 | import json |
| 2 | +import secrets |
| 3 | +from typing import Any |
2 | 4 |
|
3 | 5 | import numpy |
4 | 6 | import pytest |
5 | 7 | from starlette.status import HTTP_403_FORBIDDEN |
| 8 | +from typing_extensions import TypedDict |
6 | 9 |
|
7 | | -from tiled.authenticators import DictionaryAuthenticator |
8 | | -from tiled.server.protocols import UserSessionState |
| 10 | +from tiled.server.protocols import InternalAuthenticator, UserSessionState |
9 | 11 |
|
10 | 12 | from ..access_policies import NO_ACCESS |
11 | 13 | from ..adapters.array import ArrayAdapter |
@@ -531,22 +533,23 @@ def test_service_principal_access(tmpdir, sqlite_or_postgres_uri): |
531 | 533 | assert list(sp_client) == ["x"] |
532 | 534 |
|
533 | 535 |
|
534 | | -class CustomAttributesAuthenticator(DictionaryAuthenticator): |
| 536 | +UserAttributes = TypedDict( |
| 537 | + "UserAttributes", {"password": str, "attributes": dict[str, Any]}, total=False |
| 538 | +) |
| 539 | + |
| 540 | + |
| 541 | +class CustomAttributesAuthenticator(InternalAuthenticator): |
535 | 542 | """An example authenticator that enriches the stored user information.""" |
536 | 543 |
|
537 | | - def __init__(self, users: dict, confirmation_message: str = ""): |
538 | | - self._users = users |
539 | | - super().__init__( |
540 | | - {username: user["password"] for username, user in users.items()}, |
541 | | - confirmation_message, |
542 | | - ) |
| 544 | + users: dict[str, UserAttributes] = {} |
543 | 545 |
|
544 | 546 | async def authenticate(self, username, password): |
545 | | - state = await super().authenticate(username, password) |
546 | | - if isinstance(state, UserSessionState): |
547 | | - # enrich the auth state |
548 | | - state.state["attributes"] = self._users[username].get("attributes", {}) |
549 | | - return state |
| 547 | + if (attrs := self.users.get(username)) and (pw := attrs.get("password")): |
| 548 | + if secrets.compare_digest(pw, password): |
| 549 | + state = UserSessionState( |
| 550 | + username, {"attributes": attrs.get("attributes", {})} |
| 551 | + ) |
| 552 | + return state |
550 | 553 |
|
551 | 554 |
|
552 | 555 | class CustomAttributesAccessPolicy: |
|
0 commit comments