<s> from django . contrib import auth <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> class LazyUser ( object ) : <EOL> def __get__ ( self , request , obj_type = None ) : <EOL> if not hasattr ( request , '<STR_LIT>' ) : <EOL> from django . contrib . auth import get_user <EOL> request . _cached_user = get_user ( request ) <EOL> return request . _cached_user <EOL> class AuthenticationMiddleware ( object ) : <EOL> def process_request ( self , request ) : <EOL> assert hasattr ( request , '<STR_LIT>' ) , "<STR_LIT>" <EOL> request . __class__ . user = LazyUser ( ) <EOL> return None <EOL> class RemoteUserMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> header = "<STR_LIT>" <EOL> def process_request ( self , request ) : <EOL> if not hasattr ( request , '<STR_LIT:user>' ) : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> try : <EOL> username = request . META [ self . header ] <EOL> except KeyError : <EOL> return <EOL> if request . user . is_authenticated ( ) : <EOL> if request . user . username == self . clean_username ( username , request ) : <EOL> return <EOL> user = auth . authenticate ( remote_user = username ) <EOL> if user : <EOL> request . user = user <EOL> auth . login ( request , user ) <EOL> def clean_username ( self , username , request ) : <EOL> """<STR_LIT>""" <EOL> backend_str = request . session [ auth . BACKEND_SESSION_KEY ] <EOL> backend = auth . load_backend ( backend_str ) <EOL> try : <EOL> username = backend . clean_username ( username ) <EOL> except AttributeError : <EOL> pass <EOL> return username </s>
