def login(request, next_page=None, required=False): """Forwards to CAS login URL or verifies CAS ticket Modified locally: honour next=??? in query string, don't deliver a message, catch HTTPError and IOError, generate LogEntry """ if not next_page and 'next' in request.GET: next_page = request.GET['next'] if not next_page: next_page = _redirect_url(request) if request.user.is_authenticated: #message = "You are logged in as %s." % request.user.username #messages.success(request, message) return HttpResponseRedirect(next_page) ticket = request.GET.get('ticket') service = _service_url(request, next_page) if ticket: from django.contrib import auth try: user = auth.authenticate(ticket=ticket, service=service, request=request) except IOError as e: # Here we want to catch only: connection reset, timeouts, name or service unknown if e.errno in [104, 110, 'socket error']: user = None # HTTPError is a type of OSError, which IOError is an alias for. # Sometimes, the CAS server seems to just return a 500 internal server error. Let's handle that the # same way as the above case. elif isinstance(e, HTTPError): if e.code == 500: user = None else: # Any other HTTPError should bubble up and let us know something horrible has happened. raise HTTPError( "Got an HTTP Error when authenticating. The error is: {0!s}." .format(e)) else: raise IOError("The errno is %r: %s." % (e.errno, str(e))) except ParseError: user = None if user is not None: auth.login(request, user) #LOG EVENT# l = LogEntry(userid=user.username, description=("logged in as %s from %s") % (user.username, ip.get_ip(request)), related_object=user) l.save() return HttpResponseRedirect(next_page) elif settings.CAS_RETRY_LOGIN or required: return HttpResponseRedirect(_login_url(service)) else: error = "<h1>Forbidden</h1><p>Login failed.</p>" return HttpResponseForbidden(error) else: return HttpResponseRedirect(_login_url(service))
def login(request, next_page=None, required=False): """Forwards to CAS login URL or verifies CAS ticket Modified locally: honour next=??? in query string, don't deliver a message, catch IOError, generate LogEntry """ if not next_page and 'next' in request.GET: next_page = request.GET['next'] if not next_page: next_page = _redirect_url(request) if request.user.is_authenticated(): #message = "You are logged in as %s." % request.user.username #messages.success(request, message) return HttpResponseRedirect(next_page) ticket = request.GET.get('ticket') service = _service_url(request, next_page) if ticket: from django.contrib import auth try: user = auth.authenticate(ticket=ticket, service=service, request=request) except IOError as e: # Here we want to catch only: connection reset, timeouts, name or service unknown if e.errno in [104, 110, 'socket error']: user = None else: raise IOError, "The errno is %r: %s." % (e.errno, unicode(e)) except ParseError: user = None if user is not None: auth.login(request, user) #LOG EVENT# l = LogEntry(userid=user.username, description=("logged in as %s from %s") % (user.username, ip.get_ip(request)), related_object=user) l.save() return HttpResponseRedirect(next_page) elif settings.CAS_RETRY_LOGIN or required: return HttpResponseRedirect(_login_url(service)) else: error = "<h1>Forbidden</h1><p>Login failed.</p>" return HttpResponseForbidden(error) else: return HttpResponseRedirect(_login_url(service))