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

View File

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

View File

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

View File

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

View File

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

View File

@ -135,6 +135,9 @@ class CredentialsRequestView(MyWallet, FormView):
def get_form_kwargs(self): def get_form_kwargs(self):
kwargs = super().get_form_kwargs() kwargs = super().get_form_kwargs()
kwargs['user'] = self.request.user 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 return kwargs
def form_valid(self, form): def form_valid(self, form):