Skip to content

Fix OIDC path prefixs #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions django_pyoidc/drf/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django_pyoidc.client import OIDCClient
from django_pyoidc.engine import OIDCEngine
from django_pyoidc.exceptions import ExpiredToken
from django_pyoidc.settings import OIDCSettingsFactory
from django_pyoidc.settings import OIDCSettings, OIDCSettingsFactory
from django_pyoidc.utils import OIDCCacheBackendForDjango, check_audience

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


class OIDCBearerAuthentication(BaseAuthentication):
def __init__(self, *args: Any, **kwargs: Any):
super(OIDCBearerAuthentication, self).__init__(*args, **kwargs)
self.opsettings = OIDCSettingsFactory.get("drf")
self.general_cache_backend = OIDCCacheBackendForDjango(self.opsettings)
self.engine = OIDCEngine(self.opsettings)
@functools.cached_property
def opsettings(self) -> OIDCSettings:
return OIDCSettingsFactory.get("drf")

@functools.cached_property
def general_cache_backend(self) -> OIDCCacheBackendForDjango:
return OIDCCacheBackendForDjango(self.opsettings)

@functools.cached_property
def engine(self) -> OIDCEngine:
return OIDCEngine(self.opsettings)

@functools.cached_property
def client(self) -> OIDCClient:
Expand Down
18 changes: 13 additions & 5 deletions django_pyoidc/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from django.conf import settings as django_settings
from django.urls import reverse_lazy
from django.utils.functional import Promise

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

op_definition["oidc_paths_prefix"] = op_definition[
"oidc_paths_prefix"
].lstrip("/")

if "oidc_callback_path" not in op_definition:
op_definition["oidc_callback_path"] = reverse_lazy(
op_definition["oidc_callback_path"] = (
f"{op_definition['oidc_paths_prefix']}-callback"
)

if "oidc_callback_path" in op_definition:
# remove '/' prefix if any.
op_definition["oidc_callback_path"] = op_definition[
"oidc_callback_path"
].lstrip("/")

if not isinstance(op_definition["oidc_callback_path"], Promise):

# remove '/' prefix if any.
op_definition["oidc_callback_path"] = op_definition[
"oidc_callback_path"
].lstrip("/")

# else: do not set defaults.
# The Provider object should have defined a default callback path part and default
Expand Down
9 changes: 6 additions & 3 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,15 @@ oidc_paths_prefix

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

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.
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.

.. note::
One of the created paths is the one referenced by the setting ``oidc_callback_path``.
One of the URL is the one referenced by the setting ``oidc_callback_path``.

You can use this setting to change how the OIDC views are named. By default they are named ``<op_name>_[login|callback]``.
.. warning::
If your prefix starts with a ``/`` it will be removed.

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.]``.

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

Expand Down
2 changes: 1 addition & 1 deletion run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.test.utils import get_runner

if __name__ == "__main__":
os.environ["DJANGO_SETTINGS_MODULE"] = "tests.test_settings"
os.environ["DJANGO_SETTINGS_MODULE"] = "tests.settings"
django.setup()
TestRunner = get_runner(settings)
test_runner = TestRunner()
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions django_pyoidc/test_paul.py → tests/tests_paul.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
from django.contrib.auth.models import AbstractUser
from rest_framework import authentication, exceptions

from django_pyoidc.client import OIDCClient
from django_pyoidc.utils import OIDCCacheBackendForDjango
from django_pyoidc.views import OIDClient


class BaseOIDCAuthentication(authentication.BaseAuthentication):
def __init__(self):
# fixme : no multi-provider support here
self.op_name = "default"
self.general_cache_backend = OIDCCacheBackendForDjango(self.op_name)
self.client = OIDClient(self.op_name)
self.client = OIDCClient(self.op_name)

def authenticate(self, request):
token = self.get_access_token(request)
Expand Down