1 from django.conf import settings
2 from django.contrib.auth import login as auth_login, \
4 from django.http import HttpResponse, HttpResponseRedirect
5 from django.shortcuts import render_to_response
6 from django.template import RequestContext
7 from forms import LoginForm
8 from models import ServiceTicket, LoginTicket
11 __all__ = ['login', 'validate', 'logout', 'service_validate']
14 INVALID_TICKET = 'INVALID_TICKET'
15 INVALID_SERVICE = 'INVALID_SERVICE'
16 INVALID_REQUEST = 'INVALID_REQUEST'
17 INTERNAL_ERROR = 'INTERNAL_ERROR'
20 (INVALID_TICKET, u'The provided ticket is invalid.'),
21 (INVALID_SERVICE, u'Service is invalid'),
22 (INVALID_REQUEST, u'Not all required parameters were sent.'),
23 (INTERNAL_ERROR, u'An internal error occurred during ticket validation'),
27 def login(request, template_name='cas/login.html', \
28 success_redirect=settings.LOGIN_REDIRECT_URL,
29 warn_template_name='cas/warn.html'):
30 service = request.GET.get('service', None)
31 if request.user.is_authenticated():
32 if service is not None:
33 if request.GET.get('warn', False):
34 return render_to_response(warn_template_name, {
37 }, context_instance=RequestContext(request))
38 ticket = ServiceTicket.objects.create(service=service, user=request.user)
39 return HttpResponseRedirect(ticket.get_redirect_url())
41 return HttpResponseRedirect(success_redirect)
42 if request.method == 'POST':
43 form = LoginForm(request.POST)
45 user = form.get_user()
46 auth_login(request, user)
47 service = form.cleaned_data.get('service')
48 if service is not None:
49 ticket = ServiceTicket.objects.create(service=service, user=user)
50 success_redirect = ticket.get_redirect_url()
51 return HttpResponseRedirect(success_redirect)
53 form = LoginForm(initial={
55 'lt': LoginTicket.objects.create()
57 return render_to_response(template_name, {
59 'errors': form.get_errors()
60 }, context_instance=RequestContext(request))
63 def validate(request):
64 """Validate ticket via CAS v.1 protocol"""
65 service = request.GET.get('service', None)
66 ticket_string = request.GET.get('ticket', None)
67 if service is not None and ticket_string is not None:
68 #renew = request.GET.get('renew', True)
70 # TODO: check user SSO session
72 ticket = ServiceTicket.objects.get(ticket=ticket_string)
73 username = ticket.user.username
75 return HttpResponse("yes\n%s\n" % username)
78 return HttpResponse("no\n\n")
81 def logout(request, template_name='cas/logout.html'):
82 url = request.GET.get('url', None)
84 return render_to_response(template_name, {'url': url}, \
85 context_instance=RequestContext(request))
88 def service_validate(request):
89 """Validate ticket via CAS v.2 protocol"""
90 service = request.GET.get('service', None)
91 ticket_string = request.GET.get('ticket', None)
92 if service is None or ticket_string is None:
93 return _cas2_error_response(INVALID_REQUEST)
96 ticket = ServiceTicket.objects.get(ticket=ticket_string)
97 except ServiceTicket.DoesNotExist:
98 return _cas2_error_response(INVALID_TICKET)
100 if settings.CAS_CHECK_SERVICE and ticket.service != service:
102 return _cas2_error_response(INVALID_SERVICE)
104 username = ticket.user.username
106 return _cas2_sucess_response(username)
109 def _cas2_error_response(code):
110 return HttpResponse(u''''<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
111 <cas:authenticationFailure code="%(code)s">
113 </cas:authenticationFailure>
114 </cas:serviceResponse>''' % {
116 'message': dict(ERROR_MESSAGES).get(code)
117 }, mimetype='text/xml')
120 def _cas2_sucess_response(username):
121 return HttpResponse(u'''<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
122 <cas:authenticationSuccess>
123 <cas:user>%(username)s</cas:user>
124 </cas:authenticationSuccess>
125 </cas:serviceResponse>''' % {'username': username}, mimetype='text/xml')