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

Added: Implementation of POST login and logout #345

Merged
merged 11 commits into from
Sep 30, 2024
37 changes: 30 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,36 @@ This will add these paths to Django:
* ``/oauth2/callback`` where ADFS redirects back to after login. So make sure you set the redirect URI on ADFS to this.
* ``/oauth2/logout`` which logs out the user from both Django and ADFS.

You can use them like this in your django templates:

.. code-block:: html

<a href="{% url 'django_auth_adfs:logout' %}">Logout</a>
<a href="{% url 'django_auth_adfs:login' %}">Login</a>
<a href="{% url 'django_auth_adfs:login-no-sso' %}">Login (no SSO)</a>
Below is sample Django template code to use these paths depending if
you'd like to use GET or POST requests. Logging out was deprecated in
`Django 4.1 <https://docs.djangoproject.com/en/5.1/releases/4.1/#features-deprecated-in-4-1>`_.

- Using GET requests:

.. code-block:: html

<a href="{% url 'django_auth_adfs:logout' %}">Logout</a>
<a href="{% url 'django_auth_adfs:login' %}">Login</a>
<a href="{% url 'django_auth_adfs:login-no-sso' %}">Login (no SSO)</a>

- Using POST requests:

.. code-block:: html+django

<form method="post" action="{% url 'django_auth_adfs:logout' %}">
{% csrf_token %}
<button type="submit">Logout</button>
</form>
<form method="post" action="{% url 'django_auth_adfs:login' %}">
{% csrf_token %}
<input type="hidden" name="next" value="{{ next }}">
<button type="submit">Login</button>
</form>
<form method="post" action="{% url 'django_auth_adfs:login-no-sso' %}">
{% csrf_token %}
<input type="hidden" name="next" value="{{ next }}">
<button type="submit">Login (no SSO)</button>
</form>

Contributing
------------
Expand Down
5 changes: 4 additions & 1 deletion django_auth_adfs/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,10 @@ def build_authorization_endpoint(self, request, disable_sso=None, force_mfa=Fals

"""
self.load_config()
redirect_to = request.GET.get(REDIRECT_FIELD_NAME, None)
if request.method == 'POST':
redirect_to = request.POST.get(REDIRECT_FIELD_NAME, None)
else:
redirect_to = request.GET.get(REDIRECT_FIELD_NAME, None)
if not redirect_to:
redirect_to = django_settings.LOGIN_REDIRECT_URL
redirect_to = base64.urlsafe_b64encode(redirect_to.encode()).decode()
Expand Down
37 changes: 37 additions & 0 deletions django_auth_adfs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ def get(self, request):
"""
return redirect(provider_config.build_authorization_endpoint(request))

def post(self, request):
"""
Initiates the OAuth2 flow and redirect the user agent to ADFS

Args:
request (django.http.request.HttpRequest): A Django Request object
"""
return redirect(provider_config.build_authorization_endpoint(request))


class OAuth2LoginNoSSOView(View):
def get(self, request):
Expand All @@ -95,6 +104,15 @@ def get(self, request):
"""
return redirect(provider_config.build_authorization_endpoint(request, disable_sso=True))

def post(self, request):
"""
Initiates the OAuth2 flow and redirect the user agent to ADFS

Args:
request (django.http.request.HttpRequest): A Django Request object
"""
return redirect(provider_config.build_authorization_endpoint(request, disable_sso=True))


class OAuth2LoginForceMFA(View):
def get(self, request):
Expand All @@ -106,6 +124,15 @@ def get(self, request):
"""
return redirect(provider_config.build_authorization_endpoint(request, force_mfa=True))

def post(self, request):
"""
Initiates the OAuth2 flow and redirect the user agent to ADFS

Args:
request (django.http.request.HttpRequest): A Django Request object
"""
return redirect(provider_config.build_authorization_endpoint(request, force_mfa=True))


class OAuth2LogoutView(View):
def get(self, request):
Expand All @@ -117,3 +144,13 @@ def get(self, request):
"""
logout(request)
return redirect(provider_config.build_end_session_endpoint())

def post(self, request):
"""
Logs out the user from both Django and ADFS

Args:
request (django.http.request.HttpRequest): A Django Request object
"""
logout(request)
return redirect(provider_config.build_end_session_endpoint())
37 changes: 30 additions & 7 deletions docs/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,33 @@ This will add these paths to Django:
* ``/oauth2/callback`` where ADFS redirects back to after login. So make sure you set the redirect URI on ADFS to this.
* ``/oauth2/logout`` which logs out the user from both Django and ADFS.

You can use them like this in your django templates:

.. code-block:: html

<a href="{% url 'django_auth_adfs:logout' %}">Logout</a>
<a href="{% url 'django_auth_adfs:login' %}">Login</a>
<a href="{% url 'django_auth_adfs:login-no-sso' %}">Login (no SSO)</a>
Below is sample Django template code to use these paths depending if
you'd like to use GET or POST requests. Logging out was deprecated in
`Django 4.1 <https://docs.djangoproject.com/en/5.1/releases/4.1/#features-deprecated-in-4-1>`_.

- Using GET requests:

.. code-block:: html

<a href="{% url 'django_auth_adfs:logout' %}">Logout</a>
<a href="{% url 'django_auth_adfs:login' %}">Login</a>
<a href="{% url 'django_auth_adfs:login-no-sso' %}">Login (no SSO)</a>

- Using POST requests:

.. code-block:: html+django

<form method="post" action="{% url 'django_auth_adfs:logout' %}">
{% csrf_token %}
<button type="submit">Logout</button>
</form>
<form method="post" action="{% url 'django_auth_adfs:login' %}">
{% csrf_token %}
<input type="hidden" name="next" value="{{ next }}">
<button type="submit">Login</button>
</form>
<form method="post" action="{% url 'django_auth_adfs:login-no-sso' %}">
{% csrf_token %}
<input type="hidden" name="next" value="{{ next }}">
<button type="submit">Login (no SSO)</button>
</form>
Loading