From b7dfb6dcfbad30050f5cfa974a1a99492fe52a78 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 4 Dec 2023 10:56:22 +0100 Subject: [PATCH] send vp_token --- idhub/models.py | 2 +- oidc4vp/forms.py | 40 +++++++++++-------- oidc4vp/models.py | 2 +- .../credentials/verifiable_presentation.json | 2 +- utils/idhub_ssikit/__init__.py | 12 ++++++ 5 files changed, 38 insertions(+), 20 deletions(-) diff --git a/idhub/models.py b/idhub/models.py index b7c26fa..ee7ebed 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -529,7 +529,7 @@ class VerificableCredential(models.Model): context = { 'vc_id': self.id, 'issuer_did': self.issuer_did.did, - 'subject_did': self.subject_did.did, + 'subject_did': self.subject_did and self.subject_did.did or '', 'issuance_date': issuance_date, 'first_name': self.user.first_name, 'last_name': self.user.last_name, diff --git a/oidc4vp/forms.py b/oidc4vp/forms.py index 18ca7ac..7c04255 100644 --- a/oidc4vp/forms.py +++ b/oidc4vp/forms.py @@ -1,9 +1,13 @@ +import json import requests + from django import forms from django.conf import settings from django.template.loader import get_template +from django.utils.translation import gettext_lazy as _ +from django.core.exceptions import ValidationError -from utils.idhub_ssikit import issue_verifiable_presentation +from utils.idhub_ssikit import create_verifiable_presentation from oidc4vp.models import Organization @@ -33,34 +37,36 @@ class AuthorizeForm(forms.Form): ) 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()): + if c.status is not c.Status.ISSUED.value or not c.data: + txt = _('There are some problems with this credentials') + raise ValidationError(txt) + self.list_credentials.append(c) + return data def save(self, commit=True): if not self.list_credentials: 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, - self.list_credentials, - did.key_material, - did.did) + self.get_verificable_presentation() if commit: - return org.send(self.vp) + return self.org.send(self.vp) return + def get_verificable_presentation(self): + did = self.list_credentials[0].subject_did + vp_template = get_template('credentials/verifiable_presentation.json') + vc_list = json.dumps([json.loads(x.data) for x in self.list_credentials]) + + context = { + "holder_did": did.did, + "verifiable_credential_list": vc_list + } + unsigned_vp = vp_template.render(context) + self.vp = create_verifiable_presentation(did.key_material, unsigned_vp) diff --git a/oidc4vp/models.py b/oidc4vp/models.py index 5dae13e..4111c18 100644 --- a/oidc4vp/models.py +++ b/oidc4vp/models.py @@ -71,13 +71,13 @@ class Organization(models.Model): url=self.response_uri.strip("/"), ) auth = (self.my_client_id, self.my_client_secret) + # import pdb; pdb.set_trace() return requests.post(url, data=vp, auth=auth) def demand_authorization(self): """ Send the a request for start a process of Verifier """ - # import pdb; pdb.set_trace() url = "{url}/verify?demand_uri={redirect_uri}".format( url=self.response_uri.strip("/"), redirect_uri=settings.RESPONSE_URI diff --git a/oidc4vp/templates/credentials/verifiable_presentation.json b/oidc4vp/templates/credentials/verifiable_presentation.json index 752affb..a55b769 100644 --- a/oidc4vp/templates/credentials/verifiable_presentation.json +++ b/oidc4vp/templates/credentials/verifiable_presentation.json @@ -7,5 +7,5 @@ "VerifiablePresentation" ], "holder": "{{ holder_did }}", - "verifiableCredential": {{ verifiable_credential_list }} + "verifiableCredential": {{ verifiable_credential_list|safe }} } diff --git a/utils/idhub_ssikit/__init__.py b/utils/idhub_ssikit/__init__.py index 35464b3..cc3e9b4 100644 --- a/utils/idhub_ssikit/__init__.py +++ b/utils/idhub_ssikit/__init__.py @@ -94,6 +94,18 @@ def issue_verifiable_presentation(vp_template: Template, vc_list: list[str], jwk return asyncio.run(inner()) +def create_verifiable_presentation(jwk_holder: str, unsigned_vp: str) -> str: + async def inner(): + signed_vp = await didkit.issue_presentation( + unsigned_vp, + '{"proofFormat": "ldp"}', + jwk_holder + ) + return signed_vp + + return asyncio.run(inner()) + + def verify_presentation(vp): """ Returns a (bool, str) tuple indicating whether the credential is valid.