dash-auth-async 1.3.6__tar.gz → 1.3.7__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (22) hide show
  1. {dash_auth_async-1.3.6/dash_auth_async.egg-info → dash_auth_async-1.3.7}/PKG-INFO +1 -1
  2. {dash_auth_async-1.3.6 → dash_auth_async-1.3.7}/dash_auth_async/auth.py +10 -1
  3. {dash_auth_async-1.3.6 → dash_auth_async-1.3.7}/dash_auth_async/backends.py +35 -1
  4. {dash_auth_async-1.3.6 → dash_auth_async-1.3.7}/dash_auth_async/basic_auth.py +5 -1
  5. {dash_auth_async-1.3.6 → dash_auth_async-1.3.7}/dash_auth_async/oidc_auth.py +6 -2
  6. {dash_auth_async-1.3.6 → dash_auth_async-1.3.7}/dash_auth_async/version.py +1 -1
  7. {dash_auth_async-1.3.6 → dash_auth_async-1.3.7/dash_auth_async.egg-info}/PKG-INFO +1 -1
  8. {dash_auth_async-1.3.6 → dash_auth_async-1.3.7}/pyproject.toml +1 -1
  9. {dash_auth_async-1.3.6 → dash_auth_async-1.3.7}/LICENSE +0 -0
  10. {dash_auth_async-1.3.6 → dash_auth_async-1.3.7}/MANIFEST.in +0 -0
  11. {dash_auth_async-1.3.6 → dash_auth_async-1.3.7}/README.md +0 -0
  12. {dash_auth_async-1.3.6 → dash_auth_async-1.3.7}/dash_auth_async/__init__.py +0 -0
  13. {dash_auth_async-1.3.6 → dash_auth_async-1.3.7}/dash_auth_async/_optional.py +0 -0
  14. {dash_auth_async-1.3.6 → dash_auth_async-1.3.7}/dash_auth_async/group_protection.py +0 -0
  15. {dash_auth_async-1.3.6 → dash_auth_async-1.3.7}/dash_auth_async/public_routes.py +0 -0
  16. {dash_auth_async-1.3.6 → dash_auth_async-1.3.7}/dash_auth_async/quart_client.py +0 -0
  17. {dash_auth_async-1.3.6 → dash_auth_async-1.3.7}/dash_auth_async/websocket_auth.py +0 -0
  18. {dash_auth_async-1.3.6 → dash_auth_async-1.3.7}/dash_auth_async.egg-info/SOURCES.txt +0 -0
  19. {dash_auth_async-1.3.6 → dash_auth_async-1.3.7}/dash_auth_async.egg-info/dependency_links.txt +0 -0
  20. {dash_auth_async-1.3.6 → dash_auth_async-1.3.7}/dash_auth_async.egg-info/requires.txt +0 -0
  21. {dash_auth_async-1.3.6 → dash_auth_async-1.3.7}/dash_auth_async.egg-info/top_level.txt +0 -0
  22. {dash_auth_async-1.3.6 → dash_auth_async-1.3.7}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dash-auth-async
3
- Version: 1.3.6
3
+ Version: 1.3.7
4
4
  Summary: Dash Authorization Package.
5
5
  Author-email: Jonas Schrage <119843859+joschrag@users.noreply.github.com>
6
6
  License: MIT
@@ -7,7 +7,12 @@ from dash import Dash
7
7
  # Dash's pre-setup callback registry; no public API equivalent exists.
8
8
  from dash._callback import GLOBAL_CALLBACK_MAP # noqa: PLC2701
9
9
 
