+
+
+def service_validate(request):
+ """Validate ticket via CAS v.2 protocol"""
+ service = request.GET.get('service', None)
+ ticket_string = request.GET.get('ticket', None)
+ if service is None or ticket_string is None:
+ return _cas2_error_response(u'INVALID_REQUEST', u'Not all required parameters were sent.')
+
+ try:
+ ticket = ServiceTicket.objects.get(ticket=ticket_string)
+ except ServiceTicket.DoesNotExist:
+ return _cas2_error_response(u'INVALID_TICKET', u'The provided ticket is invalid.')
+
+ if settings.CAS_CHECK_SERVICE and ticket.service != service:
+ ticket.delete()
+ return _cas2_error_response('INVALID_SERVICE', u'Service is invalid')
+
+ username = ticket.user.username
+ ticket.delete()
+ return HttpResponse(u'''<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
+ <cas:authenticationSuccess>
+ <cas:user>%(username)s</cas:user>
+ </cas:authenticationSuccess>
+ </cas:serviceResponse>''' % {'username': username}, mimetype='text/xml')
+
+
+def _cas2_error_response(code, message):
+ return HttpResponse(u''''<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
+ <cas:authenticationFailure code="%s">
+ %s
+ </cas:authenticationFailure>
+ </cas:serviceResponse>''', mimetype='text/xml')