IdHub/oidc4vp/forms.py

64 lines
1.9 KiB
Python
Raw Normal View History

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-11-24 15:36:05 +00:00
2023-12-01 18:31:09 +00:00
from utils.idhub_ssikit import issue_verifiable_presentation
2023-11-29 16:29:31 +00:00
from oidc4vp.models import Organization
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)
self.presentation_definition = kwargs.pop('presentation_definition', [])
2023-12-01 16:10:21 +00:00
reg = r'({})'.format('|'.join(self.presentation_definition))
2023-11-29 16:29:31 +00:00
self.credentials = self.user.vcredentials.filter(
2023-12-01 16:10:21 +00:00
schema__type__iregex=reg
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()
import pdb; pdb.set_trace()
self.list_credentials = []
for c in self.credentials:
if str(c.id) == data.get(c.schema.type.lower()):
self.list_credentials.append(c)
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
did = self.list_credentials[0].subject_did
self.vp = issue_verifiable_presentation(
vp_template: Template,
vc_list: list[str],
jwk_holder: str,
holder_did: str)
self.vp = issue_verifiable_presentation(
vp_template: Template,
self.list_credentials,
did.key_material,
did.did)
2023-12-01 16:10:21 +00:00
2023-12-01 18:31:09 +00:00
if commit:
result = requests.post(self.vp)
return result
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