Skip to content
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

Allow SessionRefresh middleware subclass to easily accept dynamic request parameters in process_request method #527

Open
jordan-owen opened this issue Mar 6, 2024 · 1 comment

Comments

@jordan-owen
Copy link

I'm overriding the process_request method of SessionRefresh to dynamically add a parameter kc_idp_hint. This parameter is different for each user, so I can't use OIDC_AUTH_REQUEST_EXTRA_PARAMS. There should be an easier way to add parameters to a subclassed SessionRefresh.

from django.urls import reverse
from django.utils.functional import cached_property
from mozilla_django_oidc.middleware import SessionRefresh
from django.conf import settings


class CustomSessionRefresh(SessionRefresh):
    """
    This class extends the SessionRefresh middleware.
    """

    def process_request(self, request):
        """
        Override the process_request method to add kc_idp_hint to the OIDC_AUTH_REQUEST_EXTRA_PARAMS.
        """

        if request.user.is_authenticated and not request.user.is_anonymous:

            # NOTE: Modifying settings dynamically isn't good, but needed here to avoid code duplication
            settings.OIDC_AUTH_REQUEST_EXTRA_PARAMS = {
                "kc_idp_hint": request.user.userprofile.keycloak_identity_provider.name,
            }

        return super().process_request(request)
@jordan-owen jordan-owen changed the title Allow SessionRefresh middleware subclass to easily set dynamic request parameters Allow SessionRefresh middleware subclass to easily accept dynamic request parameters in process_request method Mar 6, 2024
@jordan-owen
Copy link
Author

Suggest a similar approach to OIDCAuthenticationRequestView.

class CustomOIDCAuthenticationRequestView(OIDCAuthenticationRequestView):
    """
    Overrride OIDCAuthenticationRequestView to add kc_idp_hint parameter.
    """

    def get_extra_params(self, request):
        keycloak_identity_provider = request.GET.get("kc_idp_hint")
        extra_params = super().get_extra_params(request)
        extra_params["kc_idp_hint"] = keycloak_identity_provider
        return extra_params

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant