FreedomBox/plinth/web_framework.py
Sunil Mohan Adapa 770974c8ce
sso: Switch to django-axes >= 5.0
- Add explicit dependency on django-ipware >=3. django-axes >= 6 adds
only and optional dependency on django-ipware. Adding explicit dependency make
the behavior safer.

- Depend on django-axes >= 5 where the authentication backend and other features
are available. The new code won't work with older versions. The new approach
uses and authentication backend to deny access to the login form on lockout and
a middleware to redirect user to locked out form when limit of attempts have
been reached.

- Drop old code used for compatibility with django-axes 3.x.

- Suppress verbose and debug messages as django-axes is too chatty.

- Re-implment the CAPTCHA form entirely. In the old style, we have a login form
with CAPTCHA field. That would not work with the new django-axes authentication
middle. On submission of the form, auth.authenticate() will be called. This
call invokes various authentication backends include django-axes authentication
backend. This backend's behavior is to reject all authentication attempts when
the IP is listed in locked table. The new approach is to provide a simple
CAPTCHA form with just the CAPTCHA field. If the form is successfully
validated (correct CAPTCHA is provided), then the lock on the IP address is
reset. The user is then free to perform 3 more attempts to login.

- Update firstboot form to send the request parameter when using
auth.authenticate() method. This needed by Django axes' authentication method
which will be triggered.

Tests:

- Run tests on Debian Bookworm and Debian testing.

- Axes verbose messages and debug messages are not printed on the console when
running FreedomBox in debug mode.

- Only three invalid attempts are allowed at the login page. After the final
incorrect attempt, user is redirected to CAPTCHA page. Visiting the login page
using the URL works but entering the correct credentials still takes the user to
CAPTCHA page.

- CAPTCHA form appears as expected. Clicking the CAPTCHA images downloads the
audio file corresponding to the image. Incorrect CAPTCHA shows an error. Correct
CAPTCHA takes the user to login form where they are able to login with correct
credentials. Entering incorrect credentials 3 times will take the user again to
CAPTCHA page.

- Creating user account during firstboot works.

- Blocked IP address the IP of the client such as 10.42.0.1 and not the local IP
address 127.0.0.1 according the django-axes log messages. While one client IP
address is blocked, another IP is able to login to the same user account that
was attempted by the blocked client.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2023-08-23 21:47:39 -04:00

132 lines
4.2 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Setup Django web framework.
"""
import logging
import os
import pathlib
import random
import stat
import django.conf
import django.core.management
import django.core.wsgi
from django.conf import global_settings
from django.contrib.messages import constants as message_constants
from . import cfg, glib, log, module_loader, settings
logger = logging.getLogger(__name__)
def init():
"""Setup Django configuration in the absence of .settings file"""
# Workaround for django-simple-captcha 0.5.6 not being compatible with
# Django 3.2. 0.5.14 is almost there in Debian. Workaround only until then.
django.utils.encoding.python_2_unicode_compatible = lambda x: x
if cfg.secure_proxy_ssl_header:
settings.SECURE_PROXY_SSL_HEADER = (cfg.secure_proxy_ssl_header,
'https')
if cfg.use_x_forwarded_for:
settings.IPWARE_META_PRECEDENCE_ORDER = ('HTTP_X_FORWARDED_FOR', )
settings.DATABASES['default']['NAME'] = cfg.store_file
settings.DEBUG = cfg.develop
settings.FORCE_SCRIPT_NAME = cfg.server_dir
settings.INSTALLED_APPS += module_loader.get_modules_to_load()
settings.LANGUAGES = get_languages()
settings.LOGGING = log.get_configuration()
settings.MESSAGE_TAGS = {message_constants.ERROR: 'danger'}
settings.SECRET_KEY = _get_secret_key()
settings.SESSION_FILE_PATH = os.path.join(cfg.data_dir, 'sessions')
settings.STATIC_URL = '/'.join([cfg.server_dir,
'static/']).replace('//', '/')
settings.USE_X_FORWARDED_HOST = cfg.use_x_forwarded_host
kwargs = {}
for setting in dir(settings):
if setting.isupper():
kwargs[setting] = getattr(settings, setting)
django.conf.settings.configure(**kwargs)
django.setup(set_prefix=True)
logger.debug('Configured Django with applications - %s',
', '.join(settings.INSTALLED_APPS))
def post_init():
"""Perform operations after completing init of other modules."""
logger.debug('Creating or adding new tables to data file')
django.core.management.call_command('migrate', '--fake-initial',
interactive=False, verbosity=0)
os.chmod(cfg.store_file, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP)
# Cleanup expired sessions every day
glib.schedule(24 * 3600, _cleanup_expired_sessions, in_thread=True)
def _get_secret_key():
"""Retrieve or create a new Django secret key."""
secret_key_file = pathlib.Path(cfg.data_dir) / 'django-secret.key'
if secret_key_file.exists():
secret_key = secret_key_file.read_text()
if len(secret_key) >= 128:
return secret_key
secret_key = _generate_secret_key()
# File should be created with permission 0o700
old_umask = os.umask(0o077)
try:
secret_key_file.write_text(secret_key)
finally:
os.umask(old_umask)
return secret_key
def _generate_secret_key():
"""Generate a new random secret key for use with Django."""
# We could have used django.core.management.utils.get_random_secret_key
# but it is not documented and should be considered private.
length = 128
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
rand = random.SystemRandom()
return ''.join(rand.choice(chars) for _ in range(length))
def get_languages():
"""Return list of languages to show in the interface.
Add additional languages that FreedomBox support but Django doesn't.
"""
def gettext_noop(string):
"""Django's actual translation methods need Django to be setup."""
return string
return sorted(
list(global_settings.LANGUAGES) + [
('gu', gettext_noop('Gujarati')),
])
def _cleanup_expired_sessions(data):
"""Cleanup expired Django sessions."""
verbosity = 1 if cfg.develop else 0
django.core.management.call_command('clearsessions', verbosity=verbosity)
def get_wsgi_application():
"""Return Django wsgi application."""
return django.core.wsgi.get_wsgi_application()
def get_static_url():
"""Return Django static URL."""
return django.conf.settings.STATIC_URL