2023-12-04 09:56:22 +00:00
|
|
|
import json
|
2023-12-04 08:51:08 +00:00
|
|
|
import requests
|
2023-12-04 09:56:22 +00:00
|
|
|
|
2023-11-24 15:36:05 +00:00
|
|
|
from django import forms
|
2023-11-29 16:29:31 +00:00
|
|
|
from django.conf import settings
|
2023-12-04 08:51:08 +00:00
|
|
|
from django.template.loader import get_template
|
2023-12-04 09:56:22 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from django.core.exceptions import ValidationError
|
2023-11-24 15:36:05 +00:00
|
|
|
|
2023-12-04 09:56:22 +00:00
|
|
|
from utils.idhub_ssikit import create_verifiable_presentation
|
2023-11-29 16:29:31 +00:00
|
|
|
from oidc4vp.models import Organization
|
2023-12-07 17:10:04 +00:00
|
|
|
from idhub.models import VerificableCredential
|
2023-11-24 15:36:05 +00:00
|
|
|
|
|
|
|
|
2023-11-29 16:29:31 +00:00
|
|
|
class AuthorizeForm(forms.Form):
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2023-12-01 16:10:21 +00:00
|
|
|
self.data = kwargs.get('data', {}).copy()
|
2023-11-29 16:29:31 +00:00
|
|
|
self.user = kwargs.pop('user', None)
|
2023-12-04 08:51:08 +00:00
|
|
|
self.org = kwargs.pop('org', None)
|
2023-12-07 17:10:04 +00:00
|
|
|
self.code = kwargs.pop('code', None)
|
2024-01-21 12:45:08 +00:00
|
|
|
self.pw = kwargs.pop('pw', None)
|
2023-11-29 16:29:31 +00:00
|
|
|
self.presentation_definition = kwargs.pop('presentation_definition', [])
|
2024-01-21 12:45:08 +00:00
|
|
|
self.subject_did = None
|
2023-12-01 16:10:21 +00:00
|
|
|
|
|
|
|
reg = r'({})'.format('|'.join(self.presentation_definition))
|
|
|
|
|
2023-12-13 16:52:18 +00:00
|
|
|
self.all_credentials = self.user.vcredentials.filter(
|
2023-12-07 17:10:04 +00:00
|
|
|
schema__type__iregex=reg,
|
2023-12-13 16:52:18 +00:00
|
|
|
)
|
|
|
|
self.credentials = self.all_credentials.filter(
|
2023-12-07 17:10:04 +00:00
|
|
|
status=VerificableCredential.Status.ISSUED.value
|
2023-11-29 16:29:31 +00:00
|
|
|
)
|
|
|
|
super().__init__(*args, **kwargs)
|
2023-12-01 16:10:21 +00:00
|
|
|
for vp in self.presentation_definition:
|
|
|
|
vp = vp.lower()
|
|
|
|
choices = [
|
|
|
|
(str(x.id), x.schema.type.lower()) for x in self.credentials.filter(
|
|
|
|
schema__type__iexact=vp)
|
|
|
|
]
|
|
|
|
self.fields[vp.lower()] = forms.ChoiceField(
|
|
|
|
widget=forms.RadioSelect,
|
|
|
|
choices=choices
|
|
|
|
)
|
2023-12-01 18:31:09 +00:00
|
|
|
def clean(self):
|
|
|
|
data = super().clean()
|
|
|
|
self.list_credentials = []
|
|
|
|
for c in self.credentials:
|
|
|
|
if str(c.id) == data.get(c.schema.type.lower()):
|
2023-12-04 09:56:22 +00:00
|
|
|
if c.status is not c.Status.ISSUED.value or not c.data:
|
|
|
|
txt = _('There are some problems with this credentials')
|
|
|
|
raise ValidationError(txt)
|
|
|
|
|
2024-01-21 12:45:08 +00:00
|
|
|
cred = self.user.decrypt_data(
|
|
|
|
c.data,
|
|
|
|
self.pw
|
|
|
|
)
|
|
|
|
self.subject_did = c.subject_did
|
|
|
|
self.list_credentials.append(cred)
|
2023-12-04 09:56:22 +00:00
|
|
|
|
2023-12-07 17:10:04 +00:00
|
|
|
if not self.code:
|
|
|
|
txt = _("There isn't code in request")
|
|
|
|
raise ValidationError(txt)
|
|
|
|
|
2023-12-01 18:31:09 +00:00
|
|
|
return data
|
2023-11-29 16:29:31 +00:00
|
|
|
|
|
|
|
def save(self, commit=True):
|
2023-12-01 18:31:09 +00:00
|
|
|
if not self.list_credentials:
|
|
|
|
return
|
|
|
|
|
2023-12-04 09:56:22 +00:00
|
|
|
self.get_verificable_presentation()
|
2023-12-01 16:10:21 +00:00
|
|
|
|
2023-12-01 18:31:09 +00:00
|
|
|
if commit:
|
2023-12-07 17:10:04 +00:00
|
|
|
return self.org.send(self.vp, self.code)
|
2023-12-01 16:10:21 +00:00
|
|
|
|
2023-12-01 18:31:09 +00:00
|
|
|
return
|
2023-11-24 15:36:05 +00:00
|
|
|
|
2023-12-04 09:56:22 +00:00
|
|
|
def get_verificable_presentation(self):
|
2024-01-21 12:45:08 +00:00
|
|
|
did = self.subject_did
|
2023-12-04 09:56:22 +00:00
|
|
|
vp_template = get_template('credentials/verifiable_presentation.json')
|
2024-01-21 12:45:08 +00:00
|
|
|
vc_list = json.dumps([json.loads(x) for x in self.list_credentials])
|
2023-12-04 09:56:22 +00:00
|
|
|
|
|
|
|
context = {
|
|
|
|
"holder_did": did.did,
|
|
|
|
"verifiable_credential_list": vc_list
|
|
|
|
}
|
|
|
|
unsigned_vp = vp_template.render(context)
|
2024-01-21 12:45:08 +00:00
|
|
|
key_material = did.get_key_material(self.pw)
|
|
|
|
self.vp = create_verifiable_presentation(key_material, unsigned_vp)
|