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