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='/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)
21 return HttpResponseRedirect(service + '&ticket=' + ticket.ticket)
23 return HttpResponseRedirect(success_redirect)
25 if request.method == 'POST':
26 email = request.POST.get('email', None)
27 password = request.POST.get('password', None)
28 service = request.POST.get('service', None)
29 lt = request.POST.get('lt', None)
30 if not request.POST.get('remember_me', None):
31 request.session.set_expiry(0)
34 login_ticket = LoginTicket.objects.get(ticket=lt)
36 errors.append('Login ticket expired. Please try again.')
39 user = authenticate(username=email, password=password)
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)
47 return HttpResponseRedirect(success_redirect)
49 errors.append('This account is disabled.')
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))
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:
60 ticket = ServiceTicket.objects.get(ticket=ticket_string)
61 ### NOTE: We've changed this to return the email address, not the username.
62 email = ticket.user.email
64 return HttpResponse("yes\n%s\n" % email)
67 return HttpResponse("no\n\n")
69 def logout(request, template_name='cas/logout.html'):
70 url = request.GET.get('url', None)
72 return render_to_response(template_name, {'url': url}, context_instance=RequestContext(request))