IdHub/idhub/views.py

44 lines
1.7 KiB
Python
Raw Normal View History

2023-09-29 16:06:17 +00:00
from django.urls import reverse_lazy
2024-01-03 18:53:11 +00:00
from django.core.cache import cache
2023-09-29 16:06:17 +00:00
from django.utils.translation import gettext_lazy as _
2023-10-09 08:49:56 +00:00
from django.contrib.auth import views as auth_views
2023-11-21 14:20:15 +00:00
from django.contrib.auth import login as auth_login
from django.http import HttpResponseRedirect
from nacl import secret
2023-09-28 09:01:14 +00:00
2023-10-09 08:49:56 +00:00
class LoginView(auth_views.LoginView):
2023-09-29 16:06:17 +00:00
template_name = 'auth/login.html'
extra_context = {
'title': _('Login'),
2023-10-09 08:49:56 +00:00
'success_url': reverse_lazy('idhub:user_dashboard'),
2023-09-29 16:06:17 +00:00
}
2023-09-28 09:01:14 +00:00
2023-10-16 17:08:18 +00:00
def get(self, request, *args, **kwargs):
2023-10-09 08:49:56 +00:00
if request.GET.get('next'):
self.extra_context['success_url'] = request.GET.get('next')
2023-10-16 17:08:18 +00:00
return super().get(request, *args, **kwargs)
2023-11-21 14:20:15 +00:00
def form_valid(self, form):
user = form.get_user()
2024-01-03 19:14:04 +00:00
# Decrypt the user's sensitive data encryption key and store it in the session.
password = form.cleaned_data.get("password")
sensitive_data_encryption_key = user.decrypt_sensitive_data_encryption_key(password)
key_dids = cache.get("KEY_DIDS", {})
2023-11-21 14:20:15 +00:00
if not user.is_anonymous and user.is_admin:
user_dashboard = reverse_lazy('idhub:user_dashboard')
admin_dashboard = reverse_lazy('idhub:admin_dashboard')
if self.extra_context['success_url'] == user_dashboard:
self.extra_context['success_url'] = admin_dashboard
2024-01-03 19:14:04 +00:00
key_dids[user.id] = sensitive_data_encryption_key
cache.set("KEY_DIDS", key_dids, None)
else:
key_dids[user.id] = sensitive_data_encryption_key
cache.set("KEY_DIDS", key_dids)
2023-11-21 14:20:15 +00:00
auth_login(self.request, user)
2024-01-03 18:53:11 +00:00
return HttpResponseRedirect(self.extra_context['success_url'])