send a verificable presentation

This commit is contained in:
Cayo Puigdefabregas 2023-12-04 09:51:08 +01:00
parent 37908ba1e7
commit 501d2b2894
7 changed files with 31 additions and 14 deletions

View File

@ -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.conf import settings
from django.db import migrations, models from django.db import migrations, models
@ -181,6 +181,7 @@ class Migration(migrations.Migration):
( (
'subject_did', 'subject_did',
models.ForeignKey( models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE, on_delete=django.db.models.deletion.CASCADE,
related_name='subject_credentials', related_name='subject_credentials',
to='idhub.did', to='idhub.did',

View File

@ -478,6 +478,7 @@ class VerificableCredential(models.Model):
DID, DID,
on_delete=models.CASCADE, on_delete=models.CASCADE,
related_name='subject_credentials', related_name='subject_credentials',
null=True
) )
issuer_did = models.ForeignKey( issuer_did = models.ForeignKey(
DID, DID,
@ -528,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, 'subject_did': self.subject_did.did,
'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

@ -44,7 +44,7 @@ class RequestCredentialForm(forms.Form):
if not all([cred.exists(), did.exists()]): if not all([cred.exists(), did.exists()]):
return return
did = did[0].did did = did[0]
cred = cred[0] cred = cred[0]
try: try:
cred.issue(did) cred.issue(did)

View File

@ -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 from django.db import migrations, models

View File

@ -1,5 +1,7 @@
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 utils.idhub_ssikit import issue_verifiable_presentation from utils.idhub_ssikit import issue_verifiable_presentation
from oidc4vp.models import Organization from oidc4vp.models import Organization
@ -10,6 +12,7 @@ class AuthorizeForm(forms.Form):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.data = kwargs.get('data', {}).copy() self.data = kwargs.get('data', {}).copy()
self.user = kwargs.pop('user', None) self.user = kwargs.pop('user', None)
self.org = kwargs.pop('org', None)
self.presentation_definition = kwargs.pop('presentation_definition', []) self.presentation_definition = kwargs.pop('presentation_definition', [])
reg = r'({})'.format('|'.join(self.presentation_definition)) reg = r'({})'.format('|'.join(self.presentation_definition))
@ -42,22 +45,22 @@ class AuthorizeForm(forms.Form):
return return
did = self.list_credentials[0].subject_did 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( self.vp = issue_verifiable_presentation(
vp_template: Template, vp_template,
vc_list: list[str],
jwk_holder: str,
holder_did: str)
self.vp = issue_verifiable_presentation(
vp_template: Template,
self.list_credentials, self.list_credentials,
did.key_material, did.key_material,
did.did) did.did)
if commit: if commit:
result = requests.post(self.vp) return org.send(self.vp)
return result
return return

View File

@ -70,7 +70,7 @@ class Organization(models.Model):
url = "{url}/verify".format( url = "{url}/verify".format(
url=self.response_uri.strip("/"), 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) return requests.post(url, data=vp, auth=auth)
def demand_authorization(self): def demand_authorization(self):

View File

@ -38,6 +38,7 @@ class AuthorizeView(UserView, FormView):
vps = self.request.GET.get('presentation_definition') vps = self.request.GET.get('presentation_definition')
# import pdb; pdb.set_trace() # import pdb; pdb.set_trace()
kwargs['presentation_definition'] = json.loads(vps) kwargs['presentation_definition'] = json.loads(vps)
kwargs["org"] = self.get_org()
return kwargs return kwargs
def form_valid(self, form): def form_valid(self, form):
@ -48,6 +49,17 @@ class AuthorizeView(UserView, FormView):
messages.error(self.request, _("Error sending credential!")) messages.error(self.request, _("Error sending credential!"))
return super().form_valid(form) 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): class VerifyView(View):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):