fix some things of interaction with the api and register dpps

This commit is contained in:
Cayo Puigdefabregas 2023-01-25 13:20:43 +01:00
parent e7e595f2c2
commit 32dc4445e4
5 changed files with 61 additions and 25 deletions

View File

@ -96,4 +96,4 @@ class DevicehubConfig(Config):
MAIL_USE_TLS = config('MAIL_USE_TLS', True) MAIL_USE_TLS = config('MAIL_USE_TLS', True)
MAIL_DEFAULT_SENDER = config('MAIL_DEFAULT_SENDER', '') MAIL_DEFAULT_SENDER = config('MAIL_DEFAULT_SENDER', '')
API_DLT = config('API_DLT', None) API_DLT = config('API_DLT', None)
KEYUSER1 = config('KEYUSER1', None) API_DLT_TOKEN = config('API_DLT_TOKEN', None)

View File

@ -13,6 +13,7 @@ Within the above general classes are subclasses in A order.
import copy import copy
import hashlib import hashlib
import json import json
import time
from collections import Iterable from collections import Iterable
from contextlib import suppress from contextlib import suppress
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
@ -87,6 +88,7 @@ from ereuse_devicehub.resources.enums import (
RatingRange, RatingRange,
Severity, Severity,
SnapshotSoftware, SnapshotSoftware,
StatusCode,
TestDataStorageLength, TestDataStorageLength,
) )
from ereuse_devicehub.resources.models import STR_SM_SIZE, Thing 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' return snapshots and 'update' or 'new_device'
def register_passport_dlt(self): def register_passport_dlt(self):
import pdb
pdb.set_trace()
if 'trublo' not in app.blueprints.keys() or not self.device.hid: if 'trublo' not in app.blueprints.keys() or not self.device.hid:
return return
if not session.get('token_dlt'): if not session.get('token_dlt'):
return return
token_dlt = session.get('token_dlt').split(".")[1] token_dlt = session.get('token_dlt')
api_dlt = app.config.get('API_DLT') api_dlt = app.config.get('API_DLT')
if not token_dlt or not api_dlt: if not token_dlt or not api_dlt:
return return
api = API(api_dlt, token_dlt, "ethereum") 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() docSig = hashlib.sha3_256(self.json_wb.encode('utf-8')).hexdigest()
docID = "{}".format(self.uuid or '') docID = "{}".format(self.uuid or '')
issuerID = "dh1:{user}".format(user=g.user.id) 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): # def register_dlt(self):
# from ereuse_devicehub.resources.did.models import PROOF_ENUM, Dpp, Proof # from ereuse_devicehub.resources.did.models import PROOF_ENUM, Dpp, Proof

View File

@ -3,6 +3,7 @@ import hashlib
import json import json
import os import os
import pathlib import pathlib
import time
import uuid import uuid
from contextlib import suppress from contextlib import suppress
from fractions import Fraction from fractions import Fraction
@ -883,24 +884,35 @@ class Device(Thing):
return types.get(self.type, '') return types.get(self.type, '')
def register_dlt(self): def register_dlt(self):
import pdb
pdb.set_trace()
if 'trublo' not in app.blueprints.keys() or not self.hid: if 'trublo' not in app.blueprints.keys() or not self.hid:
return return
if not session.get('token_dlt'): if not session.get('token_dlt'):
return return
token_dlt = session.get('token_dlt').split(".")[1] token_dlt = session.get('token_dlt')
api_dlt = app.config.get('API_DLT') api_dlt = app.config.get('API_DLT')
if not token_dlt or not api_dlt: if not token_dlt or not api_dlt:
return return
api = API(api_dlt, token_dlt, "ethereum") api = API(api_dlt, token_dlt, "ethereum")
api.register_device(self.chid) result = api.register_device(self.chid)
# result = api.register_device(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): def unreliable(self):
self.user_trusts = False self.user_trusts = False
@ -1193,7 +1205,7 @@ class Computer(Device):
"""A chassis with components inside that can be processed """A chassis with components inside that can be processed
automatically with Workbench Computer. 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. ``Server``. The property ``chassis`` defines it more granularly.
""" """

View File

@ -360,7 +360,7 @@ class ErasureStandards(Enum):
HMG_IS5 = 'British HMG Infosec Standard 5 (HMG IS5)' HMG_IS5 = 'British HMG Infosec Standard 5 (HMG IS5)'
"""`British HMG Infosec Standard 5 (HMG IS5) """`British HMG Infosec Standard 5 (HMG IS5)
<https://en.wikipedia.org/wiki/Infosec_Standard_5>`_. <https://en.wikipedia.org/wiki/Infosec_Standard_5>`.
In order to follow this standard, an erasure must have the In order to follow this standard, an erasure must have the
following steps: following steps:
@ -432,3 +432,13 @@ class SessionType(IntEnum):
def __str__(self): def __str__(self):
return self.name 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

View File

@ -105,7 +105,6 @@ class User(UserMixin, Thing):
from modules.trublo.utils import encrypt from modules.trublo.utils import encrypt
# import pdb; pdb.set_trace()
api_dlt = app.config.get('API_DLT') api_dlt = app.config.get('API_DLT')
data = register_user(api_dlt) data = register_user(api_dlt)
api_token = data.get('data', {}).get('api_token') api_token = data.get('data', {}).get('api_token')
@ -119,8 +118,6 @@ class User(UserMixin, Thing):
from modules.trublo.utils import decrypt from modules.trublo.utils import decrypt
# import pdb; pdb.set_trace()
if not self.api_keys_dlt: if not self.api_keys_dlt:
return {} return {}
@ -145,14 +142,15 @@ class User(UserMixin, Thing):
if not api_token: if not api_token:
api_token = session.get('token_dlt', '.') api_token = session.get('token_dlt', '.')
target_user = api_token.split(".")[0] 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') api_dlt = app.config.get('API_DLT')
if not keyUser1 or api_dlt: if not keyUser1 or not api_dlt:
return return
apiUser1 = API(api_dlt, keyUser1, "ethereum") 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): class UserInventory(db.Model):