Skip to content

Commit c7265ad

Browse files
committed
flake
1 parent a458bc6 commit c7265ad

File tree

12 files changed

+276
-188
lines changed

12 files changed

+276
-188
lines changed

djangosaml2/backends.py

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ def _user_model(self):
5454
try:
5555
return apps.get_model(settings.SAML_USER_MODEL)
5656
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")
5859
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'")
6062

6163
return auth.get_user_model()
6264

@@ -77,14 +79,16 @@ def _extract_user_identifier_params(self, session_info: dict, attributes: dict,
7779
# Lookup value
7880
if getattr(settings, 'SAML_USE_NAME_ID_AS_USERNAME', False):
7981
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']}")
8183
user_lookup_value = session_info['name_id'].text
8284
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.')
8487
user_lookup_value = None
8588
else:
8689
# 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)
8892

8993
return user_lookup_key, self.clean_user_main_attribute(user_lookup_value)
9094

@@ -114,14 +118,15 @@ def authenticate(self, request, session_info=None, attribute_mapping=None, creat
114118
idp_entityid = session_info['issuer']
115119

116120
attributes = self.clean_attributes(session_info['ava'], idp_entityid)
117-
118-
logger.debug('attributes: %s', attributes)
121+
122+
logger.debug(f'attributes: {attributes}')
119123

120124
if not self.is_authorized(attributes, attribute_mapping, idp_entityid):
121125
logger.error('Request not authorized')
122126
return None
123127

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)
125130
if not user_lookup_value:
126131
logger.error('Could not determine user identifier')
127132
return None
@@ -133,7 +138,8 @@ def authenticate(self, request, session_info=None, attribute_mapping=None, creat
133138

134139
# Update user with new attributes from incoming request
135140
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)
137143

138144
return user
139145

@@ -157,8 +163,7 @@ def _update_user(self, user, attributes: dict, attribute_mapping: dict, force_sa
157163
attr_value_list = attributes.get(saml_attr)
158164
if not attr_value_list:
159165
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}"')
162167
continue
163168

164169
for attr in django_attrs:
@@ -167,11 +172,13 @@ def _update_user(self, user, attributes: dict, attribute_mapping: dict, force_sa
167172
if callable(user_attr):
168173
modified = user_attr(attr_value_list)
169174
else:
170-
modified = set_attribute(user, attr, attr_value_list[0])
175+
modified = set_attribute(
176+
user, attr, attr_value_list[0])
171177

172178
has_updated_fields = has_updated_fields or modified
173179
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}"')
175182

176183
if has_updated_fields or force_save:
177184
user = self.save_user(user)
@@ -195,9 +202,9 @@ def clean_user_main_attribute(self, main_attribute: Any) -> Any:
195202
return main_attribute
196203

197204
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]:
201208
""" Look up the user to authenticate. If he doesn't exist, this method creates him (if so desired).
202209
The default implementation looks only at the user_identifier. Override this method in order to do more complex behaviour,
203210
e.g. customize this per IdP.
@@ -215,15 +222,17 @@ def get_or_create_user(self,
215222
try:
216223
user = UserModel.objects.get(**user_query_args)
217224
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)
219227
except UserModel.DoesNotExist:
220228
# Create new one if desired by settings
221229
if create_unknown_user:
222-
user = UserModel(**{ user_lookup_key: user_lookup_value })
230+
user = UserModel(**{user_lookup_key: user_lookup_value})
223231
created = True
224-
logger.debug('New user created: %s', user)
232+
logger.debug(f'New user created: {user}')
225233
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}')
227236

228237
return user, created
229238

@@ -236,7 +245,7 @@ def save_user(self, user: settings.AUTH_USER_MODEL, *args, **kwargs) -> settings
236245
if is_new_instance:
237246
logger.debug('New user created')
238247
else:
239-
logger.debug('User %s updated with incoming attributes', user)
248+
logger.debug(f'User {user} updated with incoming attributes')
240249

241250
return user
242251

@@ -245,34 +254,42 @@ def save_user(self, user: settings.AUTH_USER_MODEL, *args, **kwargs) -> settings
245254
# ############################################
246255

247256
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)
249259
return self._get_attribute_value(django_field, attributes, attribute_mapping)
250260

251261
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)
253264
return self._user_lookup_attribute
254265

255266
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)
257269
return getattr(settings, 'SAML_DJANGO_USER_MAIN_ATTRIBUTE_LOOKUP', '')
258270

259271
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)
261274
return {self.get_django_user_main_attribute() + self.get_django_user_main_attribute_lookup()}
262-
275+
263276
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)
265279
return self._update_user(user, attributes, attribute_mapping)
266280

267281
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)
269284
return self._update_user(user, attributes, attribute_mapping)
270285

271286
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)
273289
return set_attribute(obj, attr, value)
274290

275291

276292
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)
278295
return Saml2Backend()._user_model

djangosaml2/cache.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ class OutstandingQueriesCache(object):
4949
"""
5050

