diff --git a/idhub/models.py b/idhub/models.py index 8d201e5..6c5ed1e 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -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): diff --git a/idhub/templates/credentials/membership-card.json b/idhub/templates/credentials/membership-card.json index e3d1c15..5ecda0a 100644 --- a/idhub/templates/credentials/membership-card.json +++ b/idhub/templates/credentials/membership-card.json @@ -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", diff --git a/idhub/templates/idhub/admin/credentials.html b/idhub/templates/idhub/admin/credentials.html index cab6a97..caee5fc 100644 --- a/idhub/templates/idhub/admin/credentials.html +++ b/idhub/templates/idhub/admin/credentials.html @@ -23,7 +23,7 @@ {% for f in credentials.all %} - {{ f.type }} + {{ f.get_type }} {{ f.description }} {{ f.get_issued_on }} {{ f.get_status }} diff --git a/idhub/templates/idhub/user/credentials.html b/idhub/templates/idhub/user/credentials.html index 6f68e56..f12ae00 100644 --- a/idhub/templates/idhub/user/credentials.html +++ b/idhub/templates/idhub/user/credentials.html @@ -22,7 +22,7 @@ {% for f in credentials.all %} - {{ f.type }} + {{ f.get_type }} {{ f.description }} {{ f.get_issued_on }} {{ f.get_status }} diff --git a/idhub/user/forms.py b/idhub/user/forms.py index 5ac04ad..f9eda6a 100644 --- a/idhub/user/forms.py +++ b/idhub/user/forms.py @@ -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: diff --git a/idhub/user/views.py b/idhub/user/views.py index e6e28dc..2e1258e 100644 --- a/idhub/user/views.py +++ b/idhub/user/views.py @@ -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):