mozilla / mozilla-django-oidc

A django OpenID Connect library
https://mozilla-django-oidc.readthedocs.io
Mozilla Public License 2.0
449 stars 168 forks source link

Redirect url from "?next=" won't be loaded after successful authentication, even when OIDC_REDIRECT_FIELD_NAME="next" is set. #485

Open paulstrobel opened 1 year ago

paulstrobel commented 1 year ago

In my application both the standard django authentication ( django.contrib.auth.backends.ModelBackend ) and the mozilla django oidc backend ( mozilla_django_oidc.auth.OIDCAuthenticationBackend ) can be used to authenticate users. The LOGIN_REDIRECT_URL is not explicitly set.

Is this an issue of mozilla-django-oidc or is it required that the OIDC server somehow passes the next parameter along and maybe is not doing that?

nicolazilio commented 1 year ago

I have the same exact problem, the next url is not followed through after authenticating via OIDC.

soft-top-notch commented 1 year ago

Same here. How to solve this?

nicolazilio commented 1 year ago

I solved this issue, in my usecase. For me, the problem was that I set up logging in as an 'a' element, as described here, when it should have been set up as a get form that includes a next parameter

<form method="get" action="{% url 'oidc_authentication_init' %}">
     <input type="hidden" name="next" value="{{ next }}" />
     <input type="submit" value="Submit">
</form>

or see my template.

gedyeyasu commented 1 year ago

You can solve this issue by creating a custom login redirect view that gets the next query param and appends it to oidc_authentication_init url before doing the redirect. That way after successful authentication you will get redirected to the next url.

in urls.py path('auth/login/', custom_login)

in views.py

custom_login(request):
    # get next query param and append it to oidc login
    next = request.GET.get("next")
    url = reverse('oidc_authentication_init')
    if next:
        url = f"{url}?next={next}"

    return redirect(url)

This way instead of calling the url 'oidc_authentication_init' in the template or anywhere else you just call the custom login url.

jpedroreigota commented 9 months ago

In my application both the standard django authentication ( django.contrib.auth.backends.ModelBackend ) and the mozilla django oidc backend ( mozilla_django_oidc.auth.OIDCAuthenticationBackend ) can be used to authenticate users. The LOGIN_REDIRECT_URL is not explicitly set.

  • When using the default login from django, the ?next=/somepage/ parameter from the url is catched and after login the correct redirect url is loaded.
  • When using the OIDC login from mozilla-django-oidc, the next parameter is not caught. Even when explicitly setting the parameter OIDC_REDIRECT_FIELD_NAME="next" which it should be by default, the next argument is somehow lost during the authentication process. After successful authentication, the application always forwards to the default url: /accounts/profile .

Is this an issue of mozilla-django-oidc or is it required that the OIDC server somehow passes the next parameter along and maybe is not doing that?

Hey man, did you make it? I'm with the same problem