5151
def __init__(self, django_session):
52-
self._db = DjangoSessionCacheAdapter(django_session, '_outstanding_queries')
52+
self._db = DjangoSessionCacheAdapter(
53+
django_session, '_outstanding_queries')
5354

5455
def outstanding_queries(self):
5556
return self._db._get_objects()

djangosaml2/conf.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ def get_config_loader(path: str) -> Callable:
3131
try:
3232
config_loader = import_string(path)
3333
except ImportError as e:
34-
raise ImproperlyConfigured(f'Error importing SAML config loader {path}: "{e}"')
34+
raise ImproperlyConfigured(
35+
f'Error importing SAML config loader {path}: "{e}"')
3536

3637
if not callable(config_loader):
37-
raise ImproperlyConfigured("SAML config loader must be a callable object.")
38+
raise ImproperlyConfigured(
39+
"SAML config loader must be a callable object.")
3840

3941
return config_loader
4042

@@ -54,7 +56,8 @@ def get_config(config_loader_path: Optional[Union[Callable, str]] = None, reques
5456
If the config_loader_path is a callable instead of a string, no importing is necessary and it will be used directly.
5557
Return the resulting SPConfig.
5658
"""
57-
config_loader_path = config_loader_path or get_custom_setting('SAML_CONFIG_LOADER', 'djangosaml2.conf.config_settings_loader')
59+
config_loader_path = config_loader_path or get_custom_setting(
60+
'SAML_CONFIG_LOADER', 'djangosaml2.conf.config_settings_loader')
5861

5962
if callable(config_loader_path):
6063
config_loader = config_loader_path

djangosaml2/overrides.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ class Saml2Client(saml2.client.Saml2Client):
1414
(Original Saml2Client always prefers SOAP, so it is always used if declared
1515
in remote metadata); but doesn't actually work and causes crashes.
1616
"""
17+
1718
def do_logout(self, *args, **kwargs):
1819
if not kwargs.get('expected_binding'):
1920
try:
2021
kwargs['expected_binding'] = settings.SAML_LOGOUT_REQUEST_PREFERRED_BINDING
2122
except AttributeError:
2223
logger.warning('SAML_LOGOUT_REQUEST_PREFERRED_BINDING setting is'
23-
' not defined. Default binding will be used.')
24+
' not defined. Default binding will be used.')
2425
return super().do_logout(*args, **kwargs)

djangosaml2/signals.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@
1414

1515
import django.dispatch
1616

17-
pre_user_save = django.dispatch.Signal(providing_args=['attributes', 'user_modified'])
18-
post_authenticated = django.dispatch.Signal(providing_args=['session_info', 'request'])
17+
pre_user_save = django.dispatch.Signal(
18+
providing_args=['attributes', 'user_modified'])
19+
post_authenticated = django.dispatch.Signal(
20+
providing_args=['session_info', 'request'])

0 commit comments

Comments
 (0)