X-Git-Url: https://git.mdrn.pl/django-cas-provider.git/blobdiff_plain/1f20c11056173d0b46b51b43beb4a2d66327e326..8ace0586471d741bbe1d55dca48f524a3d385852:/cas_provider/views.py?ds=sidebyside diff --git a/cas_provider/views.py b/cas_provider/views.py index 2979d1b..5ba62e6 100644 --- a/cas_provider/views.py +++ b/cas_provider/views.py @@ -19,27 +19,28 @@ from . import signals __all__ = ['login', 'validate', 'logout'] -def login(request, template_name='cas/login.html', success_redirect='/account/', merge=False): +def _build_service_url(service, ticket): + if service.find('?') == -1: + return service + '?ticket=' + ticket + else: + return service + '&ticket=' + ticket + + +def login(request, template_name='cas/login.html', success_redirect='/account/', **kwargs): + merge = kwargs.get('merge', False) logging.debug('CAS Provider Login view. Method is %s, merge is %s, template is %s.', request.method, merge, template_name) + service = request.GET.get('service', None) if service is not None: + # Save the service on the session, for later use if we end up + # in one of the more complicated workflows. request.session['service'] = service - if request.user.is_authenticated(): - if service is not None: - ticket = create_service_ticket(request.user, service) - if service.find('?') == -1: - url = service + '?ticket=' + ticket.ticket - logging.debug('Redirecting to %s', url) - return HttpResponseRedirect(url) - else: - url = service + '&ticket=' + ticket.ticket - logging.debug('Redirecting to %s', url) - return HttpResponseRedirect(url) - else: - logging.debug('Redirecting to %s', success_redirect) - return HttpResponseRedirect(success_redirect) + + user = request.user + errors = [] + if request.method == 'POST': if merge: form = MergeLoginForm(request.POST, request=request) @@ -76,55 +77,49 @@ def login(request, template_name='cas/login.html', success_redirect='/account/', logging.debug('Redirecting to %s', url) return HttpResponseRedirect(url) - if user is not None: + if user is None: + errors.append('Incorrect username and/or password.') + else: if user.is_active: auth_login(request, user) - if service is not None: - ticket = create_service_ticket(user, service) - url = service + '?ticket=' + ticket.ticket - logging.debug('Redirecting to %s', url) - return HttpResponseRedirect(url) - else: - logging.debug('Redirecting to %s', success_redirect) - return HttpResponseRedirect(success_redirect) - else: - errors.append('This account is disabled.') - else: - errors.append('Incorrect username and/or password.') - else: + + else: # Not a POST... if merge: form = MergeLoginForm(initial={'service': service, 'email': request.GET.get('email')}) else: form = LoginForm(initial={'service': service}) - logging.debug('Rendering response on %s, merge is %s', template_name, merge) - 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. - """ - user = request.user - user.backend = 'django.contrib.auth.backends.ModelBackend' - if request.session.has_key('service'): - service = request.session['service'] - del request.session['service'] - else: - service = '/' - errors = [] - if user is not None: - if user.is_active: - auth_login(request, user) - if service is not None: - ticket = create_service_ticket(user, service) - return HttpResponseRedirect(service + '?ticket=' + ticket.ticket) - else: - return HttpResponseRedirect(success_redirect) - else: + if user is not None and user.is_authenticated(): + # We have an authenticated user. + if not user.is_active: errors.append('This account is disabled.') - else: - errors.append('Incorrect username and/or password.') - return render_to_response(template_name, {'errors': errors}, context_instance=RequestContext(request)) + else: + # Send the on_cas_login signal. If we get an HttpResponse, return that. + for receiver, response in signals.on_cas_login.send(sender=login, request=request, **kwargs): + if isinstance(response, HttpResponse): + return response + + if service is None: + # Try and pull the service off the session + service = request.session.pop('service', service) + + if service is None: + # Normal internal success redirection. + logging.debug('Redirecting to %s', success_redirect) + return HttpResponseRedirect(success_redirect) + else: + # Create a service ticket and redirect to the service. + ticket = create_service_ticket(request.user, service) + if 'service' in request.session: + # Don't need this any more. + del request.session['service'] + + url = _build_service_url(service, ticket.ticket) + logging.debug('Redirecting to %s', url) + return HttpResponseRedirect(url) + + logging.debug('Rendering response on %s, merge is %s', template_name, merge) + return render_to_response(template_name, {'form': form, 'errors': errors}, context_instance=RequestContext(request)) def validate(request):