2024-02-21 13:19:59 +00:00
|
|
|
import logging
|
|
|
|
|
2023-10-10 08:52:04 +00:00
|
|
|
from django import forms
|
2023-11-28 08:39:02 +00:00
|
|
|
from django.conf import settings
|
2023-12-14 16:59:40 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2023-11-24 15:36:05 +00:00
|
|
|
from idhub.models import DID, VerificableCredential
|
|
|
|
from oidc4vp.models import Organization
|
2024-01-22 15:00:15 +00:00
|
|
|
from idhub_auth.models import User
|
2023-11-02 16:13:49 +00:00
|
|
|
|
2023-12-19 17:33:09 +00:00
|
|
|
|
2024-02-21 13:19:59 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2023-10-11 07:52:05 +00:00
|
|
|
class ProfileForm(forms.ModelForm):
|
2023-10-10 08:52:04 +00:00
|
|
|
MANDATORY_FIELDS = ['first_name', 'last_name', 'email']
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = User
|
2023-11-02 13:17:07 +00:00
|
|
|
fields = ('first_name', 'last_name', 'email')
|
|
|
|
|
|
|
|
|
2024-01-20 14:06:30 +00:00
|
|
|
class TermsConditionsForm(forms.Form):
|
2024-02-01 12:32:28 +00:00
|
|
|
accept_privacy = forms.BooleanField(
|
|
|
|
widget=forms.CheckboxInput(attrs={'class': 'form-check-input'}),
|
|
|
|
required=False
|
|
|
|
)
|
|
|
|
accept_legal = forms.BooleanField(
|
|
|
|
widget=forms.CheckboxInput(attrs={'class': 'form-check-input'}),
|
|
|
|
required=False
|
|
|
|
)
|
|
|
|
accept_cookies = forms.BooleanField(
|
|
|
|
widget=forms.CheckboxInput(attrs={'class': 'form-check-input'}),
|
2024-01-20 14:06:30 +00:00
|
|
|
required=False
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.user = kwargs.pop('user', None)
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2024-02-01 12:32:28 +00:00
|
|
|
def get_label(self, url, read):
|
2024-02-05 11:25:02 +00:00
|
|
|
label = _('I read and accepted the')
|
2024-02-01 12:32:28 +00:00
|
|
|
label += f' <a class="btn btn-green-user" target="_blank" href="{url}" '
|
|
|
|
label += f'title="{read}">{read}</a>'
|
|
|
|
return label
|
|
|
|
|
|
|
|
def privacy_label(self):
|
|
|
|
url = "https://laweb.pangea.org/politica-de-privacitat/"
|
2024-02-05 13:58:54 +00:00
|
|
|
read = _("Privacy policy")
|
2024-02-01 12:32:28 +00:00
|
|
|
return self.get_label(url, read)
|
|
|
|
|
|
|
|
def legal_label(self):
|
|
|
|
url = "https://laweb.pangea.org/avis-legal/"
|
2024-02-05 13:58:54 +00:00
|
|
|
read = _("Legal policy")
|
2024-02-01 12:32:28 +00:00
|
|
|
return self.get_label(url, read)
|
|
|
|
|
|
|
|
def cookies_label(self):
|
|
|
|
url = "https://laweb.pangea.org/politica-de-cookies-2/"
|
2024-02-05 13:58:54 +00:00
|
|
|
read = _("Cookies policy")
|
2024-02-01 12:32:28 +00:00
|
|
|
return self.get_label(url, read)
|
|
|
|
|
2024-01-20 14:06:30 +00:00
|
|
|
def clean(self):
|
|
|
|
data = self.cleaned_data
|
2024-02-01 12:32:28 +00:00
|
|
|
privacy = data.get("accept_privacy")
|
|
|
|
legal = data.get("accept_legal")
|
|
|
|
cookies = data.get("accept_cookies")
|
|
|
|
if privacy and legal and cookies:
|
2024-01-20 14:06:30 +00:00
|
|
|
self.user.accept_gdpr = True
|
|
|
|
else:
|
|
|
|
self.user.accept_gdpr = False
|
|
|
|
return data
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
|
|
|
|
if commit:
|
|
|
|
self.user.save()
|
|
|
|
return self.user
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
2023-11-02 13:17:07 +00:00
|
|
|
class RequestCredentialForm(forms.Form):
|
2023-12-14 16:59:40 +00:00
|
|
|
did = forms.ChoiceField(label=_("Did"), choices=[])
|
|
|
|
credential = forms.ChoiceField(label=_("Credential"), choices=[])
|
2023-11-02 13:17:07 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.user = kwargs.pop('user', None)
|
2024-01-15 18:11:22 +00:00
|
|
|
self.lang = kwargs.pop('lang', None)
|
|
|
|
self._domain = kwargs.pop('domain', None)
|
2023-11-02 13:17:07 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.fields['did'].choices = [
|
|
|
|
(x.did, x.label) for x in DID.objects.filter(user=self.user)
|
|
|
|
]
|
|
|
|
self.fields['credential'].choices = [
|
2024-01-15 18:11:22 +00:00
|
|
|
(x.id, x.get_type(lang=self.lang)) for x in VerificableCredential.objects.filter(
|
2023-11-02 13:17:07 +00:00
|
|
|
user=self.user,
|
|
|
|
status=VerificableCredential.Status.ENABLED
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
did = DID.objects.filter(
|
|
|
|
user=self.user,
|
|
|
|
did=self.data['did']
|
|
|
|
)
|
|
|
|
cred = VerificableCredential.objects.filter(
|
|
|
|
user=self.user,
|
2023-11-02 16:13:49 +00:00
|
|
|
id=self.data['credential'],
|
|
|
|
status=VerificableCredential.Status.ENABLED
|
2023-11-02 13:17:07 +00:00
|
|
|
)
|
|
|
|
if not all([cred.exists(), did.exists()]):
|
|
|
|
return
|
|
|
|
|
2023-12-04 08:51:08 +00:00
|
|
|
did = did[0]
|
2023-11-02 13:17:07 +00:00
|
|
|
cred = cred[0]
|
2023-11-21 09:00:59 +00:00
|
|
|
try:
|
2024-02-20 16:50:45 +00:00
|
|
|
cred.issue(did, domain=self._domain)
|
2024-02-21 13:19:59 +00:00
|
|
|
except Exception as err:
|
2024-02-22 12:47:30 +00:00
|
|
|
logger.error(err)
|
2023-11-21 09:00:59 +00:00
|
|
|
return
|
2023-11-02 13:17:07 +00:00
|
|
|
|
|
|
|
if commit:
|
|
|
|
cred.save()
|
|
|
|
return cred
|
|
|
|
|
|
|
|
return
|
|
|
|
|
2023-11-02 16:13:49 +00:00
|
|
|
|
2023-11-28 08:39:02 +00:00
|
|
|
class DemandAuthorizationForm(forms.Form):
|
2023-12-14 16:59:40 +00:00
|
|
|
organization = forms.ChoiceField(label=_("Organization"), choices=[])
|
2023-11-28 08:39:02 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.user = kwargs.pop('user', None)
|
2024-03-11 12:37:49 +00:00
|
|
|
self.if_credentials = kwargs.pop('if_credentials', None)
|
2023-11-28 08:39:02 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.fields['organization'].choices = [
|
2024-03-04 08:44:53 +00:00
|
|
|
(x.id, x.name) for x in Organization.objects.exclude(
|
|
|
|
domain=settings.DOMAIN
|
|
|
|
)
|
2023-11-28 08:39:02 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
self.org = Organization.objects.filter(
|
|
|
|
id=self.data['organization']
|
|
|
|
)
|
|
|
|
if not self.org.exists():
|
|
|
|
return
|
|
|
|
|
|
|
|
self.org = self.org[0]
|
|
|
|
|
|
|
|
if commit:
|
|
|
|
url = self.org.demand_authorization()
|
2023-11-28 16:33:24 +00:00
|
|
|
if url.status_code == 200:
|
|
|
|
return url.json().get('redirect_uri')
|
2023-11-28 08:39:02 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|