from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import authenticate
+from models import ServiceTicket, LoginTicket
from utils import create_login_ticket
+
class LoginForm(forms.Form):
email = forms.CharField(max_length=255)
password = forms.CharField(widget=forms.PasswordInput)
#warn = forms.BooleanField(required=False) # TODO: Implement
- lt = forms.CharField(widget=forms.HiddenInput, initial=create_login_ticket)
- def __init__(self, service=None, renew=None, gateway=None, request=None, *args, **kwargs):
+ lt = forms.CharField(widget=forms.HiddenInput, initial=create_login_ticket, required=False)
+ service = forms.CharField(widget=forms.HiddenInput, initial='', required=False)
+ remember_me = forms.BooleanField(required=False)
+
+ def __init__(self, *args, **kwargs):
+ # renew = kwargs.pop('renew', None)
+ # gateway = kwargs.pop('gateway', None)
+ request = kwargs.pop('request', None)
super(LoginForm, self).__init__(*args, **kwargs)
self.request = request
- if service is not None:
- self.fields['service'] = forms.CharField(widget=forms.HiddenInput, initial=service)
+
+ def clean_remember_me(self):
+ remember = self.cleaned_data['remember_me']
+ if not remember and self.request is not None:
+ self.request.session.set_expiry(0)
+
+ def clean_lt(self):
+ lt = self.cleaned_data.get('lt',
+ self.initial.get('lt',
+ self.fields['lt'].initial()))
+ try:
+ login_ticket = LoginTicket.objects.get(ticket=lt)
+ except:
+ raise forms.ValidationError("Login ticket expired. Please try again.")
+ else:
+ login_ticket.delete()
+
+
+class MergeLoginForm(LoginForm):
+ email = forms.CharField(max_length=255, widget=forms.HiddenInput)
from views import *
urlpatterns = patterns('',
- url(r'^login/', login),
- url(r'^socialauth-login/$', socialauth_login),
- url(r'^validate/', validate),
- url(r'^logout/', logout),
-)
\ No newline at end of file
+ url(r'^login/', login),
+ url(r'^socialauth-login/$', socialauth_login),
+ url(r'^validate/', validate),
+ url(r'^logout/', logout),
+ url(r'^merge/', login, {'merge': True})
+ )
+import urllib
+
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.contrib.auth import authenticate
from django.contrib.auth import login as auth_login, logout as auth_logout
+from django.core.urlresolvers import reverse
-from forms import LoginForm
-from models import ServiceTicket, LoginTicket
+from forms import LoginForm, MergeLoginForm
+from models import ServiceTicket
from utils import create_service_ticket
+from exceptions import SameEmailMismatchedPasswords
__all__ = ['login', 'validate', 'logout']
-def login(request, template_name='cas/login.html', success_redirect='/account/'):
+
+def login(request, template_name='cas/login.html', success_redirect='/account/', merge=False):
service = request.GET.get('service', None)
if service is not None:
request.session['service'] = service
return HttpResponseRedirect(success_redirect)
errors = []
if request.method == 'POST':
- email = request.POST.get('email', None)
- password = request.POST.get('password', None)
- service = request.POST.get('service', None)
- lt = request.POST.get('lt', None)
- if not request.POST.get('remember_me', None):
- request.session.set_expiry(0)
-
- try:
- login_ticket = LoginTicket.objects.get(ticket=lt)
- except:
- errors.append('Login ticket expired. Please try again.')
+ if merge:
+ form = MergeLoginForm(request.POST, request=request)
else:
- login_ticket.delete()
- user = authenticate(username=email, password=password)
+ form = LoginForm(request.POST, request=request)
+
+ if form.is_valid():
+ try:
+ auth_args = dict(username=form.cleaned_data['email'],
+ password=form.cleaned_data['password'])
+ if merge:
+ # We only want to send the merge argument if it's
+ # True. If it it's False, we want it to propagate
+ # through the auth backends properly.
+ auth_args['merge'] = merge
+ user = authenticate(**auth_args)
+ except SameEmailMismatchedPasswords:
+ # Need to merge the accounts?
+ if merge:
+ # We shouldn't get here...
+ raise
+ else:
+ base_url = reverse('cas_provider_merge')
+ args = dict(
+ success_redirect=success_redirect,
+ email=form.cleaned_data['email'],
+ )
+ if service is not None:
+ args['service'] = service
+ args = urllib.urlencode(args)
+
+ return HttpResponseRedirect('%s?%s' % (base_url, args))
if user is not None:
if user.is_active:
auth_login(request, user)
errors.append('This account is disabled.')
else:
errors.append('Incorrect username and/or password.')
- form = LoginForm(service)
+ else:
+ if merge:
+ form = MergeLoginForm(request.GET, request=request)
+ else:
+ form = LoginForm(request.GET, request=request)
return render_to_response(template_name, {'form': form, 'errors': errors}, context_instance=RequestContext(request))
def socialauth_login(request, template_name='cas/login.html', success_redirect='/account/'):
else:
errors.append('Incorrect username and/or password.')
return render_to_response(template_name, {'errors': errors}, context_instance=RequestContext(request))
-
+
+
def validate(request):
service = request.GET.get('service', None)
ticket_string = request.GET.get('ticket', None)
pass
return HttpResponse("no\n\n")
+
def logout(request, template_name='cas/logout.html'):
url = request.GET.get('url', None)
auth_logout(request)