add url public for get a credential with hash

This commit is contained in:
Cayo Puigdefabregas 2024-01-11 12:21:32 +01:00
parent 6505d8e6b3
commit 2df5b42610
10 changed files with 56 additions and 17 deletions

View File

@ -239,7 +239,6 @@ class ImportCertificateForm(forms.Form):
def clean(self):
data = super().clean()
# import pdb; pdb.set_trace()
file_import = data.get('file_import')
self.pfx_file = file_import.read()
self.file_name = file_import.name

View File

@ -1,4 +1,4 @@
# Generated by Django 4.2.5 on 2024-01-10 11:52
# Generated by Django 4.2.5 on 2024-01-11 10:17
from django.conf import settings
from django.db import migrations, models
@ -151,6 +151,8 @@ class Migration(migrations.Migration):
('issued_on', models.DateTimeField(null=True)),
('data', models.TextField()),
('csv_data', models.TextField()),
('public', models.BooleanField(default=True)),
('hash', models.CharField(max_length=260)),
(
'status',
models.PositiveSmallIntegerField(

View File

@ -1,5 +1,6 @@
import json
import pytz
import hashlib
import datetime
from django.db import models
from django.conf import settings
@ -467,6 +468,8 @@ class VerificableCredential(models.Model):
issued_on = models.DateTimeField(null=True)
data = models.TextField()
csv_data = models.TextField()
public = models.BooleanField(default=settings.DEFAULT_PUBLIC_CREDENTIALS)
hash = models.CharField(max_length=260)
status = models.PositiveSmallIntegerField(
choices=Status.choices,
default=Status.ENABLED
@ -520,6 +523,8 @@ class VerificableCredential(models.Model):
self.render(),
self.issuer_did.key_material
)
if self.public:
self.hash = hashlib.sha3_256(self.data.encode()).hexdigest()
def get_context(self):
d = json.loads(self.csv_data)
@ -528,8 +533,12 @@ class VerificableCredential(models.Model):
format = "%Y-%m-%dT%H:%M:%SZ"
issuance_date = self.issued_on.strftime(format)
url_id = "{}/credentials/{}".format(
cred_path = 'credentials'
if self.public:
cred_path = 'public/credentials'
url_id = "{}/{}/{}".format(
settings.DOMAIN.strip("/"),
cred_path,
self.id
)
context = {

View File

@ -37,6 +37,11 @@
</div>
</div>
<div class="row mt-3">
{% if object.public %}
<div class="col text-center">
<a class="btn btn-green-user" href="{% url 'idhub:user_credential_pdf' object.id %}">{% trans 'View in PDF format' %}</a>
</div>
{% endif %}
<div class="col text-center">
<a class="btn btn-green-user" href="{% url 'idhub:user_credential_json' object.id %}">{% trans 'View in JSON format' %}</a>
</div>

View File

@ -80,8 +80,12 @@ urlpatterns = [
name='user_credentials'),
path('user/credentials/<int:pk>', views_user.CredentialView.as_view(),
name='user_credential'),
path('user/credentials/<int:pk>/json', views_user.CredentialJsonView.as_view(),
path('user/credentials/<int:pk>/pdf', views_user.CredentialPdfView.as_view(),
name='user_credential_pdf'),
path('credentials/<int:pk>/', views_user.CredentialJsonView.as_view(),
name='user_credential_json'),
path('public/credentials/<str:pk>/', views_user.PublicCredentialJsonView.as_view(),
name='public_credential_json'),
path('user/credentials/request/',
views_user.CredentialsRequestView.as_view(),
name='user_credentials_request'),

View File

@ -14,6 +14,7 @@ from pyhanko import stamp
from pyhanko.pdf_utils import text
from pyhanko.pdf_utils.incremental_writer import IncrementalPdfFileWriter
from django.utils.translation import gettext_lazy as _
from django.views.generic import View
from django.views.generic.edit import (
UpdateView,
CreateView,
@ -25,6 +26,7 @@ from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse_lazy
from django.http import HttpResponse
from django.contrib import messages
from django.conf import settings
from idhub.user.forms import (
ProfileForm,
RequestCredentialForm,
@ -125,7 +127,7 @@ class CredentialView(MyWallet, TemplateView):
return context
class CredentialJsonView(MyWallet, TemplateView):
class CredentialPdfView(MyWallet, TemplateView):
template_name = "certificates/4_Model_Certificat.html"
subtitle = _('Credential management')
icon = 'bi bi-patch-check-fill'
@ -138,11 +140,19 @@ class CredentialJsonView(MyWallet, TemplateView):
self.object = get_object_or_404(
VerificableCredential,
pk=pk,
public=True,
user=self.request.user
)
self.url_id = "{}/public/credentials/{}".format(
settings.DOMAIN.strip("/"),
self.object.hash
)
data = self.build_certificate()
doc = self.insert_signature(data)
if DID.objects.filter(eidas1=True).exists():
doc = self.insert_signature(data)
else:
doc = data
response = HttpResponse(doc, content_type="application/pdf")
response['Content-Disposition'] = 'attachment; filename={}'.format(self.file_name)
return response
@ -160,9 +170,7 @@ class CredentialJsonView(MyWallet, TemplateView):
with open(img_header, 'rb') as _f:
img_head = base64.b64encode(_f.read()).decode('utf-8')
qr = self.generate_qr_code("http://localhost")
if DID.objects.filter(eidas1=True).exists():
qr = ""
qr = self.generate_qr_code(self.url_id)
first_name = self.user.first_name and self.user.first_name.upper() or ""
last_name = self.user.first_name and self.user.last_name.upper() or ""
@ -231,7 +239,6 @@ class CredentialJsonView(MyWallet, TemplateView):
return s
def insert_signature(self, doc):
# import pdb; pdb.set_trace()
sig = self.signer_init()
if not sig:
return
@ -247,18 +254,17 @@ class CredentialJsonView(MyWallet, TemplateView):
meta = signers.PdfSignatureMetadata(field_name='Signature')
pdf_signer = signers.PdfSigner(
meta, signer=sig, stamp_style=stamp.QRStampStyle(
meta, signer=sig, stamp_style=stamp.TextStampStyle(
stamp_text='Signed by: %(signer)s\nTime: %(ts)s\nURL: %(url)s',
text_box_style=text.TextBoxStyle()
)
)
_bf_out = BytesIO()
url = "https://localhost:8000/"
pdf_signer.sign_pdf(w, output=_bf_out, appearance_text_params={'url': url})
pdf_signer.sign_pdf(w, output=_bf_out, appearance_text_params={'url': self.url_id})
return _bf_out.read()
class CredentialJsonView2(MyWallet, TemplateView):
class CredentialJsonView(MyWallet, TemplateView):
def get(self, request, *args, **kwargs):
pk = kwargs['pk']
@ -272,6 +278,19 @@ class CredentialJsonView2(MyWallet, TemplateView):
return response
class PublicCredentialJsonView(View):
def get(self, request, *args, **kwargs):
pk = kwargs['pk']
self.object = get_object_or_404(
VerificableCredential,
hash=pk,
)
response = HttpResponse(self.object.data, content_type="application/json")
response['Content-Disposition'] = 'attachment; filename={}'.format("credential.json")
return response
class CredentialsRequestView(MyWallet, FormView):
template_name = "idhub/user/credentials_request.html"
subtitle = _('Credential request')

View File

@ -1,4 +1,4 @@
# Generated by Django 4.2.5 on 2024-01-10 11:52
# Generated by Django 4.2.5 on 2024-01-11 10:17
from django.db import migrations, models

View File

@ -1,4 +1,4 @@
# Generated by Django 4.2.5 on 2024-01-10 11:52
# Generated by Django 4.2.5 on 2024-01-11 10:17
from django.conf import settings
from django.db import migrations, models

View File

@ -1,4 +1,4 @@
# Generated by Django 4.2.5 on 2024-01-10 11:52
# Generated by Django 4.2.5 on 2024-01-11 10:17
from django.db import migrations, models
import django.db.models.deletion

View File

@ -222,3 +222,4 @@ LOGGING = {
}
}
DEFAULT_PUBLIC_CREDENTIALS = True