d76e60b69b91d192e4f92511b5b3c25b90375427
[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='/account/'):
14     service = request.GET.get('service', None)
15     request.session['service'] = service
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         email = request.POST.get('email', None)
28         password = request.POST.get('password', None)
29         service = request.POST.get('service', None)
30         lt = request.POST.get('lt', None)
31         if not request.POST.get('remember_me', None):
32           request.session.set_expiry(0)
33                 
34         try:
35             login_ticket = LoginTicket.objects.get(ticket=lt)
36         except:
37             errors.append('Login ticket expired. Please try again.')
38         else:
39             login_ticket.delete()
40             user = authenticate(username=email, password=password)
41             if user is not None:
42                 if user.is_active:
43                     auth_login(request, user)
44                     if service is not None:
45                         ticket = create_service_ticket(user, service)
46                         return HttpResponseRedirect(service + '?ticket=' + ticket.ticket)
47                     else:
48                         return HttpResponseRedirect(success_redirect)
49                 else:
50                     errors.append('This account is disabled.')
51             else:
52                     errors.append('Incorrect username and/or password.')
53     form = LoginForm(service)
54     return render_to_response(template_name, {'form': form, 'errors': errors}, context_instance=RequestContext(request))
55
56 def socialauth_login(request, user, template_name='cas/login.html', success_redirect='/account/'):
57     """ Similiar to login but user has been authenticated already through social auth.
58         This step authenticates the login and generates a service ticket.
59     """
60     service = request.session['service']
61     errors = []
62     if user is not None:
63         if user.is_active:
64             auth_login(request, user)
65             if service is not None:
66                 ticket = create_service_ticket(user, service)
67                 return HttpResponseRedirect(service + '?ticket=' + ticket.ticket)
68             else:
69                 return HttpResponseRedirect(success_redirect)
70         else:
71             errors.append('This account is disabled.')
72     else:
73             errors.append('Incorrect username and/or password.')
74     return render_to_response(template_name, {'errors': errors}, context_instance=RequestContext(request))
75     
76 def validate(request):
77     service = request.GET.get('service', None)
78     ticket_string = request.GET.get('ticket', None)
79     if service is not None and ticket_string is not None:
80         try:
81             ticket = ServiceTicket.objects.get(ticket=ticket_string)
82             username = ticket.user.username
83             ticket.delete()
84             return HttpResponse("yes\n%s\n" % username)
85         except:
86             pass
87     return HttpResponse("no\n\n")
88     
89 def logout(request, template_name='cas/logout.html'):
90     url = request.GET.get('url', None)
91     auth_logout(request)
92     return render_to_response(template_name, {'url': url}, context_instance=RequestContext(request))