From 32dc4445e4ce8ac3c9a8d8487969fd80bbd0ba45 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 25 Jan 2023 13:20:43 +0100 Subject: [PATCH] fix some things of interaction with the api and register dpps --- ereuse_devicehub/config.py | 2 +- ereuse_devicehub/resources/action/models.py | 28 ++++++++++++++++----- ereuse_devicehub/resources/device/models.py | 26 +++++++++++++------ ereuse_devicehub/resources/enums.py | 20 +++++++++++---- ereuse_devicehub/resources/user/models.py | 10 +++----- 5 files changed, 61 insertions(+), 25 deletions(-) diff --git a/ereuse_devicehub/config.py b/ereuse_devicehub/config.py index 03908686..630a5ffb 100644 --- a/ereuse_devicehub/config.py +++ b/ereuse_devicehub/config.py @@ -96,4 +96,4 @@ class DevicehubConfig(Config): MAIL_USE_TLS = config('MAIL_USE_TLS', True) MAIL_DEFAULT_SENDER = config('MAIL_DEFAULT_SENDER', '') API_DLT = config('API_DLT', None) - KEYUSER1 = config('KEYUSER1', None) + API_DLT_TOKEN = config('API_DLT_TOKEN', None) diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index e7cb39b6..85caab24 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -13,6 +13,7 @@ Within the above general classes are subclasses in A order. import copy import hashlib import json +import time from collections import Iterable from contextlib import suppress from datetime import datetime, timedelta, timezone @@ -87,6 +88,7 @@ from ereuse_devicehub.resources.enums import ( RatingRange, Severity, SnapshotSoftware, + StatusCode, TestDataStorageLength, ) from ereuse_devicehub.resources.models import STR_SM_SIZE, Thing @@ -761,26 +763,40 @@ class Snapshot(JoinedWithOneDeviceMixin, ActionWithOneDevice): return snapshots and 'update' or 'new_device' def register_passport_dlt(self): - import pdb - - pdb.set_trace() if 'trublo' not in app.blueprints.keys() or not self.device.hid: return if not session.get('token_dlt'): return - token_dlt = session.get('token_dlt').split(".")[1] + token_dlt = session.get('token_dlt') api_dlt = app.config.get('API_DLT') if not token_dlt or not api_dlt: return api = API(api_dlt, token_dlt, "ethereum") - dpp = "{chid}:{phid}".format(chid=self.device.chid, phid=self.phid) + dpp = "{chid}:{phid}".format(chid=self.device.chid, phid=self.phid_dpp) docSig = hashlib.sha3_256(self.json_wb.encode('utf-8')).hexdigest() docID = "{}".format(self.uuid or '') issuerID = "dh1:{user}".format(user=g.user.id) - api.issue_passport(dpp, docID, docSig, issuerID) + result = api.issue_passport(dpp, docID, docSig, issuerID) + # import pdb;pdb.set_trace() + if result['Status'] is not StatusCode.Success: + return + timestamp = result['Data'].get('timestamp', time.time()) + from ereuse_devicehub.resources.did.models import Dpp + + timestamp = result['Data'].get('data', {}).get('timestamp', time.time()) + d_issue = { + "device_id": self.device.id, + "snapshot": self, + "timestamp": timestamp, + "issuer_id": g.user.id, + "documentId": docID, + "key": dpp, + } + dpp_issue = Dpp(**d_issue) + db.session.add(dpp_issue) # def register_dlt(self): # from ereuse_devicehub.resources.did.models import PROOF_ENUM, Dpp, Proof diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index da7a3776..f1d185d3 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -3,6 +3,7 @@ import hashlib import json import os import pathlib +import time import uuid from contextlib import suppress from fractions import Fraction @@ -883,24 +884,35 @@ class Device(Thing): return types.get(self.type, '') def register_dlt(self): - import pdb - - pdb.set_trace() if 'trublo' not in app.blueprints.keys() or not self.hid: return if not session.get('token_dlt'): return - token_dlt = session.get('token_dlt').split(".")[1] + token_dlt = session.get('token_dlt') api_dlt = app.config.get('API_DLT') if not token_dlt or not api_dlt: return api = API(api_dlt, token_dlt, "ethereum") - api.register_device(self.chid) - # result = api.register_device(chid) + result = api.register_device(self.chid) + # import pdb; pdb.set_trace() + from ereuse_devicehub.resources.did.models import PROOF_ENUM, Proof + from ereuse_devicehub.resources.enums import StatusCode + + if result['Status'] == StatusCode.Success: + timestamp = result['Data'].get('data', {}).get('timestamp', time.time()) + d = { + "type": PROOF_ENUM['Register'], + "device_id": self.device.id, + "snapshot": self, + "timestamp": timestamp, + "issuer_id": g.user.id, + } + proof = Proof(**d) + db.session.add(proof) def unreliable(self): self.user_trusts = False @@ -1193,7 +1205,7 @@ class Computer(Device): """A chassis with components inside that can be processed automatically with Workbench Computer. - Computer is broadly extended by ``Desktop``, ``Laptop``, and + Computer is broa extended by ``Desktop``, ``Laptop``, and ``Server``. The property ``chassis`` defines it more granularly. """ diff --git a/ereuse_devicehub/resources/enums.py b/ereuse_devicehub/resources/enums.py index bbbcdc10..a2254b29 100644 --- a/ereuse_devicehub/resources/enums.py +++ b/ereuse_devicehub/resources/enums.py @@ -359,16 +359,16 @@ class ErasureStandards(Enum): """Software erasure standards.""" HMG_IS5 = 'British HMG Infosec Standard 5 (HMG IS5)' - """`British HMG Infosec Standard 5 (HMG IS5) - `_. - + """`British HMG Infosec Standard 5 (HMG IS5) + `. + In order to follow this standard, an erasure must have the following steps: - + 1. A first step writing zeroes to the data-storage units. 2. A second step erasing with random data, verifying the erasure success in each hard-drive sector. - + And be an :class:`ereuse_devicehub.resources.action.models.EraseSectors`. """ @@ -432,3 +432,13 @@ class SessionType(IntEnum): def __str__(self): return self.name + + +class StatusCode(IntEnum): + """The code of the status response of api dlt.""" + + Success = 201 + NotWork = 400 + + def __str__(self): + return self.name diff --git a/ereuse_devicehub/resources/user/models.py b/ereuse_devicehub/resources/user/models.py index e72ca075..8b7975b6 100644 --- a/ereuse_devicehub/resources/user/models.py +++ b/ereuse_devicehub/resources/user/models.py @@ -105,7 +105,6 @@ class User(UserMixin, Thing): from modules.trublo.utils import encrypt - # import pdb; pdb.set_trace() api_dlt = app.config.get('API_DLT') data = register_user(api_dlt) api_token = data.get('data', {}).get('api_token') @@ -119,8 +118,6 @@ class User(UserMixin, Thing): from modules.trublo.utils import decrypt - # import pdb; pdb.set_trace() - if not self.api_keys_dlt: return {} @@ -145,14 +142,15 @@ class User(UserMixin, Thing): if not api_token: api_token = session.get('token_dlt', '.') target_user = api_token.split(".")[0] - keyUser1 = app.config.get('KEYUSER1') + keyUser1 = app.config.get('API_DLT_TOKEN') api_dlt = app.config.get('API_DLT') - if not keyUser1 or api_dlt: + if not keyUser1 or not api_dlt: return apiUser1 = API(api_dlt, keyUser1, "ethereum") - apiUser1.issue_credential("Operator", target_user) + result = apiUser1.issue_credential("Operator", target_user) + return result class UserInventory(db.Model):