<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from __future__ import unicode_literals <EOL> from django . contrib . auth import get_user_model <EOL> from django . contrib . auth . backends import ModelBackend <EOL> from django . conf import settings <EOL> from django_cas_ng . signals import cas_user_authenticated <EOL> from . utils import get_cas_client <EOL> User = get_user_model ( ) <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class CASBackend ( ModelBackend ) : <EOL> """<STR_LIT>""" <EOL> def authenticate ( self , ticket , service , request ) : <EOL> """<STR_LIT>""" <EOL> client = get_cas_client ( service_url = service ) <EOL> username , attributes , pgtiou = client . verify_ticket ( ticket ) <EOL> if attributes : <EOL> request . session [ '<STR_LIT>' ] = attributes <EOL> if not username : <EOL> return None <EOL> username_case = settings . CAS_FORCE_CHANGE_USERNAME_CASE <EOL> if username_case == '<STR_LIT>' : <EOL> username = username . lower ( ) <EOL> elif username_case == '<STR_LIT>' : <EOL> username = username . upper ( ) <EOL> try : <EOL> user = User . objects . get ( ** { User . USERNAME_FIELD : username } ) <EOL> created = False <EOL> except User . DoesNotExist : <EOL> if not settings . CAS_CREATE_USER : <EOL> return None <EOL> user = User . objects . create_user ( username , '<STR_LIT>' ) <EOL> user . save ( ) <EOL> created = True <EOL> if pgtiou and settings . CAS_PROXY_CALLBACK : <EOL> request . session [ '<STR_LIT>' ] = pgtiou <EOL> cas_user_authenticated . send ( <EOL> sender = self , <EOL> user = user , <EOL> created = created , <EOL> attributes = attributes , <EOL> ticket = ticket , <EOL> service = service , <EOL> ) <EOL> return user <EOL> def get_user ( self , user_id ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return User . objects . get ( pk = user_id ) <EOL> except User . DoesNotExist : <EOL> return None </s>
