Changed `<CR><LF>` line endings to `<LF>` in `views.login`. Fixes #2
[django-cas-provider.git] / cas_provider / views.py
1 from django.http import HttpResponse, HttpResponseRedirect
2 from django.shortcuts import render_to_response
3 from django.template import RequestContext
4 from django.contrib.auth import authenticate
5 from django.contrib.auth import login as auth_login, logout as auth_logout
6
7 from forms import LoginForm
8 from models import ServiceTicket, LoginTicket
9 from utils import create_service_ticket
10
11 __all__ = ['login', 'validate', 'logout']
12
13 def login(request, template_name='cas/login.html', success_redirect='/accounts/'):
14     service = request.GET.get('service', None)
15     if request.user.is_authenticated():
16         if service is not None:
17             ticket = create_service_ticket(request.user, service)
18             if service.find('?') == -1:
19                 return HttpResponseRedirect(service + '?ticket=' + ticket.ticket)
20             else:
21                 return HttpResponseRedirect(service + '&ticket=' + ticket.ticket)
22         else:
23             return HttpResponseRedirect(success_redirect)
24     errors = []
25     if request.method == 'POST':
26         username = request.POST.get('username', None)
27         password = request.POST.get('password', None)
28         service = request.POST.get('service', None)
29         lt = request.POST.get('lt', None)
30         
31         try:
32             login_ticket = LoginTicket.objects.get(ticket=lt)
33         except:
34             errors.append('Login ticket expired. Please try again.')
35         else:
36             login_ticket.delete()
37             user = authenticate(username=username, password=password)
38             if user is not None:
39                 if user.is_active:
40                     auth_login(request, user)
41                     if service is not None:
42                         ticket = create_service_ticket(user, service)
43                         return HttpResponseRedirect(service + '?ticket=' + ticket.ticket)
44                     else:
45                         return HttpResponseRedirect(success_redirect)
46                 else:
47                     errors.append('This account is disabled.')
48             else:
49                     errors.append('Incorrect username and/or password.')
50     form = LoginForm(service)
51     return render_to_response(template_name, {'form': form, 'errors': errors}, context_instance=RequestContext(request))
52     
53 def validate(request):
54     service = request.GET.get('service', None)
55     ticket_string = request.GET.get('ticket', None)
56     if service is not None and ticket_string is not None:
57         try:
58             ticket = ServiceTicket.objects.get(ticket=ticket_string)
59             username = ticket.user.username
60             ticket.delete()
61             return HttpResponse("yes\n%s\n" % username)
62         except:
63             pass
64     return HttpResponse("no\n\n")
65     
66 def logout(request, template_name='cas/logout.html'):
67     url = request.GET.get('url', None)
68     auth_logout(request)
69     return render_to_response(template_name, {'url': url}, context_instance=RequestContext(request))