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