Bumped version to 0.2.dev and added tagging of releases with date.
[django-cas-provider.git] / cas_provider / views.py
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
7
8 from cas_provider.forms import LoginForm
9 from cas_provider.models import ServiceTicket, LoginTicket, auth_success_response
10 from cas_provider.utils import create_service_ticket
11
12 __all__ = ['login', 'validate', 'service_validate', 'logout']
13
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)
21             else:
22                 return HttpResponseRedirect(service + '&ticket=' + ticket.ticket)
23         else:
24             return HttpResponseRedirect(success_redirect)
25     errors = []
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)
31         
32         try:
33             login_ticket = LoginTicket.objects.get(ticket=lt)
34         except:
35             errors.append('Login ticket expired. Please try again.')
36         else:
37             login_ticket.delete()
38             user = authenticate(username=username, password=password)
39             if user is not None:
40                 if user.is_active:
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)
45                     else:
46                         return HttpResponseRedirect(success_redirect)
47                 else:
48                     errors.append('This account is disabled.')
49             else:
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))
53     
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:
58         try:
59             ticket = ServiceTicket.objects.get(ticket=ticket_string)
60             username = ticket.user.username
61             ticket.delete()
62             return HttpResponse("yes\n%s\n" % username)
63         except:
64             pass
65     return HttpResponse("no\n\n")
66
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')
76     
77     try:
78         ticket = ServiceTicket.objects.get(ticket=ticket_string)
79         ticket.delete()
80         return HttpResponse(auth_success_response(ticket.user), mimetype='text/xml')
81     except ServiceTicket.DoesNotExist:
82         return HttpResponse('''<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
83             <cas:authenticationFailure code="INVALID_TICKET">
84                 The provided ticket is invalid.
85             </cas:authenticationFailure>
86         </cas:serviceResponse>''', mimetype='text/xml')
87
88 def logout(request, template_name='cas/logout.html'):
89     url = request.GET.get('url', None)
90     auth_logout(request)
91     return render_to_response(template_name, {'url': url}, context_instance=RequestContext(request))