Skip to content

Commit 031e365

Browse files
Merge pull request #162 from maykinmedia/hotfix/use-plugin-in-init-flow
Hotfix: use plugin in init flow redirect
2 parents f5e2454 + 1ae2e3a commit 031e365

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

mozilla_django_oidc_db/middleware.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
from .config import (
88
BadRequest,
99
dynamic_setting,
10-
get_setting_from_config,
1110
lookup_config,
1211
)
1312
from .models import OIDCClient
13+
from .registry import register as registry
1414

1515

1616
class SessionRefresh(BaseSessionRefresh):
@@ -50,7 +50,8 @@ def get_settings(self, attr: str, *args: Any) -> Any: # type: ignore
5050
if (config := getattr(self, "_config", None)) is None:
5151
raise BadRequest("No config object was set from the request")
5252

53-
return get_setting_from_config(config, attr, *args)
53+
plugin = registry[config.identifier]
54+
return plugin.get_setting(attr, *args)
5455

5556
def _set_config_from_request(self, request):
5657
self._config = lookup_config(request)

mozilla_django_oidc_db/views.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from .config import get_setting_from_config, lookup_config, store_config
2121
from .constants import OIDC_ADMIN_CONFIG_IDENTIFIER
2222
from .exceptions import OIDCProviderOutage
23-
from .models import OIDCClient
2423
from .registry import register as registry
2524
from .typing import GetParams
2625

@@ -258,15 +257,16 @@ def get(
258257

259258
def get_settings(self, attr: str, *args: Any) -> Any: # type: ignore
260259
"""
261-
Look up the request setting from the database config.
260+
Look up the requested setting from the plugin, which defers to the DB config.
262261
263-
For the duration of the request, the configuration instance is cached on the
262+
For the duration of the request, the plugin instance is cached on the
264263
view.
265264
"""
266-
if (config := getattr(self, "_config", None)) is None:
267-
config = OIDCClient.objects.get(identifier=self.identifier)
268-
self._config = config
269-
return get_setting_from_config(config, attr, *args)
265+
if (plugin := getattr(self, "_plugin", None)) is None:
266+
plugin = registry[self.identifier]
267+
plugin.validate_settings()
268+
self.plugin = plugin
269+
return plugin.get_setting(attr, *args)
270270

271271
@staticmethod
272272
def _validate_return_url(request: HttpRequest, return_url: str) -> None:

tests/test_init_flow.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Test the OIDC Authenticaton Request flow with our custom views.
33
"""
44

5+
from typing import Any
56
from urllib.parse import parse_qs, urlsplit
67

78
from django.http import HttpRequest, HttpResponseRedirect
@@ -105,3 +106,28 @@ def get_extra_params(
105106
query = parse_qs(parsed_url.query)
106107

107108
assert query["scope"] == ["not-email and-extra"]
109+
110+
111+
@oidcconfig()
112+
@auth_request
113+
def test_override_callback_url_plugin_settings_used(dummy_config, auth_request):
114+
@register("test-settings-override")
115+
class SettingsOverridePlugin(OIDCAdminPlugin):
116+
def get_setting(self, attr: str, *args) -> Any:
117+
if attr.lower() == "oidc_authentication_callback_url":
118+
return "admin:index"
119+
return super().get_setting(attr, *args)
120+
121+
OIDCClientFactory.create(identifier="test-settings-override")
122+
oidc_init = OIDCAuthenticationRequestInitView.as_view(
123+
identifier="test-settings-override"
124+
)
125+
126+
redirect_response = oidc_init(auth_request)
127+
128+
assert redirect_response.status_code == 302
129+
assert isinstance(redirect_response, HttpResponseRedirect)
130+
131+
parsed_url = urlsplit(redirect_response.url)
132+
query = parse_qs(parsed_url.query)
133+
assert query["redirect_uri"] == ["http://testserver/admin/"]

0 commit comments

Comments
 (0)