From b031f2ac0856c649a53893a79fedfaf74d0651c4 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 19 Jan 2024 10:59:35 +0100 Subject: [PATCH] admin2fauth --- idhub/admin/views.py | 18 ++++++++++++++++++ idhub/mixins.py | 3 +++ idhub/urls.py | 2 ++ idhub/views.py | 3 +++ 4 files changed, 26 insertions(+) diff --git a/idhub/admin/views.py b/idhub/admin/views.py index 44e36af..b7a13df 100644 --- a/idhub/admin/views.py +++ b/idhub/admin/views.py @@ -48,6 +48,24 @@ from idhub.models import ( ) +class DobleFactorAuthView(AdminView): + url = reverse_lazy('idhub:admin_dashboard') + + def get(self, request, *args, **kwargs): + import pdb; pdb.set_trace() + if not self.request.session.get("2fauth"): + return redirect(self.url) + + if self.request.session.get("2fauth") == '0c9116a7-c6e5-41d7-bbf0-e8492cdfca23' + if not request.user.is_admin: + return redirect(url) + + if self.request.session.get("2fauth"): + return redirect(reverse_lazy("idhub:login")) + + return super().get(request, *args, **kwargs) + + class DashboardView(AdminView, SingleTableView): template_name = "idhub/admin/dashboard.html" table_class = DashboardTable diff --git a/idhub/mixins.py b/idhub/mixins.py index 68b7344..f118b1e 100644 --- a/idhub/mixins.py +++ b/idhub/mixins.py @@ -30,4 +30,7 @@ class AdminView(UserView): url = reverse_lazy('idhub:user_dashboard') return redirect(url) + if self.request.session.get("2fauth"): + return redirect(reverse_lazy("idhub:login")) + return super().get(request, *args, **kwargs) diff --git a/idhub/urls.py b/idhub/urls.py index 7f983c3..3583ac8 100644 --- a/idhub/urls.py +++ b/idhub/urls.py @@ -175,6 +175,8 @@ urlpatterns = [ name='admin_import'), path('admin/import/new', views_admin.ImportAddView.as_view(), name='admin_import_add'), + path('admin/auth/', views_admin.DobleFactorAuthView.as_view(), + name='admin_2fauth'), path('did-registry//did.json', serve_did) diff --git a/idhub/views.py b/idhub/views.py index f513353..04e8c21 100644 --- a/idhub/views.py +++ b/idhub/views.py @@ -1,3 +1,4 @@ +import uuid from django.shortcuts import get_object_or_404 from django.urls import reverse_lazy from django.conf import settings @@ -41,6 +42,8 @@ class LoginView(auth_views.LoginView): # ) # cache.set("KEY_DIDS", encryption_key, None) cache.set("KEY_DIDS", sensitive_data_encryption_key, None) + # self.request.session["2fauth"] = uuid.uuid4() + self.request.session["2fauth"] = '0c9116a7-c6e5-41d7-bbf0-e8492cdfca23' self.request.session["key_did"] = user.encrypt_data( sensitive_data_encryption_key,