add new credentials

This commit is contained in:
Cayo Puigdefabregas 2024-01-15 19:11:22 +01:00
parent 01d9d0d189
commit 9d3b33db28
6 changed files with 61 additions and 36 deletions

View File

@ -11,6 +11,7 @@ from utils.idhub_ssikit import (
generate_did_controller_key,
keydid_from_controller_key,
sign_credential,
verify_credential
)
from idhub_auth.models import User
@ -509,10 +510,23 @@ class VerificableCredential(models.Model):
def description(self):
for des in json.loads(self.render()).get('description', []):
if settings.LANGUAGE_CODE == des.get('lang'):
if settings.LANGUAGE_CODE in des.get('lang'):
return des.get('value', '')
return ''
def get_type(self, lang=None):
schema = json.loads(self.schema.data)
if not schema.get('name'):
return ''
try:
for x in schema['name']:
if lang or settings.LANGUAGE_CODE in x['lang']:
return x.get('value', '')
except:
return self.schema.type
return ''
def get_status(self):
return self.Status(self.status).label
@ -524,16 +538,16 @@ class VerificableCredential(models.Model):
if self.status == self.Status.ISSUED:
return
self.status = self.Status.ISSUED
# self.status = self.Status.ISSUED
self.subject_did = did
self.issued_on = datetime.datetime.now().astimezone(pytz.utc)
data = sign_credential(
self.render(),
d_ordered = ujson.loads(self.render())
d_minimum = self.filter_dict(d_ordered)
data = ujson.dumps(d_minimum)
self.data = sign_credential(
data,
self.issuer_did.key_material
)
d_ordered = ujson.loads(data)
d_minimum = self.filter_dict(d_ordered)
self.data = ujson.dumps(d_minimum)
def get_context(self):
d = json.loads(self.csv_data)
@ -542,20 +556,25 @@ class VerificableCredential(models.Model):
format = "%Y-%m-%dT%H:%M:%SZ"
issuance_date = self.issued_on.strftime(format)
url_id = "{}/credentials/{}".format(
settings.DOMAIN.strip("/"),
self.id
)
try:
domain = self._domain
except:
domain = settings.DOMAIN.strip("/")
url_id = "{}/credentials/{}".format(domain, self.id)
context = {
'vc_id': url_id,
'issuer_did': self.issuer_did.did,
'subject_did': self.subject_did and self.subject_did.did or '',
'issuance_date': issuance_date,
'first_name': self.user.first_name,
'last_name': self.user.last_name,
'email': self.user.email
'firstName': self.user.first_name or "",
'lastName': self.user.last_name or "",
'email': self.user.email,
'organisation': settings.ORGANIZATION or '',
}
context.update(d)
context['firstName'] = ""
return context
def render(self):

View File

@ -9,15 +9,15 @@
"VerifiableAttestation",
"MembershipCard"
],
"id": "[[PLACEHOLDER]]",
"id": "{{ vc_id }}",
"issuer": {
"id": "[[PLACEHOLDER]]",
"name": "[[PLACEHOLDER]]"
"id": "{{ issuer_did }}",
"name": "{{ organisation }}"
},
"issuanceDate": "[[PLACEHOLDER]]",
"issued": "[[PLACEHOLDER]]",
"validFrom": "[[PLACEHOLDER]]",
"validUntil": "[[PLACEHOLDER]]",
"issuanceDate": "{{ issuance_date }}",
"issued": "{{ issuance_date }}",
"validFrom": "{{ issuance_date }}",
"validUntil": "{{ validUntil }}",
"name": [
{
"value": "Membership Card",
@ -47,18 +47,18 @@
}
],
"credentialSubject": {
"id": "[[PLACEHOLDER]]",
"firstName": "[[PLACEHOLDER]]",
"lastName": "[[PLACEHOLDER]]",
"email": "[[PLACEHOLDER]]",
"typeOfPerson": "[[PLACEHOLDER]]",
"identityDocType": "[[PLACEHOLDER]]",
"identityNumber": "[[PLACEHOLDER]]",
"organisation": "[[PLACEHOLDER]]",
"membershipType": "[[PLACEHOLDER]]",
"membershipId": "[[PLACEHOLDER]]",
"affiliatedSince": "[[PLACEHOLDER]]",
"affiliatedUntil": "[[PLACEHOLDER]]"
"id": "{{ subject_did }}",
"firstName": "{{ firstName }}",
"lastName": "{{ lastName }}",
"email": "{{ email }}",
"typeOfPerson": "{{ typeOfPerson }}",
"identityDocType": "{{ identityDocType }}",
"identityNumber": "{{ identityNumber }}",
"organisation": "{{ organisation }}",
"membershipType": "{{ membershipType }}",
"membershipId": "{{ vc_id }}",
"affiliatedSince": "{{ affiliatedSince }}",
"affiliatedUntil": "{{ affiliatedUntil }}"
},
"credentialSchema": {
"id": "https://idhub.pangea.org/vc_schemas/membership-card.json",

View File

@ -23,7 +23,7 @@
<tbody>
{% for f in credentials.all %}
<tr style="font-size:15px;">
<td>{{ f.type }}</td>
<td>{{ f.get_type }}</td>
<td>{{ f.description }}</td>
<td>{{ f.get_issued_on }}</td>
<td class="text-center">{{ f.get_status }}</td>

View File

@ -22,7 +22,7 @@
<tbody>
{% for f in credentials.all %}
<tr style="font-size:15px;">
<td>{{ f.type }}</td>
<td>{{ f.get_type }}</td>
<td>{{ f.description }}</td>
<td>{{ f.get_issued_on }}</td>
<td class="text-center">{{ f.get_status }}</td>

View File

@ -22,12 +22,14 @@ class RequestCredentialForm(forms.Form):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
self.lang = kwargs.pop('lang', None)
self._domain = kwargs.pop('domain', None)
super().__init__(*args, **kwargs)
self.fields['did'].choices = [
(x.did, x.label) for x in DID.objects.filter(user=self.user)
]
self.fields['credential'].choices = [
(x.id, x.type()) for x in VerificableCredential.objects.filter(
(x.id, x.get_type(lang=self.lang)) for x in VerificableCredential.objects.filter(
user=self.user,
status=VerificableCredential.Status.ENABLED
)
@ -48,6 +50,7 @@ class RequestCredentialForm(forms.Form):
did = did[0]
cred = cred[0]
cred._domain = self._domain
try:
cred.issue(did)
except Exception:

View File

@ -135,6 +135,9 @@ class CredentialsRequestView(MyWallet, FormView):
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['user'] = self.request.user
kwargs['lang'] = self.request.LANGUAGE_CODE
domain = "{}://{}".format(self.request.scheme, self.request.get_host())
kwargs['domain'] = domain
return kwargs
def form_valid(self, form):