Skip to content

Commit 80afed0

Browse files
Django CAS ticket validation
1 parent b76c1c7 commit 80afed0

1 file changed

Lines changed: 39 additions & 8 deletions

File tree

api/auth.py

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,37 @@
11
from django.views.decorators.http import require_GET
22
from django.http import HttpResponseRedirect, HttpResponse
3+
from furl import furl
34
from website import settings
4-
# from framework.auth.cas import CasClient
5-
from osf.models import OSFUser
5+
from framework.auth import cas
6+
from framework.auth.utils import print_cas_log, LogLevel
67

8+
def make_response_from_ticket(ticket, service_url):
9+
"""
10+
Given a CAS ticket and service URL, attempt to validate the user and return user object.
11+
12+
:param str ticket: CAS service ticket
13+
:param str service_url: Service URL from which the authentication request originates
14+
:return: user object if authentication is successful, otherwise an HttpResponse with an error message and status code
15+
"""
16+
17+
service_furl = furl(service_url)
18+
if 'ticket' in service_furl.args:
19+
service_furl.remove(args=['ticket'])
20+
client = cas.get_client()
21+
cas_resp = client.service_validate(ticket, service_furl.url)
22+
if cas_resp.authenticated:
23+
user, external_credential, action = cas.get_user_from_cas_resp(cas_resp)
24+
if user and action == 'authenticate':
25+
print_cas_log(
26+
f'CAS response - authenticating user: user=[{user._id}], '
27+
f'external=[{external_credential}], action=[{action}]',
28+
LogLevel.INFO,
29+
)
30+
# if user is authenticated by CAS
31+
print_cas_log(f'CAS response - finalizing authentication: user=[{user._id}]', LogLevel.INFO)
32+
return user
33+
34+
return HttpResponse('CAS authentication failed', status=401)
735

836
@require_GET
937
def auth_login(request):
@@ -19,14 +47,17 @@ def auth_login(request):
1947
from django.contrib.auth import login
2048
import itsdangerous
2149

22-
user = OSFUser.objects.get(username='test@mail.com')
23-
login(request, user, backend='api.base.authentication.backends.ODMBackend')
50+
service_url = furl(request.build_absolute_uri()).remove(args=['ticket'])
51+
user_or_response = make_response_from_ticket(ticket, service_url.url)
52+
if isinstance(user_or_response, HttpResponse):
53+
return user_or_response
54+
login(request, user_or_response, backend='api.base.authentication.backends.ODMBackend')
2455
session = request.session
2556
data = {
26-
'auth_user_username': user.username,
27-
'auth_user_id': user._primary_key,
28-
'auth_user_fullname': user.fullname,
29-
'user_reference_uri': user.get_semantic_iri(),
57+
'auth_user_username': user_or_response.username,
58+
'auth_user_id': user_or_response._primary_key,
59+
'auth_user_fullname': user_or_response.fullname,
60+
'user_reference_uri': user_or_response.get_semantic_iri(),
3061
}
3162
for key, value in data.items() if data else {}:
3263
session[key] = value

0 commit comments

Comments
 (0)