Basic Auth Middleware

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from django.http import HttpResponse
from django.conf import settings

class BasicAuthMiddleware(object):
    
    
    def unauthed(self):
        response = HttpResponse("""<html><title>Auth required</title><body>
                                <h1>Authorization Required</h1></body></html>""", mimetype="text/html")
        response['WWW-Authenticate'] = 'Basic realm="Development"'
        response.status_code = 401
        return response
    
    def process_request(self,request):
        if not request.META.has_key('HTTP_AUTHORIZATION'):
            
            return self.unauthed()
        else:
            authentication = request.META['HTTP_AUTHORIZATION']
            (authmeth, auth) = authentication.split(' ',1)
            if 'basic' != authmeth.lower():
                return self.unauthed()
            auth = auth.strip().decode('base64')
            username, password = auth.split(':',1)
            if username == settings.BASICAUTH_USERNAME and password == settings.BASICAUTH_PASSWORD:
                return None
            
            return self.unauthed()

More like this

  1. HTTP Authorization Middleware/Decorator by schinckel 4 years, 7 months ago
  2. Use email addresses for user name for django 1.3 by kidzik 2 years, 10 months ago
  3. Require login by url by zbyte64 5 years, 8 months ago
  4. HTTP basic auth decorator by bthomas 5 years, 2 months ago
  5. HTTP (basic) auth enabled (new-style) syndication framework feed class by hupf 3 years, 4 months ago

Comments

(Forgotten your password?)