From 501d2b2894259c67d8d4f3926dc4728dc8c8d2b6 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 4 Dec 2023 09:51:08 +0100 Subject: [PATCH] send a verificable presentation --- idhub/migrations/0001_initial.py | 3 ++- idhub/models.py | 3 ++- idhub/user/forms.py | 2 +- idhub_auth/migrations/0001_initial.py | 2 +- oidc4vp/forms.py | 21 ++++++++++++--------- oidc4vp/models.py | 2 +- oidc4vp/views.py | 12 ++++++++++++ 7 files changed, 31 insertions(+), 14 deletions(-) diff --git a/idhub/migrations/0001_initial.py b/idhub/migrations/0001_initial.py index 0a49195..d841426 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 18:29 +# Generated by Django 4.2.5 on 2023-12-04 08:44 from django.conf import settings from django.db import migrations, models @@ -181,6 +181,7 @@ class Migration(migrations.Migration): ( 'subject_did', models.ForeignKey( + null=True, on_delete=django.db.models.deletion.CASCADE, related_name='subject_credentials', to='idhub.did', diff --git a/idhub/models.py b/idhub/models.py index 915929c..b7c26fa 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -478,6 +478,7 @@ class VerificableCredential(models.Model): DID, on_delete=models.CASCADE, related_name='subject_credentials', + null=True ) issuer_did = models.ForeignKey( DID, @@ -528,7 +529,7 @@ class VerificableCredential(models.Model): context = { 'vc_id': self.id, 'issuer_did': self.issuer_did.did, - 'subject_did': self.subject_did, + 'subject_did': self.subject_did.did, 'issuance_date': issuance_date, 'first_name': self.user.first_name, 'last_name': self.user.last_name, diff --git a/idhub/user/forms.py b/idhub/user/forms.py index 16151bb..5b6f9ab 100644 --- a/idhub/user/forms.py +++ b/idhub/user/forms.py @@ -44,7 +44,7 @@ class RequestCredentialForm(forms.Form): if not all([cred.exists(), did.exists()]): return - did = did[0].did + did = did[0] cred = cred[0] try: cred.issue(did) diff --git a/idhub_auth/migrations/0001_initial.py b/idhub_auth/migrations/0001_initial.py index 741a8b9..3ee6d7a 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 18:29 +# Generated by Django 4.2.5 on 2023-12-04 08:44 from django.db import migrations, models diff --git a/oidc4vp/forms.py b/oidc4vp/forms.py index 3d4b16f..18ca7ac 100644 --- a/oidc4vp/forms.py +++ b/oidc4vp/forms.py @@ -1,5 +1,7 @@ +import requests from django import forms from django.conf import settings +from django.template.loader import get_template from utils.idhub_ssikit import issue_verifiable_presentation from oidc4vp.models import Organization @@ -10,6 +12,7 @@ class AuthorizeForm(forms.Form): def __init__(self, *args, **kwargs): self.data = kwargs.get('data', {}).copy() self.user = kwargs.pop('user', None) + self.org = kwargs.pop('org', None) self.presentation_definition = kwargs.pop('presentation_definition', []) reg = r'({})'.format('|'.join(self.presentation_definition)) @@ -42,22 +45,22 @@ class AuthorizeForm(forms.Form): return did = self.list_credentials[0].subject_did + vp_template = get_template('credentials/verifiable_presentation.json') + + # 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, - vc_list: list[str], - jwk_holder: str, - holder_did: str) - - self.vp = issue_verifiable_presentation( - vp_template: Template, + vp_template, self.list_credentials, did.key_material, did.did) if commit: - result = requests.post(self.vp) - return result + return org.send(self.vp) return diff --git a/oidc4vp/models.py b/oidc4vp/models.py index ee4e44a..5dae13e 100644 --- a/oidc4vp/models.py +++ b/oidc4vp/models.py @@ -70,7 +70,7 @@ class Organization(models.Model): url = "{url}/verify".format( url=self.response_uri.strip("/"), ) - auth = (self.my_client_id, self.client_secret) + auth = (self.my_client_id, self.my_client_secret) return requests.post(url, data=vp, auth=auth) def demand_authorization(self): diff --git a/oidc4vp/views.py b/oidc4vp/views.py index 48ea8b7..5a28741 100644 --- a/oidc4vp/views.py +++ b/oidc4vp/views.py @@ -38,6 +38,7 @@ class AuthorizeView(UserView, FormView): vps = self.request.GET.get('presentation_definition') # import pdb; pdb.set_trace() kwargs['presentation_definition'] = json.loads(vps) + kwargs["org"] = self.get_org() return kwargs def form_valid(self, form): @@ -48,6 +49,17 @@ class AuthorizeView(UserView, FormView): messages.error(self.request, _("Error sending credential!")) return super().form_valid(form) + def get_org(self): + client_id = self.request.GET.get("client_id") + if not client_id: + raise Http404("Organization not found!") + + org = get_object_or_404( + Organization, + client_id=client_id, + ) + return org + class VerifyView(View): def get(self, request, *args, **kwargs):