From 7c1dd15a5a95f32df38863febd5bd07beb05bc62 Mon Sep 17 00:00:00 2001 From: Hanne Moa Date: Mon, 7 Oct 2024 14:36:08 +0200 Subject: [PATCH 1/3] Remove unused auth util function --- src/argus/auth/utils.py | 9 --------- tests/auth/test_utils.py | 7 +------ 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/src/argus/auth/utils.py b/src/argus/auth/utils.py index f38dcad6e..c96f25202 100644 --- a/src/argus/auth/utils.py +++ b/src/argus/auth/utils.py @@ -13,15 +13,6 @@ def get_authentication_backend_classes(): return backends -def get_psa_authentication_names(backends=None): - backends = backends if backends else get_authentication_backend_classes() - psa_backends = set() - for backend in backends: - if issubclass(backend, BaseAuth): - psa_backends.add(backend.name) - return sorted(psa_backends) - - def get_authentication_backend_name_and_type(request): backends = get_authentication_backend_classes() data = [] diff --git a/tests/auth/test_utils.py b/tests/auth/test_utils.py index 71cff921d..616032d0f 100644 --- a/tests/auth/test_utils.py +++ b/tests/auth/test_utils.py @@ -3,7 +3,7 @@ from django.contrib.auth.backends import ModelBackend from django.test import TestCase -from argus.auth.utils import get_authentication_backend_name_and_type, get_psa_authentication_names +from argus.auth.utils import get_authentication_backend_name_and_type from argus.dataporten.social import DataportenFeideOAuth2 @@ -33,8 +33,3 @@ def test_get_authentication_backend_name_and_type_returns_feide_login( "name": "dataporten_feide", } ] - - @patch("argus.auth.utils.get_authentication_backend_classes") - def test_get_psa_authentication_names_returns_feide_name(self, mock_get_authentication_backend_classes): - mock_get_authentication_backend_classes.return_value = [DataportenFeideOAuth2] - assert get_psa_authentication_names() == ["dataporten_feide"] From e5afe0e322ef38d68ea234566bcfd9da24f9231e Mon Sep 17 00:00:00 2001 From: Hanne Moa Date: Tue, 8 Oct 2024 14:45:39 +0200 Subject: [PATCH 2/3] Add some helper functions --- src/argus/auth/utils.py | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/argus/auth/utils.py b/src/argus/auth/utils.py index c96f25202..780324b92 100644 --- a/src/argus/auth/utils.py +++ b/src/argus/auth/utils.py @@ -1,22 +1,45 @@ from django.conf import settings -from django.contrib.auth.backends import ModelBackend +from django.contrib.auth.backends import ModelBackend, RemoteUserBackend from django.utils.module_loading import import_string from rest_framework.reverse import reverse -from social_core.backends.base import BaseAuth + from social_core.backends.oauth import BaseOAuth2 +_all__ = [ + "get_authentication_backend_classes", + "has_model_backend", + "has_remote_user_backend", + "get_psa_authentication_backends", + "get_authentication_backend_name_and_type", +] + + def get_authentication_backend_classes(): backend_dotted_paths = getattr(settings, "AUTHENTICATION_BACKENDS") backends = [import_string(path) for path in backend_dotted_paths] return backends +def has_model_backend(backends): + return ModelBackend in backends + + +def has_remote_user_backend(backends): + return RemoteUserBackend in backends + + +def get_psa_authentication_backends(backends=None): + backends = backends if backends else get_authentication_backend_classes() + return [backend for backend in backends if issubclass(backend, BaseOAuth2)] + + def get_authentication_backend_name_and_type(request): + # Needed for SPA /login-methods/ API endpoint backends = get_authentication_backend_classes() data = [] - if ModelBackend in backends: + if has_model_backend(backends): data.append( { "type": "username_password", @@ -31,8 +54,7 @@ def get_authentication_backend_name_and_type(request): "url": reverse("social:begin", kwargs={"backend": backend.name}, request=request), "name": backend.name, } - for backend in backends - if issubclass(backend, BaseOAuth2) + for backend in get_psa_authentication_backends(backends) ) return data From 0334b4031229e7fa475de39d95ca04c20dd04940 Mon Sep 17 00:00:00 2001 From: Hanne Moa Date: Wed, 9 Oct 2024 15:00:50 +0200 Subject: [PATCH 3/3] Add changelog fragment --- changelog.d/+oauth2.removed.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelog.d/+oauth2.removed.md diff --git a/changelog.d/+oauth2.removed.md b/changelog.d/+oauth2.removed.md new file mode 100644 index 000000000..836e918ec --- /dev/null +++ b/changelog.d/+oauth2.removed.md @@ -0,0 +1,3 @@ +As part of refactoring some authentication utility functions the function +`get_psa_authentication_names()` has been removed as it wasn't used anywhere in +Argus proper.