Commit a2618512 authored by Dmitry Shelepnev's avatar Dmitry Shelepnev
Browse files

Fix Views and Basic Auth

parent 0f4f49e0
Loading
Loading
Loading
Loading
+26 −13
Original line number Diff line number Diff line
import base64

from django.http import HttpResponse
from opds_catalog import settings
from django.contrib import auth
from django.contrib.auth.backends import RemoteUserBackend
from django.core.exceptions import ImproperlyConfigured

from opds_catalog import settings


class BasicAuthMiddleware(object):
    header = "HTTP_AUTHORIZATION"

    def unauthed(self):
        response = HttpResponse("""<html><title>Auth required</title><body>
@@ -12,15 +19,22 @@ class BasicAuthMiddleware(object):
        return response

    def process_request(self,request):
        import base64

        if not settings.AUTH:
            return
        
        if not 'HTTP_AUTHORIZATION' in request.META:
        # AuthenticationMiddleware is required so that request.user exists.
        if not hasattr(request, 'user'):
            raise ImproperlyConfigured(
                "The Django remote user auth middleware requires the"
                " authentication middleware to be installed.  Edit your"
                " MIDDLEWARE setting to insert"
                " 'django.contrib.auth.middleware.AuthenticationMiddleware'"
                " before the BasicAuthMiddleware class.")
        try:
            authentication = request.META[self.header]
        except KeyError:
            return self.unauthed()  
                    
        authentication = request.META['HTTP_AUTHORIZATION']
        (auth_meth, auth_data) = authentication.split(' ',1)
        if 'basic' != auth_meth.lower():
            return self.unauthed()
@@ -28,8 +42,7 @@ class BasicAuthMiddleware(object):
        username, password = auth_data.split(':',1)            

        user = auth.authenticate(username=username, password=password)
#        if (user is not None) and user.is_active:
        if user:
        if user and user.is_active:
            request.user = user
            auth.login(request, user)
            return 
+3 −3
Original line number Diff line number Diff line
@@ -46,9 +46,9 @@ MIDDLEWARE_CLASSES = [
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'opds_catalog.opds_middleware.BasicAuthMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',    
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',  
    'opds_catalog.opds_middleware.BasicAuthMiddleware',  
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
Loading