IdHub/idhub/views.py

75 lines
2.6 KiB
Python
Raw Normal View History

2024-01-19 09:59:35 +00:00
import uuid
2024-01-15 09:34:42 +00:00
from django.shortcuts import get_object_or_404
2023-09-29 16:06:17 +00:00
from django.urls import reverse_lazy
2024-01-17 11:40:54 +00:00
from django.conf import settings
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
2024-01-15 09:34:42 +00:00
from django.http import HttpResponseRedirect, HttpResponse
from idhub.models import DID
from trustchain_idhub import settings
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):
2024-01-12 16:22:28 +00:00
self.extra_context['success_url'] = request.GET.get(
'next',
reverse_lazy('idhub:user_dashboard')
)
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
password = form.cleaned_data.get("password")
2024-01-06 18:18:59 +00:00
auth_login(self.request, user)
2024-01-04 11:43:24 +00:00
sensitive_data_encryption_key = user.decrypt_sensitive_data(password)
2024-01-06 18:18:59 +00:00
2023-11-21 14:20:15 +00:00
if not user.is_anonymous and user.is_admin:
admin_dashboard = reverse_lazy('idhub:admin_dashboard')
2024-01-12 16:22:28 +00:00
self.extra_context['success_url'] = admin_dashboard
2024-01-17 12:43:40 +00:00
# encryption_key = user.encrypt_data(
# sensitive_data_encryption_key,
# settings.SECRET_KEY
# )
# cache.set("KEY_DIDS", encryption_key, None)
cache.set("KEY_DIDS", sensitive_data_encryption_key, None)
2024-01-19 09:59:35 +00:00
# self.request.session["2fauth"] = uuid.uuid4()
self.request.session["2fauth"] = '0c9116a7-c6e5-41d7-bbf0-e8492cdfca23'
2024-01-03 19:14:04 +00:00
2024-01-06 18:18:59 +00:00
self.request.session["key_did"] = user.encrypt_data(
sensitive_data_encryption_key,
user.password+self.request.session._session_key
)
2023-11-21 14:20:15 +00:00
return HttpResponseRedirect(self.extra_context['success_url'])
2024-01-15 09:34:42 +00:00
2024-01-04 11:43:24 +00:00
class PasswordResetConfirmView(auth_views.PasswordResetConfirmView):
template_name = 'auth/password_reset_confirm.html'
success_url = reverse_lazy('idhub:password_reset_complete')
def form_valid(self, form):
password = form.cleaned_data.get("password")
user = form.get_user()
user.set_encrypted_sensitive_data(password)
user.save()
return HttpResponseRedirect(self.success_url)
2024-01-17 13:11:47 +00:00
2024-01-15 09:34:42 +00:00
def serve_did(request, did_id):
2024-01-16 13:00:59 +00:00
id_did = f'did:web:{settings.DOMAIN}:did-registry:{did_id}'
did = get_object_or_404(DID, did=id_did)
document = did.didweb_document
2024-01-15 09:34:42 +00:00
retval = HttpResponse(document)
retval.headers["Content-Type"] = "application/json"
return retval