1 from django.http import HttpResponse, HttpResponseForbidden, HttpResponseRedirect
2 from django.shortcuts import render_to_response
3 from django.template import RequestContext
4 from django.contrib.auth.models import User
5 from django.contrib.auth import authenticate
6 from django.contrib.auth import login as auth_login, logout as auth_logout
8 from cas_provider.forms import LoginForm
9 from cas_provider.models import ServiceTicket, LoginTicket
10 from cas_provider.utils import create_service_ticket
12 __all__ = ['login', 'validate', 'service_validate', 'logout']
14 def login(request, template_name='cas/login.html', success_redirect='/accounts/'):
15 service = request.GET.get('service', None)
16 if request.user.is_authenticated():
17 if service is not None:
18 ticket = create_service_ticket(request.user, service)
19 if service.find('?') == -1:
20 return HttpResponseRedirect(service + '?ticket=' + ticket.ticket)
22 return HttpResponseRedirect(service + '&ticket=' + ticket.ticket)
24 return HttpResponseRedirect(success_redirect)
26 if request.method == 'POST':
27 username = request.POST.get('username', None)
28 password = request.POST.get('password', None)
29 service = request.POST.get('service', None)
30 lt = request.POST.get('lt', None)
33 login_ticket = LoginTicket.objects.get(ticket=lt)
35 errors.append('Login ticket expired. Please try again.')
38 user = authenticate(username=username, password=password)
41 auth_login(request, user)
42 if service is not None:
43 ticket = create_service_ticket(user, service)
44 return HttpResponseRedirect(service + '?ticket=' + ticket.ticket)
46 return HttpResponseRedirect(success_redirect)
48 errors.append('This account is disabled.')
50 errors.append('Incorrect username and/or password.')
51 form = LoginForm(service)
52 return render_to_response(template_name, {'form': form, 'errors': errors}, context_instance=RequestContext(request))
54 def validate(request):
55 service = request.GET.get('service', None)
56 ticket_string = request.GET.get('ticket', None)
57 if service is not None and ticket_string is not None:
59 ticket = ServiceTicket.objects.get(ticket=ticket_string)
60 username = ticket.user.username
62 return HttpResponse("yes\n%s\n" % username)
65 return HttpResponse("no\n\n")
67 def service_validate(request):
68 service = request.GET.get('service', None)
69 ticket_string = request.GET.get('ticket', None)
70 if service is None or ticket_string is None:
71 return HttpResponse('''<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
72 <cas:authenticationFailure code="INVALID_REQUEST">
73 Not all required parameters were sent.
74 </cas:authenticationFailure>
75 </cas:serviceResponse>''', mimetype='text/xml')
78 ticket = ServiceTicket.objects.get(ticket=ticket_string)
79 username = ticket.user.username
81 return HttpResponse('''<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
82 <cas:authenticationSuccess>
83 <cas:user>%(username)s</cas:user>
84 </cas:authenticationSuccess>
85 </cas:serviceResponse>''' % {'username': username}, mimetype='text/xml')
86 except ServiceTicket.DoesNotExist:
87 return HttpResponse(''''<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
88 <cas:authenticationFailure code="INVALID_TICKET">
89 The provided ticket is invalid.
90 </cas:authenticationFailure>
91 </cas:serviceResponse>''', mimetype='text/xml')
93 def logout(request, template_name='cas/logout.html'):
94 url = request.GET.get('url', None)
96 return render_to_response(template_name, {'url': url}, context_instance=RequestContext(request))