diff --git a/did/views.py b/did/views.py index 3d4ef35..711e4be 100644 --- a/did/views.py +++ b/did/views.py @@ -4,6 +4,7 @@ import logging from django.http import JsonResponse, Http404 from django.views.generic.base import TemplateView from device.models import Device +from evidence.parse import Build from dpp.api_dlt import ALGORITHM from dpp.models import Proof from dpp.api_dlt import PROOF_TYPE @@ -93,6 +94,8 @@ class PublicDeviceWebView(TemplateView): 'algorithm': ALGORITHM, 'components': components, 'manufacturer DPP': '', + 'device': {}, + 'components': [], } result = { '@context': ['https://ereuse.org/dpp0.json'], @@ -100,7 +103,11 @@ class PublicDeviceWebView(TemplateView): } if len(self.pk.split(":")) > 1: - data['document'] = json.dumps(self.object.last_evidence.doc) + dev = Build(self.object.last_evidence.doc, None, check=True) + doc = dev.get_phid() + data['document'] = json.dumps(doc) + data['device'] = dev.device + data['components'] = dev.components self.object.get_evidences() last_dpp = Proof.objects.filter( @@ -122,13 +129,18 @@ class PublicDeviceWebView(TemplateView): self.object.initial() for d in self.object.evidences: d.get_doc() - ev = json.dumps(d.doc) + dev = Build(d.doc, None, check=True) + doc = dev.get_phid() + ev = json.dumps(doc) rr = { 'dpp': d.key, 'document': ev, 'algorithm': ALGORITHM, 'manufacturer DPP': '', + 'device': dev.device, + 'components': dev.components } + dpps.append(rr) return { '@context': ['https://ereuse.org/dpp0.json'], diff --git a/evidence/parse.py b/evidence/parse.py index ccee6a2..587588d 100644 --- a/evidence/parse.py +++ b/evidence/parse.py @@ -2,6 +2,9 @@ import json import hashlib import logging +from dmidecode import DMIParse +from evidence.parse_details import ParseSnapshot + from evidence.models import Annotation from evidence.xapian import index from dpp.api_dlt import register_device_dlt, register_passport_dlt @@ -55,6 +58,7 @@ class Build: def generate_chids(self): self.algorithms = { 'hidalgo1': self.get_hid_14(), + 'legacy_dpp': self.get_chid_dpp(), } def get_hid_14(self): @@ -73,6 +77,56 @@ class Build: self.chid = hashlib.sha3_256(hid.encode()).hexdigest() return self.chid + def get_chid_dpp(self): + if self.json.get("software") == "workbench-script": + dmidecode_raw = self.json["data"]["dmidecode"] + dmi = DMIParse(dmidecode_raw) + + manufacturer = dmi.manufacturer().strip() + model = dmi.model().strip() + chassis = self.get_chassis_dh() + serial_number = dmi.serial_number() + sku = self.get_sku() + typ = chassis + version = self.get_version() + hid = f"{manufacturer}{model}{chassis}{serial_number}{sku}{typ}{version}" + else: + device = self.json['device'] + hid = self.get_id_hw_dpp(device) + + self.chid = hashlib.sha3_256(hid.encode("utf-8")).hexdigest() + return self.chid + + def get_id_hw_dpp(self, d): + manufacturer = d.get("manufacturer", '') + model = d.get("model", '') + chassis = d.get("chassis", '') + serial_number = d.get("serialNumber", '') + sku = d.get("sku", '') + typ = d.get("type", '') + version = d.get("version", '') + + return f"{manufacturer}{model}{chassis}{serial_number}{sku}{typ}{version}" + + def get_phid(self): + if self.json.get("software") == "workbench-script": + data = ParseSnapshot(self.json) + self.device = data.device + self.components = data.components + else: + self.device = self.json.get("device") + self.components = self.json.get("components" []) + + device = self.get_id_hw_dpp(self.device) + components = self.json.get(self.components) + components = sorted(components, key=lambda x: x.get("type")) + doc = [("computer", device)] + + for c in components: + doc.append((c.get("type"), self.get_id_hw_dpp(c))) + + return doc + def create_annotations(self): annotation = Annotation.objects.filter( uuid=self.uuid, @@ -95,6 +149,26 @@ class Build: value=v ) +<<<<<<< HEAD +======= + def get_chassis_dh(self): + chassis = self.get_chassis() + lower_type = chassis.lower() + for k, v in CHASSIS_DH.items(): + if lower_type in v: + return k + return self.default + + def get_sku(self): + return self.dmi.get("System")[0].get("SKU Number", "n/a").strip() + + def get_chassis(self): + return self.dmi.get("Chassis")[0].get("Type", '_virtual') + + def get_version(self): + return self.dmi.get("System")[0].get("Verson", '_virtual') + +>>>>>>> 5949049 (new document and out device and components) def get_hid(self, snapshot): try: self.inxi = self.json["data"]["inxi"] @@ -127,5 +201,7 @@ class Build: return hashlib.sha3_256(json.dumps(doc).encode()).hexdigest() def register_device_dlt(self): - register_device_dlt(self.chid, self.phid, self.uuid, self.user) - register_passport_dlt(self.chid, self.phid, self.uuid, self.user) + chid = self.algorithms.get('legacy_dpp') + phid = self.get_signature(self.get_phid(self)) + register_device_dlt(chid, phid, self.uuid, self.user) + register_passport_dlt(chid, phid, self.uuid, self.user) diff --git a/utils/constants.py b/utils/constants.py index bc43922..75c5dc5 100644 --- a/utils/constants.py +++ b/utils/constants.py @@ -17,8 +17,19 @@ HID_ALGO1 = [ "sku" ] +LEGACY_DPP = [ + "manufacturer", + "model", + "chassis", + "serialNumber", + "sku", + "type", + "version" +] + ALGOS = { "hidalgo1": HID_ALGO1, + "legacy_dpp": LEGACY_DPP }