#NEWLINE# def authenticate( self , session_info = None , attribute_mapping = None , create_unknown_user = True ) : #NEWLINE# #INDENT# if ( ( session_info is None ) or ( attribute_mapping is None ) ) : #NEWLINE# #INDENT# logger . error ( 'Session info or attribute mapping are None' ) #NEWLINE# return None #NEWLINE# #UNINDENT# if ( not ( 'ava' in session_info ) ) : #NEWLINE# #INDENT# logger . error ( '"ava" key not found in session_info' ) #NEWLINE# return None #NEWLINE# #UNINDENT# attributes = session_info [ 'ava' ] #NEWLINE# if ( not attributes ) : #NEWLINE# #INDENT# logger . error ( 'The attributes dictionary is empty' ) #NEWLINE# #UNINDENT# use_name_id_as_username = getattr ( settings , 'SAML_USE_NAME_ID_AS_USERNAME' , False ) #NEWLINE# django_user_main_attribute = getattr ( settings , 'SAML_DJANGO_USER_MAIN_ATTRIBUTE' , 'username' ) #NEWLINE# logger . debug ( ( 'attributes: %s' % attributes ) ) #NEWLINE# saml_user = None #NEWLINE# if use_name_id_as_username : #NEWLINE# #INDENT# if ( 'name_id' in session_info ) : #NEWLINE# #INDENT# logger . debug ( ( 'name_id: %s' % session_info [ 'name_id' ] ) ) #NEWLINE# saml_user = session_info [ 'name_id' ] . text #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# logger . error ( 'The nameid is not available. Cannot find user without a nameid.' ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# logger . debug ( ( 'attribute_mapping: %s' % attribute_mapping ) ) #NEWLINE# for ( saml_attr , django_fields ) in attribute_mapping . items ( ) : #NEWLINE# #INDENT# if ( ( django_user_main_attribute in django_fields ) and ( saml_attr in attributes ) ) : #NEWLINE# #INDENT# saml_user = attributes [ saml_attr ] [ 0 ] #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( saml_user is None ) : #NEWLINE# #INDENT# logger . error ( 'Could not find saml_user value' ) #NEWLINE# return None #NEWLINE# #UNINDENT# if ( not self . is_authorized ( attributes , attribute_mapping ) ) : #NEWLINE# #INDENT# return None #NEWLINE# #UNINDENT# user = None #NEWLINE# main_attribute = self . clean_user_main_attribute ( saml_user ) #NEWLINE# user_query_args = { #NEWLINE# #INDENT# django_user_main_attribute : main_attribute , #NEWLINE# #UNINDENT# } #NEWLINE# if create_unknown_user : #NEWLINE# #INDENT# logger . debug ( ( 'Check if the user "%s" exists or create otherwise' % main_attribute ) ) #NEWLINE# try : #NEWLINE# #INDENT# ( user , created ) = User . objects . get_or_create ( ** user_query_args ) #NEWLINE# #UNINDENT# except MultipleObjectsReturned : #NEWLINE# #INDENT# logger . error ( ( 'There are more than one user with %s = %s' % ( django_user_main_attribute , main_attribute ) ) ) #NEWLINE# return None #NEWLINE# #UNINDENT# if created : #NEWLINE# #INDENT# logger . debug ( 'New user created' ) #NEWLINE# user = self . configure_user ( user , attributes , attribute_mapping ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# logger . debug ( 'User updated' ) #NEWLINE# user = self . update_user ( user , attributes , attribute_mapping ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# logger . debug ( ( 'Retrieving existing user "%s"' % main_attribute ) ) #NEWLINE# try : #NEWLINE# #INDENT# user = User . objects . get ( ** user_query_args ) #NEWLINE# user = self . update_user ( user , attributes , attribute_mapping ) #NEWLINE# #UNINDENT# except User . DoesNotExist : #NEWLINE# #INDENT# logger . error ( ( 'The user "%s" does not exist' % main_attribute ) ) #NEWLINE# return None #NEWLINE# #UNINDENT# except MultipleObjectsReturned : #NEWLINE# #INDENT# logger . error ( ( 'There are more than one user with %s = %s' % ( django_user_main_attribute , main_attribute ) ) ) #NEWLINE# return None #NEWLINE# #UNINDENT# #UNINDENT# return user