10
- from .backends import Backend, detect_backend, set_active_backend
10
+ from .backends import (
11
+ Backend,
12
+ detect_backend,
13
+ register_backend,
14
+ set_active_backend,
15
+ )
11
16
  from .public_routes import (
12
17
  add_public_routes,
13
18
  get_public_callbacks,
@@ -32,6 +37,9 @@ class Auth(ABC):
32
37
  :param app: Dash app
33
38
  :param public_routes: list of public routes, routes should follow the
34
39
  Flask route syntax
40
+ :param backend: framework adapter to use. ``None`` (default) detects it
41
+ from the server type; pass a custom ``Backend`` for a server that
42
+ is not Flask, Quart or FastAPI.
35
43
 
36
44
  Raises:
37
45
  TypeError: if any deprecated/unexpected keyword argument is passed.
@@ -42,6 +50,7 @@ class Auth(ABC):
42
50
 
43
51
  self.app = app
44
52
  self.backend = backend if backend is not None else detect_backend(app.server)
53
+ register_backend(app.server, self.backend)
45
54
  set_active_backend(self.backend)
46
55
  self._protect()
47
56
  if public_routes is not None:
@@ -8,6 +8,7 @@ from abc import ABC, abstractmethod
8
8
  from collections.abc import Callable, MutableMapping
9
9
  from contextvars import ContextVar
10
10
  from typing import Any
11
+ from weakref import WeakKeyDictionary
11
12
 
12
13
  import flask
13
14
 
@@ -885,8 +886,41 @@ class FastAPIBackend(Backend):
885
886
  server.add_middleware(_AuthMiddleware)
886
887
 
887
888
 
889
+ # server -> the backend its ``Auth`` was constructed with. Helpers that only
890
+ # hold the server (public routes/callbacks, ``get_oauth``) resolve through
891
+ # ``detect_backend``, which must hand back an explicitly passed custom backend
892
+ # rather than re-detecting by server type. Weak keys so test apps are collected.
893
+ _BACKEND_BY_SERVER: WeakKeyDictionary[Any, Backend] = WeakKeyDictionary()
894
+ # Fallback for servers that can't be weakly referenced (e.g. ``__slots__``),
895
+ # keyed by ``id``; holding the server keeps its id from being reused.
896
+ _BACKEND_BY_SERVER_ID: dict[int, tuple[Any, Backend]] = {}
897
+
898
+
899
+ def register_backend(server: Any, backend: Backend) -> None:
900
+ """Record ``backend`` as the one serving ``server`` (see ``detect_backend``)."""
901
+ try:
902
+ _BACKEND_BY_SERVER[server] = backend
903
+ except TypeError: # not weakly referenceable
904
+ _BACKEND_BY_SERVER_ID[id(server)] = (server, backend)
905
+
906
+
907
+ def _registered_backend(server: Any) -> Backend | None:
908
+ try:
909
+ return _BACKEND_BY_SERVER.get(server)
910
+ except TypeError: # not weakly referenceable
911
+ entry = _BACKEND_BY_SERVER_ID.get(id(server))
912
+ return entry[1] if entry is not None and entry[0] is server else None
913
+
914
+
888
915
  def detect_backend(server: Any) -> Backend:
889
- """Return the matching backend for a Flask, Quart, or FastAPI server."""
916
+ """Return the backend for a server.
917
+
918
+ The backend registered for it by ``Auth.__init__`` if any, otherwise the
919
+ one matching a Flask, Quart, or FastAPI server type.
920
+ """
921
+ registered = _registered_backend(server)
922
+ if registered is not None:
923
+ return registered
890
924
  if quart is not None:
891
925
  if isinstance(server, quart.Quart):
892
926
  return QuartBackend()
@@ -10,6 +10,7 @@ from typing import cast
10
10
  from dash import Dash
11
11
 
12
12
  from .auth import Auth
13
+ from .backends import Backend
13
14
 
14
15
  UserGroups = dict[str, list[str]]
15
16
 
@@ -25,6 +26,7 @@ class BasicAuth(Auth):
25
26
  public_routes: list | None = None,
26
27
  user_groups: UserGroups | Callable[[str], UserGroups] | None = None,
27
28
  secret_key: str | None = None,
29
+ backend: Backend | None = None,
28
30
  ):
29
31
  """Add basic authentication to Dash.
30
32
 
@@ -51,6 +53,8 @@ class BasicAuth(Auth):
51
53
  Note that you should not do this dynamically:
52
54
  you should create a key and then assign the value of
53
55
  that key in your code.
56
+ :param backend: framework adapter to use; ``None`` (default) detects
57
+ it from the server type.
54
58
 
55
59
  Raises:
56
60
  ValueError: if both ``auth_func`` and ``username_password_list``
@@ -82,7 +86,7 @@ class BasicAuth(Auth):
82
86
  if isinstance(username_password_list, dict)
83
87
  else {k: v for k, v in username_password_list}
84
88
  )
85
- super().__init__(app, public_routes=public_routes)
89
+ super().__init__(app, public_routes=public_routes, backend=backend)
86
90
 
87
91
  # After super().__init__: self.backend now exists, and the auth
88
92
  # middleware is registered, so SessionMiddleware (added here) lands
@@ -15,7 +15,7 @@ from werkzeug.routing import Map, Rule
15
15
  from dash_auth_async.auth import Auth
16
16
  from dash_auth_async.public_routes import get_url_base
17
17
 
18
- from .backends import detect_backend
18
+ from .backends import Backend, detect_backend
19
19
  from .websocket_auth import WS_CLIENT_COOKIE, close_authenticated_ws
20
20
 
21
21
  if TYPE_CHECKING:
@@ -43,6 +43,7 @@ class OIDCAuth(Auth):
43
43
  public_routes: list | None = None,
44
44
  logout_page: str | Response | None = None,
45
45
  secure_session: bool = False,
46
+ backend: Backend | None = None,
46
47
  ):
47
48
  """Secure a Dash app through OpenID Connect.
48
49
 
@@ -97,13 +98,16 @@ class OIDCAuth(Auth):
97
98
  On Flask/Quart this sets SESSION_COOKIE_SECURE and
98
99
  SESSION_COOKIE_HTTPONLY; on FastAPI it sets the Starlette
99
100
  SessionMiddleware ``https_only`` flag (HttpOnly is always on).
101
+ backend : Backend, optional
102
+ Framework adapter to use, by default None (detected from the
103
+ server type).
100
104
 
101
105
  Raises:
102
106
  RuntimeError: if ``app.server.secret_key`` is not defined.
103
107
  Exception: if the login or callback route lacks an ``<idp>``
104
108
  placeholder.
105
109
  """
106
- super().__init__(app, public_routes=public_routes)
110
+ super().__init__(app, public_routes=public_routes, backend=backend)
107
111
 
108
112
  if isinstance(force_https_callback, str):
109
113
  self.force_https_callback = force_https_callback in os.environ
@@ -1,3 +1,3 @@
1
1
  """Single source of truth for the package version."""
2
2
 
3
- __version__ = "1.3.6"
3
+ __version__ = "1.3.7"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dash-auth-async
3
- Version: 1.3.6
3
+ Version: 1.3.7
4
4
  Summary: Dash Authorization Package.
5
5
  Author-email: Jonas Schrage <119843859+joschrag@users.noreply.github.com>
6
6
  License: MIT
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "dash-auth-async"
7
- version = "1.3.6"
7
+ version = "1.3.7"
8
8
  description = "Dash Authorization Package."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
File without changes