diff --git a/idhub/migrations/0001_initial.py b/idhub/migrations/0001_initial.py index 4fbf53d..0a49195 100644 --- a/idhub/migrations/0001_initial.py +++ b/idhub/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.5 on 2023-12-01 17:19 +# Generated by Django 4.2.5 on 2023-12-01 18:29 from django.conf import settings from django.db import migrations, models @@ -148,7 +148,6 @@ class Migration(migrations.Migration): ('verified', models.BooleanField()), ('created_on', models.DateTimeField(auto_now=True)), ('issued_on', models.DateTimeField(null=True)), - ('subject_did', models.CharField(max_length=250)), ('data', models.TextField()), ('csv_data', models.TextField()), ( @@ -179,6 +178,14 @@ class Migration(migrations.Migration): to='idhub.schemas', ), ), + ( + 'subject_did', + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name='subject_credentials', + to='idhub.did', + ), + ), ( 'user', models.ForeignKey( diff --git a/idhub/models.py b/idhub/models.py index 262f186..915929c 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -463,7 +463,6 @@ class VerificableCredential(models.Model): verified = models.BooleanField() created_on = models.DateTimeField(auto_now=True) issued_on = models.DateTimeField(null=True) - subject_did = models.CharField(max_length=250) data = models.TextField() csv_data = models.TextField() status = models.PositiveSmallIntegerField( @@ -475,6 +474,11 @@ class VerificableCredential(models.Model): on_delete=models.CASCADE, related_name='vcredentials', ) + subject_did = models.ForeignKey( + DID, + on_delete=models.CASCADE, + related_name='subject_credentials', + ) issuer_did = models.ForeignKey( DID, on_delete=models.CASCADE, diff --git a/idhub_auth/migrations/0001_initial.py b/idhub_auth/migrations/0001_initial.py index 9091472..741a8b9 100644 --- a/idhub_auth/migrations/0001_initial.py +++ b/idhub_auth/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.5 on 2023-12-01 17:19 +# Generated by Django 4.2.5 on 2023-12-01 18:29 from django.db import migrations, models diff --git a/oidc4vp/forms.py b/oidc4vp/forms.py index 9a8633e..3d4b16f 100644 --- a/oidc4vp/forms.py +++ b/oidc4vp/forms.py @@ -1,54 +1,13 @@ from django import forms from django.conf import settings +from utils.idhub_ssikit import issue_verifiable_presentation from oidc4vp.models import Organization -# class OrganizationForm(forms.Form): -# wallet = forms.ChoiceField( -# "Wallet", -# choices=[(x.id, x.name) for x in Organization.objects.all()] -# ) - -# def clean_wallet(self): -# data = self.cleaned_data["wallet"] -# organization = Organization.objects.filter( -# id=data -# ) - -# if not organization.exists(): -# raise ValidationError("organization is not valid!") - -# self.organization = organization.first() - -# return data - -# def authorize(self): -# data = { -# "response_type": "vp_token", -# "response_mode": "direct_post", -# "client_id": self.organization.client_id, -# "response_uri": settings.RESPONSE_URI, -# "presentation_definition": self.pv_definition(), -# "nonce": "" -# } -# query_dict = QueryDict('', mutable=True) -# query_dict.update(data) - -# url = '{response_uri}/authorize?{params}'.format( -# response_uri=self.organization.response_uri, -# params=query_dict.urlencode() -# ) - -# def pv_definition(self): -# return "" - - class AuthorizeForm(forms.Form): - # organization = forms.ChoiceField(choices=[]) def __init__(self, *args, **kwargs): - # import pdb; pdb.set_trace() self.data = kwargs.get('data', {}).copy() self.user = kwargs.pop('user', None) self.presentation_definition = kwargs.pop('presentation_definition', []) @@ -69,20 +28,36 @@ class AuthorizeForm(forms.Form): widget=forms.RadioSelect, choices=choices ) + 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 def save(self, commit=True): - # self.org = Organization.objects.filter( - # id=self.data['organization'] - # ) - # if not self.org.exists(): - # return + if not self.list_credentials: + return - # self.org = self.org[0] + did = self.list_credentials[0].subject_did - # if commit: - # url = self.org.demand_authorization() - # if url.status_code == 200: - # return url.json().get('redirect_uri') - - return + 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) + + if commit: + result = requests.post(self.vp) + return result + + return diff --git a/oidc4vp/templates/credentials_presentation.html b/oidc4vp/templates/credentials_presentation.html new file mode 100644 index 0000000..63f54ba --- /dev/null +++ b/oidc4vp/templates/credentials_presentation.html @@ -0,0 +1,96 @@ +{% extends "idhub/base.html" %} +{% load i18n %} + +{% block content %} +