send vp_token

This commit is contained in:
Cayo Puigdefabregas 2023-12-04 10:56:22 +01:00
parent 501d2b2894
commit b7dfb6dcfb
5 changed files with 38 additions and 20 deletions

View File

@ -529,7 +529,7 @@ class VerificableCredential(models.Model):
context = { context = {
'vc_id': self.id, 'vc_id': self.id,
'issuer_did': self.issuer_did.did, '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, 'issuance_date': issuance_date,
'first_name': self.user.first_name, 'first_name': self.user.first_name,
'last_name': self.user.last_name, 'last_name': self.user.last_name,

View File

@ -1,9 +1,13 @@
import json
import requests import requests
from django import forms from django import forms
from django.conf import settings from django.conf import settings
from django.template.loader import get_template 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 from oidc4vp.models import Organization
@ -33,34 +37,36 @@ class AuthorizeForm(forms.Form):
) )
def clean(self): def clean(self):
data = super().clean() data = super().clean()
import pdb; pdb.set_trace()
self.list_credentials = [] self.list_credentials = []
for c in self.credentials: for c in self.credentials:
if str(c.id) == data.get(c.schema.type.lower()): 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) self.list_credentials.append(c)
return data return data
def save(self, commit=True): def save(self, commit=True):
if not self.list_credentials: if not self.list_credentials:
return return
did = self.list_credentials[0].subject_did self.get_verificable_presentation()
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)
if commit: if commit:
return org.send(self.vp) return self.org.send(self.vp)
return 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)

View File

@ -71,13 +71,13 @@ class Organization(models.Model):
url=self.response_uri.strip("/"), url=self.response_uri.strip("/"),
) )
auth = (self.my_client_id, self.my_client_secret) auth = (self.my_client_id, self.my_client_secret)
# import pdb; pdb.set_trace()
return requests.post(url, data=vp, auth=auth) return requests.post(url, data=vp, auth=auth)
def demand_authorization(self): def demand_authorization(self):
""" """
Send the a request for start a process of Verifier Send the a request for start a process of Verifier
""" """
# import pdb; pdb.set_trace()
url = "{url}/verify?demand_uri={redirect_uri}".format( url = "{url}/verify?demand_uri={redirect_uri}".format(
url=self.response_uri.strip("/"), url=self.response_uri.strip("/"),
redirect_uri=settings.RESPONSE_URI redirect_uri=settings.RESPONSE_URI

View File

@ -7,5 +7,5 @@
"VerifiablePresentation" "VerifiablePresentation"
], ],
"holder": "{{ holder_did }}", "holder": "{{ holder_did }}",
"verifiableCredential": {{ verifiable_credential_list }} "verifiableCredential": {{ verifiable_credential_list|safe }}
} }

View File

@ -94,6 +94,18 @@ def issue_verifiable_presentation(vp_template: Template, vc_list: list[str], jwk
return asyncio.run(inner()) 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): def verify_presentation(vp):
""" """
Returns a (bool, str) tuple indicating whether the credential is valid. Returns a (bool, str) tuple indicating whether the credential is valid.