Skip to content
Snippets Groups Projects
Verified Commit 6be1929f authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Revert "Support custom authentication backends"

parent d39a3291
No related branches found
No related tags found
1 merge request!459Revert "Support custom authentication backends"
Pipeline #5485 passed
......@@ -2,8 +2,6 @@ from typing import Any, List, Optional, Tuple
import django.apps
from django.apps import apps
from django.conf import settings
from django.db import OperationalError, ProgrammingError
from django.http import HttpRequest
from django.utils.module_loading import autodiscover_modules
......@@ -16,7 +14,7 @@ from .registries import (
site_preferences_registry,
)
from .util.apps import AppConfig
from .util.core_helpers import get_site_preferences, has_person
from .util.core_helpers import has_person
from .util.sass_helpers import clean_scss
......@@ -52,8 +50,6 @@ class CoreConfig(AppConfig):
preference_models.register(personpreferencemodel, person_preferences_registry)
preference_models.register(grouppreferencemodel, group_preferences_registry)
self._refresh_authentication_backends()
self._load_data_checks()
from .health_checks import DataChecksHealthCheckBackend
......@@ -70,24 +66,6 @@ class CoreConfig(AppConfig):
data_checks += getattr(model, "data_checks", [])
DataCheckRegistry.data_checks = data_checks
@classmethod
def _refresh_authentication_backends(cls):
"""Refresh config list of enabled authentication backends."""
from .preferences import AuthenticationBackends # noqa
idx = settings.AUTHENTICATION_BACKENDS.index("django.contrib.auth.backends.ModelBackend")
try:
# Don't set array directly in order to keep object reference
settings._wrapped.AUTHENTICATION_BACKENDS.clear()
settings._wrapped.AUTHENTICATION_BACKENDS += settings.ORIGINAL_AUTHENTICATION_BACKENDS
for backend in get_site_preferences()["auth__backends"]:
settings._wrapped.AUTHENTICATION_BACKENDS.insert(idx, backend)
idx += 1
except (ProgrammingError, OperationalError):
pass
def preference_updated(
self,
sender: Any,
......@@ -97,9 +75,6 @@ class CoreConfig(AppConfig):
new_value: Optional[Any] = None,
**kwargs,
) -> None:
if section == "auth" and name == "backends":
self._refresh_authentication_backends()
if section == "theme":
if name in ("primary", "secondary"):
clean_scss()
......
......@@ -190,18 +190,6 @@ class SchoolNameOfficial(StringPreference):
verbose_name = _("Official name of the school, e.g. as given by supervisory authority")
@site_preferences_registry.register
class AuthenticationBackends(MultipleChoicePreference):
section = auth
name = "backends"
default = None
verbose_name = _("Enabled custom authentication backends")
field_attribute = {"initial": []}
def get_choices(self):
return [(b, b) for b in settings.CUSTOM_AUTHENTICATION_BACKENDS]
@site_preferences_registry.register
class AvailableLanguages(MultipleChoicePreference):
section = internationalisation
......
......@@ -723,6 +723,4 @@ HEALTH_CHECK = {
"MEMORY_MIN": _settings.get("health.memory_min_mb", 500),
}
ORIGINAL_AUTHENTICATION_BACKENDS = AUTHENTICATION_BACKENDS[:]
PROMETHEUS_EXPORT_MIGRATIONS = False
from typing import Optional
from django.contrib.auth import authenticate
from django.contrib.auth.backends import BaseBackend
from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.models import User
import pytest
from aleksis.core.apps import CoreConfig
from aleksis.core.util.core_helpers import get_site_preferences
pytestmark = pytest.mark.django_db
class DummyBackend(BaseBackend):
def authenticate(
self, request, username: str, password: str, **kwargs
) -> Optional[AbstractBaseUser]:
if username == "foo" and password == "baz":
return User.objects.get_or_create(username="foo")[0]
backend_name = "aleksis.core.tests.test_authentication_backends.DummyBackend"
def test_backends_simple(settings):
assert not authenticate(username="foo", password="baz")
assert backend_name not in settings.AUTHENTICATION_BACKENDS
settings.AUTHENTICATION_BACKENDS.append(backend_name)
assert backend_name in settings.AUTHENTICATION_BACKENDS
assert authenticate(username="foo", password="baz")
settings.AUTHENTICATION_BACKENDS.remove(backend_name)
assert not authenticate(username="foo", password="baz")
def test_backends_with_activation(settings):
assert not authenticate(username="foo", password="baz")
settings.CUSTOM_AUTHENTICATION_BACKENDS.append(backend_name)
assert backend_name not in get_site_preferences()["auth__backends"]
assert backend_name not in settings.AUTHENTICATION_BACKENDS
assert not authenticate(username="foo", password="baz")
print(get_site_preferences()["auth__backends"])
print(get_site_preferences()["auth__backends"].append(backend_name))
get_site_preferences()["auth__backends"] = [backend_name]
assert backend_name in get_site_preferences()["auth__backends"]
assert backend_name in settings.AUTHENTICATION_BACKENDS
assert authenticate(username="foo", password="baz")
get_site_preferences()["auth__backends"] = []
assert backend_name not in get_site_preferences()["auth__backends"]
assert backend_name not in settings.AUTHENTICATION_BACKENDS
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment