X-Git-Url: https://git.mdrn.pl/django-cas-provider.git/blobdiff_plain/d20955a26f2dbfe2480c601c2e209be1a43060e1..a92081108f2f9f0f70bd804bd3e87ab16bd02ea7:/cas_provider/views.py?ds=inline diff --git a/cas_provider/views.py b/cas_provider/views.py index 6a13b22..526a100 100644 --- a/cas_provider/views.py +++ b/cas_provider/views.py @@ -1,16 +1,21 @@ +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 @@ -25,20 +30,37 @@ def login(request, template_name='cas/login.html', success_redirect='/account/') 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) @@ -51,9 +73,14 @@ def login(request, template_name='cas/login.html', success_redirect='/account/') errors.append('This account is disabled.') else: errors.append('Incorrect username and/or password.') - form = LoginForm(service) + else: + if merge: + form = MergeLoginForm(initial={'service': service, 'email': request.GET.get('email')}) + else: + form = LoginForm(initial={'service': service}) 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/'): """ Similiar to login but user has been authenticated already through social auth. This step authenticates the login and generates a service ticket. @@ -79,7 +106,8 @@ def socialauth_login(request, template_name='cas/login.html', success_redirect=' 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) @@ -93,6 +121,7 @@ def validate(request): pass return HttpResponse("no\n\n") + def logout(request, template_name='cas/logout.html'): url = request.GET.get('url', None) auth_logout(request)