From 00ccd540a532bb171a60c643b31bfcf8bbcce50f Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 7 Jun 2019 10:34:02 +0200 Subject: [PATCH 1/2] code style (#10196) --- mellon/adapters.py | 19 +++++++++-- mellon/middleware.py | 15 +++++++++ mellon/models.py | 15 +++++++++ mellon/sessions_backends/cached_db.py | 15 +++++++++ mellon/sessions_backends/db.py | 15 +++++++++ mellon/utils.py | 47 ++++++++++++++++++--------- mellon/views.py | 15 +++++++++ 7 files changed, 124 insertions(+), 17 deletions(-) diff --git a/mellon/adapters.py b/mellon/adapters.py index 460d1a6..a402896 100644 --- a/mellon/adapters.py +++ b/mellon/adapters.py @@ -1,3 +1,18 @@ +# django-mellon - SAML2 authentication for Django +# Copyright (C) 2014-2019 Entr'ouvert +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. + +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + import logging import uuid from xml.etree import ElementTree as ET @@ -62,7 +77,7 @@ class DefaultAdapter(object): self.logger.error(u'METADATA of %d-th idp has no EntityDescriptor root tag', i) continue - if not 'entityID' in doc.attrib: + if 'entityID' not in doc.attrib: self.logger.error( u'METADATA of %d-th idp has no entityID attribute on its root tag', i) continue @@ -91,7 +106,7 @@ class DefaultAdapter(object): except (AttributeError, KeyError, IndexError) as e: self.logger.error( u'invalid reference in username template %r: %s', username_template, e) - except Exception as e: + except Exception: self.logger.exception(u'unknown error when formatting username') else: return username diff --git a/mellon/middleware.py b/mellon/middleware.py index a0b814a..8e79218 100644 --- a/mellon/middleware.py +++ b/mellon/middleware.py @@ -1,3 +1,18 @@ +# django-mellon - SAML2 authentication for Django +# Copyright (C) 2014-2019 Entr'ouvert +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. + +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + from django.utils.http import urlencode from django.http import HttpResponseRedirect from django.core.urlresolvers import reverse diff --git a/mellon/models.py b/mellon/models.py index 9368a1d..62cf842 100644 --- a/mellon/models.py +++ b/mellon/models.py @@ -1,3 +1,18 @@ +# django-mellon - SAML2 authentication for Django +# Copyright (C) 2014-2019 Entr'ouvert +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. + +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + from django.db import models from django.utils.translation import ugettext_lazy as _ from django.conf import settings diff --git a/mellon/sessions_backends/cached_db.py b/mellon/sessions_backends/cached_db.py index 7e3aed0..b30a977 100644 --- a/mellon/sessions_backends/cached_db.py +++ b/mellon/sessions_backends/cached_db.py @@ -1,3 +1,18 @@ +# django-mellon - SAML2 authentication for Django +# Copyright (C) 2014-2019 Entr'ouvert +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. + +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + from django.contrib.sessions.backends.db import SessionStore from . import db diff --git a/mellon/sessions_backends/db.py b/mellon/sessions_backends/db.py index 9c89e86..a45eadf 100644 --- a/mellon/sessions_backends/db.py +++ b/mellon/sessions_backends/db.py @@ -1,3 +1,18 @@ +# django-mellon - SAML2 authentication for Django +# Copyright (C) 2014-2019 Entr'ouvert +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. + +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + from django.contrib.sessions.backends.db import SessionStore from mellon import utils diff --git a/mellon/utils.py b/mellon/utils.py index 6462f81..b095b1a 100644 --- a/mellon/utils.py +++ b/mellon/utils.py @@ -1,3 +1,18 @@ +# django-mellon - SAML2 authentication for Django +# Copyright (C) 2014-2019 Entr'ouvert +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. + +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + import logging import datetime import importlib @@ -29,16 +44,16 @@ def create_metadata(request): public_keys.append(public_key) name_id_formats = app_settings.NAME_ID_FORMATS return render_to_string('mellon/metadata.xml', { - 'entity_id': request.build_absolute_uri(entity_id), - 'login_url': request.build_absolute_uri(login_url), - 'logout_url': request.build_absolute_uri(logout_url), - 'public_keys': public_keys, - 'name_id_formats': name_id_formats, - 'default_assertion_consumer_binding': app_settings.DEFAULT_ASSERTION_CONSUMER_BINDING, - 'organization': app_settings.ORGANIZATION, - 'contact_persons': app_settings.CONTACT_PERSONS, - 'discovery_endpoint_url': request.build_absolute_uri(reverse('mellon_login')), - }) + 'entity_id': request.build_absolute_uri(entity_id), + 'login_url': request.build_absolute_uri(login_url), + 'logout_url': request.build_absolute_uri(logout_url), + 'public_keys': public_keys, + 'name_id_formats': name_id_formats, + 'default_assertion_consumer_binding': app_settings.DEFAULT_ASSERTION_CONSUMER_BINDING, + 'organization': app_settings.ORGANIZATION, + 'contact_persons': app_settings.CONTACT_PERSONS, + 'discovery_endpoint_url': request.build_absolute_uri(reverse('mellon_login')), + }) def create_server(request): @@ -130,7 +145,7 @@ def iso8601_to_datetime(date_string, default=None): This function ignores the sub-second resolution''' try: dt = isodate.parse_datetime(date_string) - except: + except (ValueError, TypeError): return default if is_aware(dt): if not settings.USE_TZ: @@ -209,7 +224,7 @@ def create_logout(request): def is_nonnull(s): - return not '\x00' in s + return '\x00' not in s def same_origin(url1, url2): @@ -244,6 +259,7 @@ def get_status_codes_and_message(profile): message = lasso_decode(status.statusMessage) return status_codes, message + def login(request, user): for adapter in get_adapters(): if hasattr(adapter, 'auth_login'): @@ -254,13 +270,14 @@ def login(request, user): def get_xml_encoding(content): - xml_encoding = 'utf-8' + xml_encoding = ['utf-8'] + def xmlDeclHandler(version, encoding, standalone): - xml_encoding = encoding + xml_encoding[0] = encoding parser = expat.ParserCreate() parser.XmlDeclHandler = xmlDeclHandler parser.Parse(content, True) - return xml_encoding + return xml_encoding[0] def get_local_path(request, url): diff --git a/mellon/views.py b/mellon/views.py index 5a39adf..9a685e9 100644 --- a/mellon/views.py +++ b/mellon/views.py @@ -1,3 +1,18 @@ +# django-mellon - SAML2 authentication for Django +# Copyright (C) 2014-2019 Entr'ouvert +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. + +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + import logging import requests import lasso -- 2.20.1