Skip to content

Commit d5fd77a

Browse files
author
Cristhian Garcia
authored
Merge pull request #10 from eduNEXT/cag/fix-request-without-ticket
fix request without ticket
2 parents 15db4fd + 121dd69 commit d5fd77a

4 files changed

Lines changed: 62 additions & 2 deletions

File tree

CHANGELOG.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@ Change Log
66
in this file. It adheres to the structure of https://keepachangelog.com/ ,
77
but in reStructuredText instead of Markdown (for ease of incorporation into
88
Sphinx documentation and the PyPI description).
9-
9+
1010
This project adheres to Semantic Versioning (https://semver.org/).
1111

1212
.. There should always be an "Unreleased" section for changes pending release.
1313
1414
Unreleased
1515
~~~~~~~~~~
1616

17+
[0.2.3] - 2022-08-11
18+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19+
20+
Added
21+
_____
22+
23+
* Added CAS_REDIRECT_WITHOUT_TICKET setting.
1724

1825
[0.2.2] - 2022-07-27
1926
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

README.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ The required configuration includes:
4949
"CAS_SERVICE_URL": "https://LMS_BASE/auth/complete/centralized-auth-service/?next=/"
5050
}
5151
52+
Optional configuration:
53+
54+
.. code-block:: json
55+
56+
{
57+
"CAS_REDIRECT_WITHOUT_TICKET": true
58+
}
59+
60+
This settings allows you to modify the default behavior when the auth/complete backend receives a request without ticket. Usually when the user reset his password in the CAS Server and it's automatically redirected to the LMS.
61+
62+
Expected behavior:
63+
- true: Redirects to login automatically
64+
- false: Raise an AuthMissingParameter exception
65+
5266
We advise you to use the following third party auth pipeline:
5367

5468
.. code-block:: json

openedx_cas/backends.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from django.conf import settings
1111
from django.contrib.auth import get_user_model
12+
from django.shortcuts import redirect
1213
from django_cas_ng.backends import CASBackend
1314
from django_cas_ng.utils import get_cas_client
1415
from social_core.backends.base import BaseAuth
@@ -63,7 +64,13 @@ def auth_complete(self, *args, **kwargs):
6364
ticket = request.GET.get("ticket", None)
6465

6566
if not ticket:
66-
raise AuthMissingParameter(self, "ticket")
67+
logger.error("Ticket was not found in the request to authenticate user")
68+
# The setting is added to allow changing the behavior when there is not
69+
# ticket in the request. Usually when the user changes his password.
70+
if getattr(settings, "CAS_REDIRECT_WITHOUT_TICKET", None):
71+
return redirect(settings.LOGIN_URL)
72+
else:
73+
raise AuthMissingParameter(self, "ticket")
6774

6875
response = self.cas_validation(request, ticket, settings.CAS_SERVICE_URL)
6976
kwargs.update({"response": response, "backend": self})

openedx_cas/tests/test_backends.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,38 @@ def test_auth_complete_ticket_validated(self, cas_validation):
7979
backend=self.cas_backend
8080
)
8181

82+
@override_settings(CAS_REDIRECT_WITHOUT_TICKET=True)
83+
@override_settings(LOGIN_URL="/login")
84+
@patch('openedx_cas.backends.redirect')
85+
def test_auth_complete_redirect_on_missing_ticket(self, redirect):
86+
"""
87+
This method is used to verify that the flow is valid when the ticket is valid.
88+
89+
Expected behavior:
90+
Response returned by auth_complete should include the correspoding values when the ticket is valid
91+
"""
92+
service_response = {}
93+
request = Mock(GET=service_response)
94+
95+
self.cas_backend.auth_complete(request=request)
96+
97+
redirect.assert_called_with(
98+
settings.LOGIN_URL
99+
)
100+
101+
@override_settings(CAS_REDIRECT_WITHOUT_TICKET=None)
102+
def test_auth_complete_raises_on_missing_ticket(self):
103+
"""
104+
This method is used to verify that the flow is valid when the ticket is valid.
105+
106+
Expected behavior:
107+
Response returned by auth_complete should include the correspoding values when the ticket is valid
108+
"""
109+
service_response = {}
110+
request = Mock(GET=service_response)
111+
112+
self.assertRaises(AuthMissingParameter, self.cas_backend.auth_complete, request=request)
113+
82114
@unpack
83115
@data(
84116
{

0 commit comments

Comments
 (0)