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
7 from forms import LoginForm
8 from models import ServiceTicket, LoginTicket
9 from utils import create_service_ticket
11 __all__ = ['login', 'validate', 'logout']
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)
22 return HttpResponseRedirect(service + '&ticket=' + ticket.ticket)
24 return HttpResponseRedirect(success_redirect)
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)
35 login_ticket = LoginTicket.objects.get(ticket=lt)
37 errors.append('Login ticket expired. Please try again.')
40 user = authenticate(username=email, password=password)
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)
48 return HttpResponseRedirect(success_redirect)
50 errors.append('This account is disabled.')
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))
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.
60 service = request.session['service']
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)
69 return HttpResponseRedirect(success_redirect)
71 errors.append('This account is disabled.')
73 errors.append('Incorrect username and/or password.')
74 return render_to_response(template_name, {'errors': errors}, context_instance=RequestContext(request))
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:
81 ticket = ServiceTicket.objects.get(ticket=ticket_string)
82 username = ticket.user.username
84 return HttpResponse("yes\n%s\n" % username)
87 return HttpResponse("no\n\n")
89 def logout(request, template_name='cas/logout.html'):
90 url = request.GET.get('url', None)
92 return render_to_response(template_name, {'url': url}, context_instance=RequestContext(request))