-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from maykinmedia/rework/clean-up-caching-and-…
…middleware Rework/clean up caching and middleware
- Loading branch information
Showing
10 changed files
with
184 additions
and
316 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,75 @@ | ||
from mozilla_django_oidc.middleware import SessionRefresh as _SessionRefresh | ||
from typing import Any, ClassVar, Generic, TypeVar, cast | ||
|
||
from .mixins import GetAttributeMixin, SoloConfigMixin | ||
from django.http import HttpRequest | ||
|
||
from mozilla_django_oidc.middleware import SessionRefresh as BaseSessionRefresh | ||
from typing_extensions import override | ||
|
||
from .config import dynamic_setting, get_setting_from_config | ||
from .models import OpenIDConnectConfig, OpenIDConnectConfigBase | ||
|
||
T = TypeVar("T", bound=OpenIDConnectConfigBase) | ||
|
||
|
||
class BaseRefreshMiddleware(Generic[T], BaseSessionRefresh): | ||
""" | ||
Point the middleware to a particular config class to use. | ||
This base class sets up the dynamic settings mechanism. | ||
""" | ||
|
||
_config: T | ||
config_class: ClassVar[type[OpenIDConnectConfigBase]] | ||
""" | ||
The config model/class to get the endpoints/credentials from. | ||
""" | ||
|
||
OIDC_EXEMPT_URLS = dynamic_setting[list[str]](default=[]) | ||
OIDC_OP_AUTHORIZATION_ENDPOINT = dynamic_setting[str]() | ||
OIDC_RP_CLIENT_ID = dynamic_setting[str]() | ||
OIDC_STATE_SIZE = dynamic_setting[int](default=32) | ||
OIDC_AUTHENTICATION_CALLBACK_URL = dynamic_setting[str]( | ||
default="oidc_authentication_callback" | ||
) | ||
OIDC_RP_SCOPES = dynamic_setting[str](default="openid email") | ||
OIDC_USE_NONCE = dynamic_setting[bool](default=True) | ||
OIDC_NONCE_SIZE = dynamic_setting[int](default=32) | ||
|
||
class SessionRefresh(GetAttributeMixin, SoloConfigMixin, _SessionRefresh): | ||
def __init__(self, get_response): | ||
# `super().__init__` is not called here, because this attempts to initialize | ||
# the settings (which should be retrieved from `OpenIDConnectConfig`). | ||
# `super().__init__` is not called here, because it calls self.get_setting() | ||
# The retrieval of these settings is handled via dynamic settings above. | ||
super(BaseSessionRefresh, self).__init__(get_response=get_response) | ||
|
||
# The retrieval of these settings has been moved to runtime (`__getattribute__` from the `GetAttributeMixin`) | ||
super(_SessionRefresh, self).__init__(get_response=get_response) | ||
@override | ||
def get_settings(self, attr: str, *args: Any) -> Any: # type: ignore | ||
""" | ||
Look up the request setting from the database config. | ||
def process_request(self, request): | ||
# Initialize to retrieve the settings from config model | ||
super().__init__(self.get_response) | ||
We cache the resolved config instance on the middleware instance for when | ||
settings are repeatedly looked up. Note however, that we also override the | ||
__call__ method to delete this cached property, as a middleware instance lives | ||
for the entire lifecycle of the applicatation, and each request must not look | ||
at stale cached configuration. | ||
""" | ||
if (config := getattr(self, "_config", None)) is None: | ||
# django-solo and type checking is challenging, but a new release is on the | ||
# way and should fix that :fingers_crossed: | ||
config = cast(T, self.config_class.get_solo()) | ||
self._config = config | ||
return get_setting_from_config(config, attr, *args) | ||
|
||
self.refresh_config() | ||
if not self.config.enabled: | ||
return | ||
def __call__(self, request: HttpRequest): | ||
# reset the python-level cache for each request | ||
if hasattr(self, "_config"): | ||
del self._config | ||
return super().__call__(request) | ||
|
||
def process_request(self, request): | ||
# do nothing if the configuration is not enabled | ||
if not self.get_settings("ENABLED"): | ||
return None | ||
return super().process_request(request) | ||
|
||
|
||
class SessionRefresh(BaseRefreshMiddleware[OpenIDConnectConfig]): | ||
config_class = OpenIDConnectConfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.