<s> '''<STR_LIT>''' <EOL> from django . contrib . auth import authenticate , login , logout <EOL> from django . contrib . sites . models import Site <EOL> from django . http import HttpResponse <EOL> from django . conf import settings <EOL> import re <EOL> from utils import RegexMatcher <EOL> def basic_challenge ( realm = None ) : <EOL> if realm is None : <EOL> try : <EOL> realm = Site . objects . get_current ( ) . name <EOL> except : <EOL> realm = "<STR_LIT>" <EOL> response = HttpResponse ( '<STR_LIT>' , mimetype = "<STR_LIT>" ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' % ( realm ) <EOL> response . status_code = <NUM_LIT> <EOL> return response <EOL> def basic_authenticate ( authentication ) : <EOL> ( authmeth , auth ) = authentication . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> if '<STR_LIT>' != authmeth . lower ( ) : <EOL> return None <EOL> auth = auth . strip ( ) . decode ( '<STR_LIT>' ) <EOL> username , password = auth . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> return authenticate ( username = username , password = password ) <EOL> class HTTPBasicAuthMiddleware ( RegexMatcher ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( HTTPBasicAuthMiddleware , self ) . __init__ ( "<STR_LIT>" ) <EOL> def process_request ( self , request ) : <EOL> if request . META . has_key ( "<STR_LIT>" ) : <EOL> return None <EOL> if self . matches ( request . path ) : <EOL> if not request . META . has_key ( '<STR_LIT>' ) : <EOL> logout ( request ) <EOL> return basic_challenge ( ) <EOL> user = basic_authenticate ( request . META [ '<STR_LIT>' ] ) <EOL> if user is None : <EOL> return basic_challenge ( ) <EOL> else : <EOL> login ( request , user ) </s>
