fix sign snapshots

This commit is contained in:
Cayo Puigdefabregas 2024-12-05 19:25:22 +01:00
parent 3a7eb0f97e
commit 4c07920a6a
3 changed files with 12 additions and 11 deletions

View file

@ -680,7 +680,7 @@ class VerificableCredential(models.Model):
credential_subject = ujson.loads(data).get("credentialSubject", {}) credential_subject = ujson.loads(data).get("credentialSubject", {})
return credential_subject.items() return credential_subject.items()
def issue(self, did, domain, encrypt=True): def issue(self, did, domain, save=True):
if self.status == self.Status.ISSUED: if self.status == self.Status.ISSUED:
return return
@ -700,7 +700,7 @@ class VerificableCredential(models.Model):
if not valid: if not valid:
return return
if not encrypt: if not save:
return vc_str return vc_str
self.data = self.user.encrypt_data(vc_str) self.data = self.user.encrypt_data(vc_str)

View file

@ -6,7 +6,7 @@
"issuer": "{{ issuer_did }}", "issuer": "{{ issuer_did }}",
"issuanceDate": "{{ issuance_date }}", "issuanceDate": "{{ issuance_date }}",
"credentialSubject": { "credentialSubject": {
"operatorId": "123456789011121314", "operatorId": "{{ operator_id }}",
"uuid": "{{ uuid }}", "uuid": "{{ uuid }}",
"type": "hardwareList", "type": "hardwareList",
"software": "workbench-script", "software": "workbench-script",
@ -44,13 +44,13 @@
{ {
"type": "HardwareList", "type": "HardwareList",
"operation": "smartctl", "operation": "smartctl",
"output": "{{ smartctl }}", "output": {{ smartctl|default:'""'|safe }},
"timestamp": "{{ issuance_date }}" "timestamp": "{{ issuance_date }}"
}, },
{ {
"type": "HardwareList", "type": "HardwareList",
"operation": "inxi", "operation": "inxi",
"output": "{{ inxi }}", "output": {{ inxi|default:'""'|safe }},
"timestamp": "{{ issuance_date }}" "timestamp": "{{ issuance_date }}"
} }
], ],

View file

@ -24,7 +24,7 @@ def webhook_verify(request):
if not auth_header or not auth_header.startswith('Bearer '): if not auth_header or not auth_header.startswith('Bearer '):
return JsonResponse({'error': 'Invalid or missing token'}, status=401) return JsonResponse({'error': 'Invalid or missing token'}, status=401)
token = auth_header.split(' ')[1] token = auth_header.split(' ')[1].strip("'").strip('"')
tk = Token.objects.filter(token=token).first() tk = Token.objects.filter(token=token).first()
if not tk: if not tk:
return JsonResponse({'error': 'Invalid or missing token'}, status=401) return JsonResponse({'error': 'Invalid or missing token'}, status=401)
@ -60,7 +60,7 @@ def webhook_issue(request):
if not auth_header or not auth_header.startswith('Bearer '): if not auth_header or not auth_header.startswith('Bearer '):
return JsonResponse({'error': 'Invalid or missing token'}, status=401) return JsonResponse({'error': 'Invalid or missing token'}, status=401)
token = auth_header.split(' ')[1] token = auth_header.split(' ')[1].strip("'").strip('"')
tk = Token.objects.filter(token=token).first() tk = Token.objects.filter(token=token).first()
if not tk: if not tk:
return JsonResponse({'error': 'Invalid or missing token'}, status=401) return JsonResponse({'error': 'Invalid or missing token'}, status=401)
@ -72,23 +72,24 @@ def webhook_issue(request):
typ = data.get("type") typ = data.get("type")
vc = data.get("data") vc = data.get("data")
save = data.get("save", True)
try: try:
vc = json.dumps(vc) vc = json.dumps(vc)
except Exception: except Exception:
return JsonResponse({'error': 'Invalid JSON'}, status=400) return JsonResponse({'error': 'Invalid JSON'}, status=400)
user = User.objects.filter(email=data.get("user")).first() if not typ or not vc:
if not typ or not vc or not user:
return JsonResponse({'error': 'Invalid JSON'}, status=400) return JsonResponse({'error': 'Invalid JSON'}, status=400)
did = DID.objects.filter(user__isnull=True).first() did = DID.objects.filter(user__isnull=True).first()
if not did: if not did:
return JsonResponse({'error': 'Invalid DID'}, status=400) return JsonResponse({'error': 'Invalid DID'}, status=400)
schema = Schemas.objects.filter(file_schema=typ).first() schema = Schemas.objects.filter(type=typ).first()
if not schema: if not schema:
return JsonResponse({'error': 'Invalid credential'}, status=400) return JsonResponse({'error': 'Invalid credential'}, status=400)
user = User.objects.filter(is_admin=True).first()
cred = VerificableCredential( cred = VerificableCredential(
csv_data=vc, csv_data=vc,
issuer_did=did, issuer_did=did,
@ -97,7 +98,7 @@ def webhook_issue(request):
) )
cred.set_type() cred.set_type()
vc_signed = cred.issue(did, domain=request.get_host(), encrypt=False) vc_signed = cred.issue(did, domain=request.get_host(), save=save)
return JsonResponse({'status': 'success', "data": vc_signed}, status=200) return JsonResponse({'status': 'success', "data": vc_signed}, status=200)