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']
19 (INVALID_TICKET, u'The provided ticket is invalid.'),
20 (INVALID_SERVICE, u'Service is invalid'),
21 (INVALID_REQUEST, u'Not all required parameters were sent.'),
25 def login(request, template_name='cas/login.html', \
26 success_redirect=settings.LOGIN_REDIRECT_URL):
27 service = request.GET.get('service', None)
28 if request.user.is_authenticated():
29 if service is not None:
30 ticket = ServiceTicket.objects.create(service=service, user=request.user)
31 return HttpResponseRedirect(ticket.get_redirect_url())
33 return HttpResponseRedirect(success_redirect)
34 if request.method == 'POST':
35 form = LoginForm(request.POST)
37 user = form.get_user()
38 auth_login(request, user)
39 service = form.cleaned_data.get('service')
40 if service is not None:
41 ticket = ServiceTicket.objects.create(service=service, user=user)
42 success_redirect = ticket.get_redirect_url()
43 return HttpResponseRedirect(success_redirect)
45 form = LoginForm(initial={
47 'lt': LoginTicket.objects.create()
49 return render_to_response(template_name, {
51 'errors': form.get_errors()
52 }, context_instance=RequestContext(request))
55 def validate(request):
56 """Validate ticket via CAS v.1 protocol"""
57 service = request.GET.get('service', None)
58 ticket_string = request.GET.get('ticket', None)
59 if service is not None and ticket_string is not None:
61 ticket = ServiceTicket.objects.get(ticket=ticket_string)
62 username = ticket.user.username
64 return HttpResponse("yes\r\n%s\r\n" % username)
67 return HttpResponse("no\r\n\r\n")
70 def logout(request, template_name='cas/logout.html'):
71 url = request.GET.get('url', None)
73 return render_to_response(template_name, {'url': url}, \
74 context_instance=RequestContext(request))
77 def service_validate(request):
78 """Validate ticket via CAS v.2 protocol"""
79 service = request.GET.get('service', None)
80 ticket_string = request.GET.get('ticket', None)
81 if service is None or ticket_string is None:
82 return _cas2_error_response(INVALID_REQUEST)
85 ticket = ServiceTicket.objects.get(ticket=ticket_string)
86 except ServiceTicket.DoesNotExist:
87 return _cas2_error_response(INVALID_TICKET)
89 if settings.CAS_CHECK_SERVICE and ticket.service != service:
91 return _cas2_error_response(INVALID_SERVICE)
93 username = ticket.user.username
95 return _cas2_sucess_response(username)
98 def _cas2_error_response(code):
99 return HttpResponse(u''''<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
100 <cas:authenticationFailure code="%(code)s">
102 </cas:authenticationFailure>
103 </cas:serviceResponse>''' % {
105 'message': dict(ERROR_MESSAGES).get(code)
106 }, mimetype='text/xml')
109 def _cas2_sucess_response(username):
110 return HttpResponse(u'''<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
111 <cas:authenticationSuccess>
112 <cas:user>%(username)s</cas:user>
113 </cas:authenticationSuccess>
114 </cas:serviceResponse>''' % {'username': username}, mimetype='text/xml')