ezproxy group handling code now in correct try/catch alignment. Added a bit of ezproxy logging.
from __future__ import unicode_literals
import hashlib
+import logging
import urllib
from time import gmtime
from calendar import timegm
from django.conf import settings
-from django.core.exceptions import (
- ObjectDoesNotExist,
- SuspiciousOperation,
- ValidationError,
-)
+from django.core.exceptions import SuspiciousOperation, ValidationError
from django.core.validators import URLValidator
from django.http import HttpResponseRedirect
from django.views import View
from TWLight.resources.models import Partner, Stream
from TWLight.users.models import Authorization
+logger = logging.getLogger(__name__)
+
class EZProxyAuth(View):
@staticmethod
@@ -29,20 +28,36 @@ class EZProxyAuth(View):
try:
authorizations = Authorization.objects.filter(authorized_user=request.user)
+ logger.info(
+ "Editor {username} has the following authorizations: {authorizations}.".format(
+ username=username, authorizations=authorizations
+ )
+ )
+ except Authorization.DoesNotExist:
+ authorizations = None
+
for authorization in authorizations:
if authorization.is_valid:
- partner = Partner.objects.get(authorization_method=Partner.PROXY, pk=authorization.partner_id)
group = ""
- if partner:
- group = "P" + repr(partner.pk)
- stream = Stream.objects.get(authorization_method=Partner.PROXY, pk=authorization.stream_id)
- if stream:
+ try:
+ partner = Partner.objects.get(
+ authorization_method=Partner.PROXY, pk=authorization.partner_id
+ )
+ group += "P" + repr(partner.pk)
+ try:
+ stream = Stream.objects.get(
+ authorization_method=Partner.PROXY,
+ pk=authorization.stream_id,
+ )
group += "S" + repr(stream.pk)
- if group:
- groups.append(group)
- except ObjectDoesNotExist:
+ except Stream.DoesNotExist:
+ pass
+ except Partner.DoesNotExist:
pass
+ groups.append(group)
+ logger.info("{group}.".format(group=group))
+
if url:
try:
validate = URLValidator(schemes=("http", "https"))
@@ -75,6 +90,11 @@ class EZProxyTicket(object):
packet += "$g" + "+".join(groups)
packet += "$e"
+ logger.info(
+ "Editor {username} has the following EZProxy group packet: {packet}.".format(
+ username=user, packet=packet
+ )
+ )
ticket = urllib.quote(
hashlib.sha512(secret + user + packet).hexdigest() + packet
)
