@@ -54,9 +54,11 @@ def _user_model(self):
54
54
try :
55
55
return apps .get_model (settings .SAML_USER_MODEL )
56
56
except LookupError :
57
- raise ImproperlyConfigured (f"Model '{ settings .SAML_USER_MODEL } ' could not be loaded" )
57
+ raise ImproperlyConfigured (
58
+ f"Model '{ settings .SAML_USER_MODEL } ' could not be loaded" )
58
59
except ValueError :
59
- raise ImproperlyConfigured (f"Model was specified as '{ settings .SAML_USER_MODEL } ', but it must be of the form 'app_label.model_name'" )
60
+ raise ImproperlyConfigured (
61
+ f"Model was specified as '{ settings .SAML_USER_MODEL } ', but it must be of the form 'app_label.model_name'" )
60
62
61
63
return auth .get_user_model ()
62
64
@@ -77,14 +79,16 @@ def _extract_user_identifier_params(self, session_info: dict, attributes: dict,
77
79
# Lookup value
78
80
if getattr (settings , 'SAML_USE_NAME_ID_AS_USERNAME' , False ):
79
81
if session_info .get ('name_id' ):
80
- logger .debug (' name_id: %s' , session_info ['name_id' ])
82
+ logger .debug (f" name_id: { session_info ['name_id' ]} " )
81
83
user_lookup_value = session_info ['name_id' ].text
82
84
else :
83
- logger .error ('The nameid is not available. Cannot find user without a nameid.' )
85
+ logger .error (
86
+ 'The nameid is not available. Cannot find user without a nameid.' )
84
87
user_lookup_value = None
85
88
else :
86
89
# Obtain the value of the custom attribute to use
87
- user_lookup_value = self ._get_attribute_value (user_lookup_key , attributes , attribute_mapping )
90
+ user_lookup_value = self ._get_attribute_value (
91
+ user_lookup_key , attributes , attribute_mapping )
88
92
89
93
return user_lookup_key , self .clean_user_main_attribute (user_lookup_value )
90
94
@@ -114,14 +118,15 @@ def authenticate(self, request, session_info=None, attribute_mapping=None, creat
114
118
idp_entityid = session_info ['issuer' ]
115
119
116
120
attributes = self .clean_attributes (session_info ['ava' ], idp_entityid )
117
-
118
- logger .debug ('attributes: %s' , attributes )
121
+
122
+ logger .debug (f 'attributes: { attributes } ' )
119
123
120
124
if not self .is_authorized (attributes , attribute_mapping , idp_entityid ):
121
125
logger .error ('Request not authorized' )
122
126
return None
123
127
124
- user_lookup_key , user_lookup_value = self ._extract_user_identifier_params (session_info , attributes , attribute_mapping )
128
+ user_lookup_key , user_lookup_value = self ._extract_user_identifier_params (
129
+ session_info , attributes , attribute_mapping )
125
130
if not user_lookup_value :
126
131
logger .error ('Could not determine user identifier' )
127
132
return None
@@ -133,7 +138,8 @@ def authenticate(self, request, session_info=None, attribute_mapping=None, creat
133
138
134
139
# Update user with new attributes from incoming request
135
140
if user is not None :
136
- user = self ._update_user (user , attributes , attribute_mapping , force_save = created )
141
+ user = self ._update_user (
142
+ user , attributes , attribute_mapping , force_save = created )
137
143
138
144
return user
139
145
@@ -157,8 +163,7 @@ def _update_user(self, user, attributes: dict, attribute_mapping: dict, force_sa
157
163
attr_value_list = attributes .get (saml_attr )
158
164
if not attr_value_list :
159
165
logger .debug (
160
- 'Could not find value for "%s", not updating fields "%s"' ,
161
- saml_attr , django_attrs )
166
+ f'Could not find value for "{ saml_attr } ", not updating fields "{ django_attrs } "' )
162
167
continue
163
168
164
169
for attr in django_attrs :
@@ -167,11 +172,13 @@ def _update_user(self, user, attributes: dict, attribute_mapping: dict, force_sa
167
172
if callable (user_attr ):
168
173
modified = user_attr (attr_value_list )
169
174
else :
170
- modified = set_attribute (user , attr , attr_value_list [0 ])
175
+ modified = set_attribute (
176
+ user , attr , attr_value_list [0 ])
171
177
172
178
has_updated_fields = has_updated_fields or modified
173
179
else :
174
- logger .debug ('Could not find attribute "%s" on user "%s"' , attr , user )
180
+ logger .debug (
181
+ f'Could not find attribute "{ attr } " on user "{ user } "' )
175
182
176
183
if has_updated_fields or force_save :
177
184
user = self .save_user (user )
@@ -195,9 +202,9 @@ def clean_user_main_attribute(self, main_attribute: Any) -> Any:
195
202
return main_attribute
196
203
197
204
def get_or_create_user (self ,
198
- user_lookup_key : str , user_lookup_value : Any , create_unknown_user : bool ,
199
- idp_entityid : str , attributes : dict , attribute_mapping : dict , request
200
- ) -> Tuple [Optional [settings .AUTH_USER_MODEL ], bool ]:
205
+ user_lookup_key : str , user_lookup_value : Any , create_unknown_user : bool ,
206
+ idp_entityid : str , attributes : dict , attribute_mapping : dict , request
207
+ ) -> Tuple [Optional [settings .AUTH_USER_MODEL ], bool ]:
201
208
""" Look up the user to authenticate. If he doesn't exist, this method creates him (if so desired).
202
209
The default implementation looks only at the user_identifier. Override this method in order to do more complex behaviour,
203
210
e.g. customize this per IdP.
@@ -215,15 +222,17 @@ def get_or_create_user(self,
215
222
try :
216
223
user = UserModel .objects .get (** user_query_args )
217
224
except MultipleObjectsReturned :
218
- logger .error ("Multiple users match, model: %s, lookup: %s" , UserModel ._meta , user_query_args )
225
+ logger .error ("Multiple users match, model: %s, lookup: %s" ,
226
+ UserModel ._meta , user_query_args )
219
227
except UserModel .DoesNotExist :
220
228
# Create new one if desired by settings
221
229
if create_unknown_user :
222
- user = UserModel (** { user_lookup_key : user_lookup_value })
230
+ user = UserModel (** {user_lookup_key : user_lookup_value })
223
231
created = True
224
- logger .debug ('New user created: %s' , user )
232
+ logger .debug (f 'New user created: { user } ' )
225
233
else :
226
- logger .error ('The user does not exist, model: %s, lookup: %s' , UserModel ._meta , user_query_args )
234
+ logger .error (
235
+ f'The user does not exist, model: { UserModel ._meta } , lookup: { user_query_args } ' )
227
236
228
237
return user , created
229
238
@@ -236,7 +245,7 @@ def save_user(self, user: settings.AUTH_USER_MODEL, *args, **kwargs) -> settings
236
245
if is_new_instance :
237
246
logger .debug ('New user created' )
238
247
else :
239
- logger .debug ('User %s updated with incoming attributes' , user )
248
+ logger .debug (f 'User { user } updated with incoming attributes' )
240
249
241
250
return user
242
251
@@ -245,34 +254,42 @@ def save_user(self, user: settings.AUTH_USER_MODEL, *args, **kwargs) -> settings
245
254
# ############################################
246
255
247
256
def get_attribute_value (self , django_field , attributes , attribute_mapping ):
248
- warnings .warn ("get_attribute_value() is deprecated, look at the Saml2Backend on how to subclass it" , DeprecationWarning )
257
+ warnings .warn (
258
+ "get_attribute_value() is deprecated, look at the Saml2Backend on how to subclass it" , DeprecationWarning )
249
259
return self ._get_attribute_value (django_field , attributes , attribute_mapping )
250
260
251
261
def get_django_user_main_attribute (self ):
252
- warnings .warn ("get_django_user_main_attribute() is deprecated, look at the Saml2Backend on how to subclass it" , DeprecationWarning )
262
+ warnings .warn (
263
+ "get_django_user_main_attribute() is deprecated, look at the Saml2Backend on how to subclass it" , DeprecationWarning )
253
264
return self ._user_lookup_attribute
254
265
255
266
def get_django_user_main_attribute_lookup (self ):
256
- warnings .warn ("get_django_user_main_attribute_lookup() is deprecated, look at the Saml2Backend on how to subclass it" , DeprecationWarning )
267
+ warnings .warn (
268
+ "get_django_user_main_attribute_lookup() is deprecated, look at the Saml2Backend on how to subclass it" , DeprecationWarning )
257
269
return getattr (settings , 'SAML_DJANGO_USER_MAIN_ATTRIBUTE_LOOKUP' , '' )
258
270
259
271
def get_user_query_args (self , main_attribute ):
260
- warnings .warn ("get_user_query_args() is deprecated, look at the Saml2Backend on how to subclass it" , DeprecationWarning )
272
+ warnings .warn (
273
+ "get_user_query_args() is deprecated, look at the Saml2Backend on how to subclass it" , DeprecationWarning )
261
274
return {self .get_django_user_main_attribute () + self .get_django_user_main_attribute_lookup ()}
262
-
275
+
263
276
def configure_user (self , user , attributes , attribute_mapping ):
264
- warnings .warn ("configure_user() is deprecated, look at the Saml2Backend on how to subclass it" , DeprecationWarning )
277
+ warnings .warn (
278
+ "configure_user() is deprecated, look at the Saml2Backend on how to subclass it" , DeprecationWarning )
265
279
return self ._update_user (user , attributes , attribute_mapping )
266
280
267
281
def update_user (self , user , attributes , attribute_mapping , force_save = False ):
268
- warnings .warn ("update_user() is deprecated, look at the Saml2Backend on how to subclass it" , DeprecationWarning )
282
+ warnings .warn (
283
+ "update_user() is deprecated, look at the Saml2Backend on how to subclass it" , DeprecationWarning )
269
284
return self ._update_user (user , attributes , attribute_mapping )
270
285
271
286
def _set_attribute (self , obj , attr , value ):
272
- warnings .warn ("_set_attribute() is deprecated, look at the Saml2Backend on how to subclass it" , DeprecationWarning )
287
+ warnings .warn (
288
+ "_set_attribute() is deprecated, look at the Saml2Backend on how to subclass it" , DeprecationWarning )
273
289
return set_attribute (obj , attr , value )
274
290
275
291
276
292
def get_saml_user_model ():
277
- warnings .warn ("_set_attribute() is deprecated, look at the Saml2Backend on how to subclass it" , DeprecationWarning )
293
+ warnings .warn (
294
+ "_set_attribute() is deprecated, look at the Saml2Backend on how to subclass it" , DeprecationWarning )
278
295
return Saml2Backend ()._user_model
0 commit comments