45 lines
1.7 KiB
Python
45 lines
1.7 KiB
Python
from django.urls import reverse_lazy
|
|
from django.conf import settings
|
|
from django.utils.translation import gettext_lazy as _
|
|
from django.contrib.auth import views as auth_views
|
|
from django.contrib.auth import login as auth_login
|
|
from django.http import HttpResponseRedirect
|
|
from nacl import secret
|
|
|
|
|
|
class LoginView(auth_views.LoginView):
|
|
template_name = 'auth/login.html'
|
|
extra_context = {
|
|
'title': _('Login'),
|
|
'success_url': reverse_lazy('idhub:user_dashboard'),
|
|
}
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
if request.GET.get('next'):
|
|
self.extra_context['success_url'] = request.GET.get('next')
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
def form_valid(self, form):
|
|
user = form.get_user()
|
|
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
|
|
password = form.cleaned_data.get("password")
|
|
# Decrypt the user's sensitive data encryption key and store it in the session.
|
|
self.decript_key(user, password)
|
|
|
|
auth_login(self.request, user)
|
|
return HttpResponseRedirect(self.extra_context['success_url'])
|
|
|
|
def decript_key(self, user, password):
|
|
if not settings.KEY_CREDENTIALS:
|
|
return
|
|
|
|
sb_key = user.derive_key_from_password(password)
|
|
sb = secret.SecretBox(sb_key)
|
|
data_decript = sb.decrypt(settings.KEY_CREDENTIALS)
|
|
settings.KEY_CREDENTIALS_CLEAN = data_decript
|
|
|