2323
2424from djangosaml2 .signals import pre_user_save
2525
26- from . import settings as saml_settings
27-
2826try :
2927 from django .contrib .auth .models import SiteProfileNotAvailable
3028except ImportError :
@@ -85,8 +83,7 @@ def authenticate(self, request, session_info=None, attribute_mapping=None,
8583 use_name_id_as_username = getattr (
8684 settings , 'SAML_USE_NAME_ID_AS_USERNAME' , False )
8785
88- django_user_main_attribute = saml_settings .SAML_DJANGO_USER_MAIN_ATTRIBUTE
89- django_user_main_attribute_lookup = saml_settings .SAML_DJANGO_USER_MAIN_ATTRIBUTE_LOOKUP
86+ django_user_main_attribute = self .get_django_user_main_attribute ()
9087
9188 logger .debug ('attributes: %s' , attributes )
9289 saml_user = None
@@ -137,15 +134,19 @@ def clean_user_main_attribute(self, main_attribute):
137134 """
138135 return main_attribute
139136
140- def get_user_query_args (self , main_attribute ):
141- django_user_main_attribute = getattr (
142- settings , 'SAML_DJANGO_USER_MAIN_ATTRIBUTE' , 'username' )
143- django_user_main_attribute_lookup = getattr (
144- settings , 'SAML_DJANGO_USER_MAIN_ATTRIBUTE_LOOKUP ' , '' )
137+ def get_django_user_main_attribute (self ):
138+ return getattr (
139+ settings ,
140+ 'SAML_DJANGO_USER_MAIN_ATTRIBUTE' ,
141+ getattr ( get_saml_user_model () , 'USERNAME_FIELD ' , 'username' ) )
145142
146- return {
147- django_user_main_attribute + django_user_main_attribute_lookup : main_attribute
148- }
143+ def get_django_user_main_attribute_lookup (self ):
144+ return getattr (settings , 'SAML_DJANGO_USER_MAIN_ATTRIBUTE_LOOKUP' , '' )
145+
146+ def get_user_query_args (self , main_attribute ):
147+ lookup = (self .get_django_user_main_attribute () +
148+ self .get_django_user_main_attribute_lookup ())
149+ return {lookup : main_attribute }
149150
150151 def get_saml2_user (self , create , main_attribute , attributes , attribute_mapping ):
151152 if create :
@@ -156,8 +157,7 @@ def get_saml2_user(self, create, main_attribute, attributes, attribute_mapping):
156157 def _get_or_create_saml2_user (self , main_attribute , attributes , attribute_mapping ):
157158 logger .debug ('Check if the user "%s" exists or create otherwise' ,
158159 main_attribute )
159- django_user_main_attribute = saml_settings .SAML_DJANGO_USER_MAIN_ATTRIBUTE
160- django_user_main_attribute_lookup = saml_settings .SAML_DJANGO_USER_MAIN_ATTRIBUTE_LOOKUP
160+ django_user_main_attribute = self .get_django_user_main_attribute ()
161161 user_query_args = self .get_user_query_args (main_attribute )
162162 user_create_defaults = {django_user_main_attribute : main_attribute }
163163
@@ -180,7 +180,7 @@ def _get_or_create_saml2_user(self, main_attribute, attributes, attribute_mappin
180180
181181 def _get_saml2_user (self , main_attribute , attributes , attribute_mapping ):
182182 User = get_saml_user_model ()
183- django_user_main_attribute = saml_settings . SAML_DJANGO_USER_MAIN_ATTRIBUTE
183+ django_user_main_attribute = self . get_django_user_main_attribute ()
184184 user_query_args = self .get_user_query_args (main_attribute )
185185
186186 logger .debug ('Retrieving existing user "%s"' , main_attribute )
0 commit comments