From 119789364cec84939263f3ee9fd83a6929ed5892 Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Sun, 17 Jan 2021 10:56:22 +0100 Subject: [PATCH] Make some dependencies optional --- README.md | 4 +++- django_ynh/__init__.py | 2 +- django_ynh/sso_auth/auth_middleware.py | 21 +++++++++++++-------- manifest.json | 2 +- pyproject.toml | 16 +++++++++++----- scripts/_common.sh | 2 +- tests/test_project_setup.py | 2 +- 7 files changed, 31 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 545651d..d1de132 100644 --- a/README.md +++ b/README.md @@ -121,8 +121,10 @@ Notes: ## history -* [compare v0.1.4...master](https://github.com/YunoHost-Apps/django_ynh/compare/v0.1.4...master) **dev** +* [compare v0.1.5...master](https://github.com/YunoHost-Apps/django_ynh/compare/v0.1.5...master) **dev** * tbc +* [v0.1.5 - 17.01.2021](https://github.com/YunoHost-Apps/django_ynh/compare/v0.1.4...v0.1.5) + * Make some deps `gunicorn`, `psycopg2-binary`, `django-redis`, `django-axes` optional * [v0.1.4 - 08.01.2021](https://github.com/YunoHost-Apps/django_ynh/compare/v0.1.3...v0.1.4) * Bugfix [CSRF verification failed on POST requests #7](https://github.com/YunoHost-Apps/django_ynh/issues/7) * [v0.1.3 - 08.01.2021](https://github.com/YunoHost-Apps/django_ynh/compare/v0.1.2...v0.1.3) diff --git a/django_ynh/__init__.py b/django_ynh/__init__.py index 7525d19..66a87bb 100644 --- a/django_ynh/__init__.py +++ b/django_ynh/__init__.py @@ -1 +1 @@ -__version__ = '0.1.4' +__version__ = '0.1.5' diff --git a/django_ynh/sso_auth/auth_middleware.py b/django_ynh/sso_auth/auth_middleware.py index a3f4da2..7b41d0a 100644 --- a/django_ynh/sso_auth/auth_middleware.py +++ b/django_ynh/sso_auth/auth_middleware.py @@ -1,12 +1,17 @@ import base64 import logging -from axes.exceptions import AxesBackendPermissionDenied from django.conf import settings from django.contrib import auth from django.contrib.auth import get_user_model from django.contrib.auth.middleware import RemoteUserMiddleware + +try: + from axes.exceptions import AxesBackendPermissionDenied as SuspiciousOperation # log to Axes DB models +except ImportError: + from django.core.exceptions import SuspiciousOperation + from django_ynh.sso_auth.user_profile import call_setup_user, update_user_profile @@ -50,38 +55,38 @@ class SSOwatRemoteUserMiddleware(RemoteUserMiddleware): else: # emits a signal indicating user login failed, which is processed by # axes.signals.log_user_login_failed which logs and flags the failed request. - raise AxesBackendPermissionDenied('Cookie missing') + raise SuspiciousOperation('Cookie missing') else: logger.info('SSOwat username from cookies: %r', username) if username != user.username: - raise AxesBackendPermissionDenied('Wrong username') + raise SuspiciousOperation('Wrong username') # Compare with HTTP_AUTH_USER try: username = request.META['HTTP_AUTH_USER'] except KeyError: logger.error('HTTP_AUTH_USER missing!') - raise AxesBackendPermissionDenied('No HTTP_AUTH_USER') + raise SuspiciousOperation('No HTTP_AUTH_USER') if username != user.username: - raise AxesBackendPermissionDenied('Wrong HTTP_AUTH_USER username') + raise SuspiciousOperation('Wrong HTTP_AUTH_USER username') # Also check 'HTTP_AUTHORIZATION', but only the username ;) try: authorization = request.META['HTTP_AUTHORIZATION'] except KeyError: logger.error('HTTP_AUTHORIZATION missing!') - raise AxesBackendPermissionDenied('No HTTP_AUTHORIZATION') + raise SuspiciousOperation('No HTTP_AUTHORIZATION') scheme, creds = authorization.split(' ', 1) if scheme.lower() != 'basic': logger.error('HTTP_AUTHORIZATION with %r not supported', scheme) - raise AxesBackendPermissionDenied('HTTP_AUTHORIZATION scheme not supported') + raise SuspiciousOperation('HTTP_AUTHORIZATION scheme not supported') creds = str(base64.b64decode(creds), encoding='utf-8') username = creds.split(':', 1)[0] if username != user.username: - raise AxesBackendPermissionDenied('Wrong HTTP_AUTHORIZATION username') + raise SuspiciousOperation('Wrong HTTP_AUTHORIZATION username') if not was_authenticated: # First request, after login -> update user informations diff --git a/manifest.json b/manifest.json index b4c7049..a4b85e6 100644 --- a/manifest.json +++ b/manifest.json @@ -5,7 +5,7 @@ "description": { "en": "Glue code to package django projects as yunohost apps." }, - "version": "0.1.4~ynh1", + "version": "0.1.5~ynh1", "url": "https://github.com/jedie/django_ynh", "license": "GPL-3.0", "maintainer": { diff --git a/pyproject.toml b/pyproject.toml index 6ac0e66..8176ca5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "django_ynh" -version = "0.1.4" +version = "0.1.5" description = "Glue code to package django projects as yunohost apps." authors = ["JensDiemer "] license = "GPL" @@ -14,12 +14,14 @@ packages = [ [tool.poetry.dependencies] python = ">=3.7,<4.0.0" django = "*" -gunicorn = "*" -django-axes = "*" # https://github.com/jazzband/django-axes -psycopg2-binary = "*" -django-redis = "*" +# The follogin extra packages are used for install "django_ynh" as YunoHost app: +gunicorn = { version = "*", optional = true } +psycopg2-binary = { version = "*", optional = true } +django-redis = { version = "*", optional = true } +django-axes = { version = "*", optional = true } # https://github.com/jazzband/django-axes [tool.poetry.dev-dependencies] +django-axes = "*" # https://github.com/jazzband/django-axes poetry-publish = "*" # https://github.com/jedie/poetry-publish bx_py_utils = "*" tox = "*" @@ -33,6 +35,10 @@ flynt = "*" black = "*" pyupgrade = "*" +[tool.poetry.extras] +ynh = ["gunicorn", "psycopg2-binary", "django-redis", "django-axes"] # install as YunoHost app + + [build-system] requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api" diff --git a/scripts/_common.sh b/scripts/_common.sh index bf80263..1c6e13d 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -28,7 +28,7 @@ log_file="${log_path}/django_ynh.log" pkg_dependencies="build-essential python3-dev python3-pip python3-venv git postgresql postgresql-contrib" # To install/upgrade this project via pip: -pip_install_string="django_ynh==0.1.4" +pip_install_string="django_ynh[ynh]==0.1.5" #================================================= # Redis HELPERS diff --git a/tests/test_project_setup.py b/tests/test_project_setup.py index 037ad73..aa391b4 100644 --- a/tests/test_project_setup.py +++ b/tests/test_project_setup.py @@ -32,7 +32,7 @@ def test_version(package_root=None, version=None): assert_file_contains_string(file_path=Path(package_root, 'pyproject.toml'), string=f'version = "{version}"') assert_file_contains_string(file_path=Path(package_root, 'manifest.json'), string=f'"version": "{version}~ynh') assert_file_contains_string( - file_path=Path(package_root, 'scripts', '_common.sh'), string=f'"django_ynh=={version}"' + file_path=Path(package_root, 'scripts', '_common.sh'), string=f'"django_ynh[ynh]=={version}"' )