Skip to content

Commit 57d2d63

Browse files
authored
Merge pull request #23 from makinacorpus/fix_oidc_path_prefixs
Fix OIDC path prefixs
2 parents f3bc393 + 62c75d8 commit 57d2d63

File tree

6 files changed

+34
-17
lines changed

6 files changed

+34
-17
lines changed

django_pyoidc/drf/authentication.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from django_pyoidc.client import OIDCClient
1212
from django_pyoidc.engine import OIDCEngine
1313
from django_pyoidc.exceptions import ExpiredToken
14-
from django_pyoidc.settings import OIDCSettingsFactory
14+
from django_pyoidc.settings import OIDCSettings, OIDCSettingsFactory
1515
from django_pyoidc.utils import OIDCCacheBackendForDjango, check_audience
1616

1717
logger = logging.getLogger(__name__)
@@ -22,11 +22,17 @@ class OidcAuthException(Exception):
2222

2323

2424
class OIDCBearerAuthentication(BaseAuthentication):
25-
def __init__(self, *args: Any, **kwargs: Any):
26-
super(OIDCBearerAuthentication, self).__init__(*args, **kwargs)
27-
self.opsettings = OIDCSettingsFactory.get("drf")
28-
self.general_cache_backend = OIDCCacheBackendForDjango(self.opsettings)
29-
self.engine = OIDCEngine(self.opsettings)
25+
@functools.cached_property
26+
def opsettings(self) -> OIDCSettings:
27+
return OIDCSettingsFactory.get("drf")
28+
29+
@functools.cached_property
30+
def general_cache_backend(self) -> OIDCCacheBackendForDjango:
31+
return OIDCCacheBackendForDjango(self.opsettings)
32+
33+
@functools.cached_property
34+
def engine(self) -> OIDCEngine:
35+
return OIDCEngine(self.opsettings)
3036

3137
@functools.cached_property
3238
def client(self) -> OIDCClient:

django_pyoidc/settings.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from django.conf import settings as django_settings
77
from django.urls import reverse_lazy
8+
from django.utils.functional import Promise
89

910
from django_pyoidc.exceptions import InvalidOIDCConfigurationException
1011
from django_pyoidc.providers.provider import Provider
@@ -164,16 +165,23 @@ def _fix_settings(self, op_definition: Dict[str, Any]) -> Dict[str, Any]:
164165
# is a better way to define callback path.
165166
# here this will only work when no route prefix is used.
166167

168+
op_definition["oidc_paths_prefix"] = op_definition[
169+
"oidc_paths_prefix"
170+
].lstrip("/")
171+
167172
if "oidc_callback_path" not in op_definition:
168-
op_definition["oidc_callback_path"] = reverse_lazy(
173+
op_definition["oidc_callback_path"] = (
169174
f"{op_definition['oidc_paths_prefix']}-callback"
170175
)
171176

172177
if "oidc_callback_path" in op_definition:
173-
# remove '/' prefix if any.
174-
op_definition["oidc_callback_path"] = op_definition[
175-
"oidc_callback_path"
176-
].lstrip("/")
178+
179+
if not isinstance(op_definition["oidc_callback_path"], Promise):
180+
181+
# remove '/' prefix if any.
182+
op_definition["oidc_callback_path"] = op_definition[
183+
"oidc_callback_path"
184+
].lstrip("/")
177185

178186
# else: do not set defaults.
179187
# The Provider object should have defined a default callback path part and default

docs/settings.rst

+6-3
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,15 @@ oidc_paths_prefix
7373

7474
**Default** : dynamically computed using the name of your identity provider.
7575

76-
This is the prefix of the various url names created by the OIDCHelper when using get_urlpatterns. If not set it defaults to the op_name.
76+
This is the prefix of the various url names created *automagically* by the OIDCHelper (when using get_urlpatterns). If not set it defaults to the op_name.
7777

7878
.. note::
79-
One of the created paths is the one referenced by the setting ``oidc_callback_path``.
79+
One of the URL is the one referenced by the setting ``oidc_callback_path``.
8080

81-
You can use this setting to change how the OIDC views are named. By default they are named ``<op_name>_[login|callback]``.
81+
.. warning::
82+
If your prefix starts with a ``/`` it will be removed.
83+
84+
You can use this setting to change the prefix used to name the URLs generated by this library. By default the URLS are named ``<op_name>-[login|callback|logout|etc.]``.
8285

8386
Configuring this setting allows you to swap ``<op_name>`` with an other value.
8487

run_tests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from django.test.utils import get_runner
88

99
if __name__ == "__main__":
10-
os.environ["DJANGO_SETTINGS_MODULE"] = "tests.test_settings"
10+
os.environ["DJANGO_SETTINGS_MODULE"] = "tests.settings"
1111
django.setup()
1212
TestRunner = get_runner(settings)
1313
test_runner = TestRunner()
File renamed without changes.

django_pyoidc/test_paul.py renamed to tests/tests_paul.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
from django.contrib.auth.models import AbstractUser
66
from rest_framework import authentication, exceptions
77

8+
from django_pyoidc.client import OIDCClient
89
from django_pyoidc.utils import OIDCCacheBackendForDjango
9-
from django_pyoidc.views import OIDClient
1010

1111

1212
class BaseOIDCAuthentication(authentication.BaseAuthentication):
1313
def __init__(self):
1414
# fixme : no multi-provider support here
1515
self.op_name = "default"
1616
self.general_cache_backend = OIDCCacheBackendForDjango(self.op_name)
17-
self.client = OIDClient(self.op_name)
17+
self.client = OIDCClient(self.op_name)
1818

1919
def authenticate(self, request):
2020
token = self.get_access_token(request)

0 commit comments

Comments
 (0)