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 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)
23 return HttpResponseRedirect(service + '&ticket=' + ticket.ticket)
25 return HttpResponseRedirect(success_redirect)
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)
36 login_ticket = LoginTicket.objects.get(ticket=lt)
38 errors.append('Login ticket expired. Please try again.')
41 user = authenticate(username=email, password=password)
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)
49 return HttpResponseRedirect(success_redirect)
51 errors.append('This account is disabled.')
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))
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.
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']
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)
76 return HttpResponseRedirect(success_redirect)
78 errors.append('This account is disabled.')
80 errors.append('Incorrect username and/or password.')
81 return render_to_response(template_name, {'errors': errors}, context_instance=RequestContext(request))
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:
88 ticket = ServiceTicket.objects.get(ticket=ticket_string)
89 username = ticket.user.username
91 return HttpResponse("yes\n%s\n" % username)
94 return HttpResponse("no\n\n")
96 def logout(request, template_name='cas/logout.html'):
97 url = request.GET.get('url', None)
99 return render_to_response(template_name, {'url': url}, context_instance=RequestContext(request))