<s> from django . core . exceptions import ImproperlyConfigured <EOL> SESSION_KEY = '<STR_LIT>' <EOL> BACKEND_SESSION_KEY = '<STR_LIT>' <EOL> LOGIN_URL = '<STR_LIT>' <EOL> REDIRECT_FIELD_NAME = '<STR_LIT>' <EOL> def load_backend ( path ) : <EOL> i = path . rfind ( '<STR_LIT:.>' ) <EOL> module , attr = path [ : i ] , path [ i + <NUM_LIT:1> : ] <EOL> try : <EOL> mod = __import__ ( module , { } , { } , [ attr ] ) <EOL> except ImportError , e : <EOL> raise ImproperlyConfigured , '<STR_LIT>' % ( module , e ) <EOL> try : <EOL> cls = getattr ( mod , attr ) <EOL> except AttributeError : <EOL> raise ImproperlyConfigured , '<STR_LIT>' % ( module , attr ) <EOL> return cls ( ) <EOL> def get_backends ( ) : <EOL> from django . conf import settings <EOL> backends = [ ] <EOL> for backend_path in settings . AUTHENTICATION_BACKENDS : <EOL> backends . append ( load_backend ( backend_path ) ) <EOL> return backends <EOL> def authenticate ( ** credentials ) : <EOL> """<STR_LIT>""" <EOL> for backend in get_backends ( ) : <EOL> try : <EOL> user = backend . authenticate ( ** credentials ) <EOL> except TypeError : <EOL> continue <EOL> if user is None : <EOL> continue <EOL> user . backend = "<STR_LIT>" % ( backend . __module__ , backend . __class__ . __name__ ) <EOL> return user <EOL> def login ( request , user ) : <EOL> """<STR_LIT>""" <EOL> if user is None : <EOL> user = request . user <EOL> request . session [ SESSION_KEY ] = user . id <EOL> request . session [ BACKEND_SESSION_KEY ] = user . backend <EOL> def logout ( request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> del request . session [ SESSION_KEY ] <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> del request . session [ BACKEND_SESSION_KEY ] <EOL> except KeyError : <EOL> pass <EOL> def get_user ( request ) : <EOL> from django . contrib . auth . models import AnonymousUser <EOL> try : <EOL> user_id = request . session [ SESSION_KEY ] <EOL> backend_path = request . session [ BACKEND_SESSION_KEY ] <EOL> backend = load_backend ( backend_path ) <EOL> user = backend . get_user ( user_id ) or AnonymousUser ( ) <EOL> except KeyError : <EOL> user = AnonymousUser ( ) <EOL> return user </s>
