Skip to content

Commit

Permalink
Relax LOGIN_DEFAULT_SESSION_AUTHENTICATION_BACKEND setting (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
apragacz committed Sep 20, 2022
1 parent 89a42c3 commit ac43874
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
4 changes: 2 additions & 2 deletions rest_registration/settings_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,14 @@ def __new__(
),
Field(
'LOGIN_DEFAULT_SESSION_AUTHENTICATION_BACKEND',
default='django.contrib.auth.backends.ModelBackend',
default=None,
help=dedent("""\
This setting allows to override the backend used in the login function.
It may be useful if Django ``AUTHENTICATION_BACKENDS`` setting
does contain multiple values.
The value must be a dotted import path string.
The value must be a dotted import path string or ``None``.
"""),
)
]
Expand Down
6 changes: 4 additions & 2 deletions rest_registration/utils/auth_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
from django.contrib.auth.base_user import AbstractBaseUser


def get_login_authentication_backend(user: Optional['AbstractBaseUser'] = None) -> str:
def get_login_authentication_backend(
user: Optional['AbstractBaseUser'] = None
) -> Optional[str]:
if user and hasattr(user, 'backend'):
return user.backend # type: ignore
backends = settings.AUTHENTICATION_BACKENDS
Expand All @@ -18,7 +20,7 @@ def get_login_authentication_backend(user: Optional['AbstractBaseUser'] = None)
raise ImproperlyConfigured("No AUTHENTICATION_BACKENDS specified")
if len(backends) == 1:
return backends[0]
if default_login_backend not in backends:
if default_login_backend is not None and default_login_backend not in backends:
raise ImproperlyConfigured(
"LOGIN_DEFAULT_SESSION_AUTHENTICATION_BACKEND"
" is not in AUTHENTICATION_BACKENDS")
Expand Down
20 changes: 16 additions & 4 deletions tests/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,23 @@ def test_when_authtokenmanager_does_not_implement_methods_then_check_fails():
'django.contrib.auth.backends.RemoteUserBackend',
],
)
def test_when_multiple_auth_backends_then_check_succeeds():
def test_ok_when_multiple_auth_backends():
errors = simulate_checks()
assert_error_codes_match(errors, [])


@override_rest_registration_settings({
'LOGIN_DEFAULT_SESSION_AUTHENTICATION_BACKEND': 'django.contrib.auth.backends.RemoteUserBackend', # noqa: E501
})
@override_settings(
AUTHENTICATION_BACKENDS=[
'django.contrib.auth.backends.ModelBackend',
'django.contrib.auth.backends.RemoteUserBackend',
],
)
def test_ok_when_login_auth_backend_in_multiple_auth_backends():
errors = simulate_checks()
assert_error_codes_match(errors, [])
expected_messages = set()
assert {e.msg for e in errors} == expected_messages


@override_rest_registration_settings({
Expand All @@ -369,7 +381,7 @@ def test_when_multiple_auth_backends_then_check_succeeds():
'django.contrib.auth.backends.RemoteUserBackend',
],
)
def test_when_login_auth_backend_not_in_multiple_auth_backends_then_check_fails():
def test_fail_when_login_auth_backend_not_in_multiple_auth_backends():
errors = simulate_checks()
assert_error_codes_match(errors, [
ErrorCode.INVALID_AUTH_BACKENDS_CONFIG,
Expand Down

0 comments on commit ac43874

Please sign in to comment.