From 707e57f618745115f155911e619a89af1e986564 Mon Sep 17 00:00:00 2001 From: Ryan J Field Date: Sat, 24 Feb 2024 09:39:59 +0000 Subject: [PATCH 1/9] Fix auth in templates --- templates/rest_framework/api.html | 1 + 1 file changed, 1 insertion(+) diff --git a/templates/rest_framework/api.html b/templates/rest_framework/api.html index cc76196..8347fd5 100644 --- a/templates/rest_framework/api.html +++ b/templates/rest_framework/api.html @@ -1,4 +1,5 @@ {% extends "rest_framework/base.html" %} +{% load auth_tags %} {% load static %} From 3771a145d4f9ed8da585c0cb5ea1a2ef35e23056 Mon Sep 17 00:00:00 2001 From: Ryan J Field Date: Sat, 24 Feb 2024 10:34:16 +0000 Subject: [PATCH 2/9] fix api view --- data_management/rest/renderers.py | 6 +++++- templates/rest_framework/api.html | 24 +++++++++++++++--------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/data_management/rest/renderers.py b/data_management/rest/renderers.py index 9445c11..613fbaa 100644 --- a/data_management/rest/renderers.py +++ b/data_management/rest/renderers.py @@ -1,6 +1,6 @@ from rest_framework import renderers, serializers from rest_framework.utils.field_mapping import ClassLookupDict - +from data_management import settings class HTMLFormRenderer(renderers.HTMLFormRenderer): """ @@ -89,3 +89,7 @@ class BrowsableAPIRenderer(renderers.BrowsableAPIRenderer): Subclassing the BrowsableAPIRenderer to use our custom HTMLFormRenderer. """ form_renderer_class = HTMLFormRenderer + def get_context(self, *args, **kwargs): + context = super(BrowsableAPIRenderer, self).get_context(*args, **kwargs) + context["remote_registry"] = settings.REMOTE_REGISTRY + return(context) diff --git a/templates/rest_framework/api.html b/templates/rest_framework/api.html index 8347fd5..3e32474 100644 --- a/templates/rest_framework/api.html +++ b/templates/rest_framework/api.html @@ -20,15 +20,21 @@ {% block userlinks %} {% if remote_registry %} - - {% if user.is_authenticated %} - Logged in as: {{ user.username }} - logout - {% else %} - Login / Signup with GitHub - Login / Signup with GitLab - {% endif %} - + + {% if user.is_authenticated %} + Logged in as: {{ user.username }} + logout + {% else %} + {% is_auth_method "GitHub" as is_github %} + {% if is_github %} + Login / Signup with GitHub + {% endif %} + {% is_auth_method "GitLab" as is_gitlab %} + {% if is_gitlab %} + Login / Signup with GitLab + {% endif %} + {% endif %} + {% endif %} {% endblock %} From d8c9985467def6d2da731424860e43b6b7851b68 Mon Sep 17 00:00:00 2001 From: Ryan J Field Date: Sat, 24 Feb 2024 10:43:28 +0000 Subject: [PATCH 3/9] fix login redirect --- data_management/urls.py | 1 + drams/base_settings.py | 2 +- templates/base.html | 4 ++-- templates/rest_framework/api.html | 4 ++-- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/data_management/urls.py b/data_management/urls.py index 16353fa..dcb61a1 100644 --- a/data_management/urls.py +++ b/data_management/urls.py @@ -40,6 +40,7 @@ path('api/data/', api_views.ObjectStorageView.as_view()), path('api/data', api_views.ObjectStorageView.as_view()), path('api/auth-provider', api_views.auth_provider), + path('api/logout', views.logout), path('logout', views.logout), ] diff --git a/drams/base_settings.py b/drams/base_settings.py index 835680a..b59d515 100644 --- a/drams/base_settings.py +++ b/drams/base_settings.py @@ -162,7 +162,7 @@ ACCOUNT_UNIQUE_EMAIL = False # Redirect authenticated users to this URL -LOGIN_REDIRECT_URL = 'index' +#LOGIN_REDIRECT_URL = 'index' CRISPY_TEMPLATE_PACK = 'bootstrap3' diff --git a/templates/base.html b/templates/base.html index e460a95..4be7624 100644 --- a/templates/base.html +++ b/templates/base.html @@ -52,11 +52,11 @@ {% else %} {% is_auth_method "GitHub" as is_github %} {% if is_github %} - Login / Signup with GitHub + Login / Signup with GitHub {% endif %} {% is_auth_method "GitLab" as is_gitlab %} {% if is_gitlab %} - Login / Signup with GitLab + Login / Signup with GitLab {% endif %} {% endif %} diff --git a/templates/rest_framework/api.html b/templates/rest_framework/api.html index 3e32474..86d2dd6 100644 --- a/templates/rest_framework/api.html +++ b/templates/rest_framework/api.html @@ -27,11 +27,11 @@ {% else %} {% is_auth_method "GitHub" as is_github %} {% if is_github %} - Login / Signup with GitHub + Login / Signup with GitHub {% endif %} {% is_auth_method "GitLab" as is_gitlab %} {% if is_gitlab %} - Login / Signup with GitLab + Login / Signup with GitLab {% endif %} {% endif %} From c01280a143679a4412420bdb7523b40efa16c1fa Mon Sep 17 00:00:00 2001 From: Ryan J Field Date: Sat, 24 Feb 2024 10:45:01 +0000 Subject: [PATCH 4/9] fix logout --- templates/base.html | 2 +- templates/rest_framework/api.html | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/base.html b/templates/base.html index 4be7624..593ef0f 100644 --- a/templates/base.html +++ b/templates/base.html @@ -48,7 +48,7 @@ {% if user.is_authenticated %} Logged in as: {{ user.username }} - logout + logout {% else %} {% is_auth_method "GitHub" as is_github %} {% if is_github %} diff --git a/templates/rest_framework/api.html b/templates/rest_framework/api.html index 86d2dd6..2de5231 100644 --- a/templates/rest_framework/api.html +++ b/templates/rest_framework/api.html @@ -23,7 +23,7 @@ {% if user.is_authenticated %} Logged in as: {{ user.username }} - logout + logout {% else %} {% is_auth_method "GitHub" as is_github %} {% if is_github %} From abb4a963e4779c89c32cd8364c8cd125e7a544a3 Mon Sep 17 00:00:00 2001 From: Ryan J Field Date: Sat, 24 Feb 2024 10:49:46 +0000 Subject: [PATCH 5/9] fix logout redirect --- data_management/views.py | 2 +- templates/base.html | 2 +- templates/rest_framework/api.html | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/data_management/views.py b/data_management/views.py index 001d590..c835376 100644 --- a/data_management/views.py +++ b/data_management/views.py @@ -298,4 +298,4 @@ def external_object(request, alternate_identifier, title, version): def logout(request): """Logs out user""" auth_logout(request) - return redirect("/") \ No newline at end of file + return redirect(request.META.get('HTTP_REFERER', '/')) \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index 593ef0f..4be7624 100644 --- a/templates/base.html +++ b/templates/base.html @@ -48,7 +48,7 @@ {% if user.is_authenticated %} Logged in as: {{ user.username }} - logout + logout {% else %} {% is_auth_method "GitHub" as is_github %} {% if is_github %} diff --git a/templates/rest_framework/api.html b/templates/rest_framework/api.html index 2de5231..86d2dd6 100644 --- a/templates/rest_framework/api.html +++ b/templates/rest_framework/api.html @@ -23,7 +23,7 @@ {% if user.is_authenticated %} Logged in as: {{ user.username }} - logout + logout {% else %} {% is_auth_method "GitHub" as is_github %} {% if is_github %} From d534c2322da15cf9cd57bf48d901148e98130ec9 Mon Sep 17 00:00:00 2001 From: Ryan J Field Date: Mon, 11 Mar 2024 15:48:20 +0000 Subject: [PATCH 6/9] Update urls --- data_management/rest/views.py | 14 +++++ data_management/tests/test_api.py | 3 +- data_management/tests/test_custom_urls.py | 64 +++++++++++++++++++++++ data_management/urls.py | 3 +- 4 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 data_management/tests/test_custom_urls.py diff --git a/data_management/rest/views.py b/data_management/rest/views.py index acc4582..c9267f4 100644 --- a/data_management/rest/views.py +++ b/data_management/rest/views.py @@ -569,4 +569,18 @@ def auth_provider(request): _data = { "auth_provider":conf_settings.AUTH_METHOD } + return JsonResponse(_data) + +def auth_url(request): + """Returns Auth Provider URL in Json Format""" + auth_url = None + if conf_settings.SOCIAL_AUTH_GITLAB_API_URL: + auth_url = conf_settings.SOCIAL_AUTH_GITLAB_API_URL + elif conf_settings.AUTH_METHOD == 'GitLab': + auth_url = "https://gitlab.com" + elif conf_settings.AUTH_METHOD == 'GitHub': + auth_url = "https://github.com" + _data = { + "auth_url":auth_url + } return JsonResponse(_data) \ No newline at end of file diff --git a/data_management/tests/test_api.py b/data_management/tests/test_api.py index 15e6d2d..39ddcad 100644 --- a/data_management/tests/test_api.py +++ b/data_management/tests/test_api.py @@ -827,8 +827,7 @@ def test_filter_by_key(self): self.assertEqual(response['Content-Type'], 'application/json') results = response.json()['results'] self.assertEqual(len(results), 1) - self.assertEqual(results[0]['key'], 'TestKey2') - + self.assertEqual(results[0]['key'], 'TestKey2') class ProvAPITests(TestCase): diff --git a/data_management/tests/test_custom_urls.py b/data_management/tests/test_custom_urls.py new file mode 100644 index 0000000..8170fad --- /dev/null +++ b/data_management/tests/test_custom_urls.py @@ -0,0 +1,64 @@ +from django.conf import settings +from django.test import TestCase +from django.urls import reverse +from django.contrib.auth import get_user_model +from rest_framework.test import APIClient + +from .initdb import init_db + +class CustomURLTests(TestCase): + def setUp(self): + settings.AUTH_METHOD = "GitLab" + settings.SOCIAL_AUTH_GITLAB_API_URL = "https://test.com" + self.user = get_user_model().objects.create(username='Test User') + init_db() + + def test_auth_provider_url(self): + client = APIClient() + url = reverse('auth-provider') + response = client.get(url, format='json') + + self.assertEqual(response.status_code, 200) + self.assertEqual(response['Content-Type'], 'application/json') + self.assertEqual(response.json()['auth_provider'], 'GitLab') + + def test_auth_url(self): + client = APIClient() + url = reverse('auth-url') + response = client.get(url, format='json') + + self.assertEqual(response.status_code, 200) + self.assertEqual(response['Content-Type'], 'application/json') + self.assertEqual(response.json()['auth_url'], 'https://test.com') + + def test_auth_url_github(self): + client = APIClient() + settings.AUTH_METHOD = "GitHub" + settings.SOCIAL_AUTH_GITLAB_API_URL = None + url = reverse('auth-url') + response = client.get(url, format='json') + + self.assertEqual(response.status_code, 200) + self.assertEqual(response['Content-Type'], 'application/json') + self.assertEqual(response.json()['auth_url'], 'https://github.com') + + def test_auth_url_github_no_api(self): + client = APIClient() + settings.AUTH_METHOD = "GitLab" + settings.SOCIAL_AUTH_GITLAB_API_URL = None + url = reverse('auth-url') + response = client.get(url, format='json') + + self.assertEqual(response.status_code, 200) + self.assertEqual(response['Content-Type'], 'application/json') + self.assertEqual(response.json()['auth_url'], 'https://gitlab.com') + + def test_auth_provider_url_github(self): + client = APIClient() + settings.AUTH_METHOD = "GitHub" + url = reverse('auth-provider') + response = client.get(url, format='json') + + self.assertEqual(response.status_code, 200) + self.assertEqual(response['Content-Type'], 'application/json') + self.assertEqual(response.json()['auth_provider'], 'GitHub') diff --git a/data_management/urls.py b/data_management/urls.py index dcb61a1..a4f684c 100644 --- a/data_management/urls.py +++ b/data_management/urls.py @@ -39,7 +39,8 @@ path('data/', views.get_data), path('api/data/', api_views.ObjectStorageView.as_view()), path('api/data', api_views.ObjectStorageView.as_view()), - path('api/auth-provider', api_views.auth_provider), + path('api/auth-provider', api_views.auth_provider, name='auth-provider'), + path('api/auth-url', api_views.auth_url, name='auth-url'), path('api/logout', views.logout), path('logout', views.logout), ] From 040c45c99b87db92a785637f9d5dfededcf48f70 Mon Sep 17 00:00:00 2001 From: Ryan J Field Date: Tue, 12 Mar 2024 14:45:33 +0000 Subject: [PATCH 7/9] fix urls for cli --- data_management/urls.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_management/urls.py b/data_management/urls.py index a4f684c..10c1fc5 100644 --- a/data_management/urls.py +++ b/data_management/urls.py @@ -39,8 +39,8 @@ path('data/', views.get_data), path('api/data/', api_views.ObjectStorageView.as_view()), path('api/data', api_views.ObjectStorageView.as_view()), - path('api/auth-provider', api_views.auth_provider, name='auth-provider'), - path('api/auth-url', api_views.auth_url, name='auth-url'), + path('api/auth-provider/', api_views.auth_provider, name='auth-provider'), + path('api/auth-url/', api_views.auth_url, name='auth-url'), path('api/logout', views.logout), path('logout', views.logout), ] From c8c89257fa28cb745928408f90c6b4f0e967e70d Mon Sep 17 00:00:00 2001 From: Ryan J Field Date: Tue, 12 Mar 2024 15:06:17 +0000 Subject: [PATCH 8/9] Fix missing key reference --- data_management/rest/views.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/data_management/rest/views.py b/data_management/rest/views.py index c9267f4..30ad28e 100644 --- a/data_management/rest/views.py +++ b/data_management/rest/views.py @@ -574,11 +574,12 @@ def auth_provider(request): def auth_url(request): """Returns Auth Provider URL in Json Format""" auth_url = None - if conf_settings.SOCIAL_AUTH_GITLAB_API_URL: - auth_url = conf_settings.SOCIAL_AUTH_GITLAB_API_URL - elif conf_settings.AUTH_METHOD == 'GitLab': + if hasattr(conf_settings, 'SOCIAL_AUTH_GITLAB_API_URL'): + if conf_settings.SOCIAL_AUTH_GITLAB_API_URL: + auth_url = conf_settings.SOCIAL_AUTH_GITLAB_API_URL + if conf_settings.AUTH_METHOD == 'GitLab' and not auth_url: auth_url = "https://gitlab.com" - elif conf_settings.AUTH_METHOD == 'GitHub': + if conf_settings.AUTH_METHOD == 'GitHub' and not auth_url: auth_url = "https://github.com" _data = { "auth_url":auth_url From 62490d7f599696efa866f73a5fded2dc9bb44edf Mon Sep 17 00:00:00 2001 From: Ryan J Field Date: Wed, 13 Mar 2024 21:13:15 +0000 Subject: [PATCH 9/9] Add domain url --- drams/settings.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drams/settings.py b/drams/settings.py index 264133c..7f2d565 100644 --- a/drams/settings.py +++ b/drams/settings.py @@ -5,6 +5,8 @@ ALLOWED_HOSTS = ['data.fairdatapipeline.org', '127.0.0.1', 'localhost'] +DOMAIN_URL = 'https://data.fairdatapipeline.org/' + REMOTE = True DATABASES = {