From fd8f404908324f59c0efd983daef3a3699504331 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 20 Jun 2024 12:18:53 +0200 Subject: [PATCH] send credential as issuer to TA --- idhub/models.py | 46 +++++++++++++++++++ .../templates/credentials/ereuse-issuer.json | 2 +- trustchain_idhub/settings.py | 2 + 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/idhub/models.py b/idhub/models.py index c0a0304..29dc75b 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -3,6 +3,7 @@ import ujson import pytz import hashlib import datetime +import requests from collections import OrderedDict from django.db import models from django.conf import settings @@ -515,6 +516,51 @@ class DID(models.Model): user.set_encrypted_sensitive_data() user.save() return user.encrypt_data(value) + + def send_credential_as_issuer_to_TA(self): + url = settings.VERIFIABLE_REGISTER_URL + token = settings.TOKEN_TA_API + if not url or not token: + return + + headers = {"Bearer {}".format(token)} + + credential = self._render_credential_issuer() + response = requests.post(url=url, data=credential, headers=headers) + if response.status_code >= 300: + return + + self.credential_as_issuer = response.text + + def get_context(self): + format = "%Y-%m-%dT%H:%M:%SZ" + issuance_date = datetime.datetime.now().strftime(format) + credential_status_id = 'https://revocation.not.supported/' + org = Organization.objects.get(main=True) + allow_schemas = [x.url for x in Schemas.objects.all()] + context = { + "vc_id": "", + "id_credential": "", + "issuer_did": "", + "organization": "", + "validUntil": "", + "issuance_date": issuance_date, + "subject_did": self.did, + "legalName": org.name or "", + "allowedSchemas": allow_schemas, + "email": self.user.email, + "credential_status_id": credential_status_id, + } + return context + + def _render_credential_issuer(self): + context = self.get_context() + template_name = "credentials/ereuse-issuer.json" + tmpl = get_template(template_name) + credential = ujson.loads(tmpl.render(context)) + credential.pop("credentialStatus", None) + + return ujson.dumps(credential) diff --git a/idhub/templates/credentials/ereuse-issuer.json b/idhub/templates/credentials/ereuse-issuer.json index c0665bc..2d5e221 100644 --- a/idhub/templates/credentials/ereuse-issuer.json +++ b/idhub/templates/credentials/ereuse-issuer.json @@ -52,7 +52,7 @@ "email": "{{ email }}" }, "credentialStatus": { - "id": "{{ credential_status_id}}", + "id": "{{ credential_status_id }}", "type": "RevocationBitmap2022", "revocationBitmapIndex": "{{ id_credential }}" }, diff --git a/trustchain_idhub/settings.py b/trustchain_idhub/settings.py index 473fa24..8951ba7 100644 --- a/trustchain_idhub/settings.py +++ b/trustchain_idhub/settings.py @@ -242,3 +242,5 @@ CREATE_TEST_USERS = config('CREATE_TEST_USERS', default=False, cast=bool) ENABLE_2FACTOR_AUTH = config('ENABLE_2FACTOR_AUTH', default=True, cast=bool) COMMIT = config('COMMIT', default='') +VERIFIABLE_REGISTER_URL = config('VERIFIABLE_REGISTER_URL', default='') +TOKEN_TA_API = config('TOKEN_TA_API', default='')