X-Git-Url: https://git.mdrn.pl/django-cas-provider.git/blobdiff_plain/36da57ccf1745c118c112fe457a173293e74ad36..2e6b29407ab1eabe814f664663c1f3dd1b523f82:/cas_provider/views.py?ds=sidebyside diff --git a/cas_provider/views.py b/cas_provider/views.py index 526a100..b67635d 100644 --- a/cas_provider/views.py +++ b/cas_provider/views.py @@ -1,3 +1,5 @@ +import logging +logger = logging.getLogger('cas_provider.views') import urllib from django.http import HttpResponse, HttpResponseRedirect @@ -12,10 +14,14 @@ from models import ServiceTicket from utils import create_service_ticket from exceptions import SameEmailMismatchedPasswords +from . import signals + __all__ = ['login', 'validate', 'logout'] def login(request, template_name='cas/login.html', success_redirect='/account/', 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: request.session['service'] = service @@ -23,10 +29,15 @@ def login(request, template_name='cas/login.html', success_redirect='/account/', if service is not None: ticket = create_service_ticket(request.user, service) if service.find('?') == -1: - return HttpResponseRedirect(service + '?ticket=' + ticket.ticket) + url = service + '?ticket=' + ticket.ticket + logging.debug('Redirecting to %s', url) + return HttpResponseRedirect(url) else: - return HttpResponseRedirect(service + '&ticket=' + ticket.ticket) + url = service + '&ticket=' + ticket.ticket + logging.debug('Redirecting to %s', url) + return HttpResponseRedirect() else: + logging.debug('Redirecting to %s', success_redirect) return HttpResponseRedirect(success_redirect) errors = [] if request.method == 'POST': @@ -36,6 +47,7 @@ def login(request, template_name='cas/login.html', success_redirect='/account/', form = LoginForm(request.POST, request=request) if form.is_valid(): + service = form.cleaned_data.get('service', None) try: auth_args = dict(username=form.cleaned_data['email'], password=form.cleaned_data['password']) @@ -60,14 +72,20 @@ def login(request, template_name='cas/login.html', success_redirect='/account/', args['service'] = service args = urllib.urlencode(args) - return HttpResponseRedirect('%s?%s' % (base_url, args)) + url = '%s?%s' % (base_url, args) + logging.debug('Redirecting to %s', url) + return HttpResponseRedirect(url) + 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) + 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.') @@ -78,6 +96,7 @@ def login(request, template_name='cas/login.html', success_redirect='/account/', 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)) @@ -111,14 +130,21 @@ def socialauth_login(request, template_name='cas/login.html', success_redirect=' def validate(request): service = request.GET.get('service', None) ticket_string = request.GET.get('ticket', None) + logger.info('Validating ticket %s for %s', ticket_string, service) if service is not None and ticket_string is not None: try: ticket = ServiceTicket.objects.get(ticket=ticket_string) + except ServiceTicket.DoesNotExist: + logger.exception("Tried to validate with an invalid ticket %s for %s", ticket_string, service) + else: username = ticket.user.username ticket.delete() - return HttpResponse("yes\n%s\n" % username) - except: - pass + + results = signals.on_cas_collect_histories.send(sender=validate, for_email=ticket.user.email) + histories = '\n'.join('\n'.join(rs) for rc, rs in results) + + return HttpResponse("yes\n%s\n%s" % (username, histories)) + return HttpResponse("no\n\n")