send credential as issuer to TA

This commit is contained in:
Cayo Puigdefabregas 2024-06-20 12:18:53 +02:00
parent a975d831f9
commit fd8f404908
3 changed files with 49 additions and 1 deletions

View File

@ -3,6 +3,7 @@ import ujson
import pytz import pytz
import hashlib import hashlib
import datetime import datetime
import requests
from collections import OrderedDict from collections import OrderedDict
from django.db import models from django.db import models
from django.conf import settings from django.conf import settings
@ -515,6 +516,51 @@ class DID(models.Model):
user.set_encrypted_sensitive_data() user.set_encrypted_sensitive_data()
user.save() user.save()
return user.encrypt_data(value) 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)

View File

@ -52,7 +52,7 @@
"email": "{{ email }}" "email": "{{ email }}"
}, },
"credentialStatus": { "credentialStatus": {
"id": "{{ credential_status_id}}", "id": "{{ credential_status_id }}",
"type": "RevocationBitmap2022", "type": "RevocationBitmap2022",
"revocationBitmapIndex": "{{ id_credential }}" "revocationBitmapIndex": "{{ id_credential }}"
}, },

View File

@ -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) ENABLE_2FACTOR_AUTH = config('ENABLE_2FACTOR_AUTH', default=True, cast=bool)
COMMIT = config('COMMIT', default='') COMMIT = config('COMMIT', default='')
VERIFIABLE_REGISTER_URL = config('VERIFIABLE_REGISTER_URL', default='')
TOKEN_TA_API = config('TOKEN_TA_API', default='')