From 789eb1b52606482745c96645486e13763c94c604 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 25 Nov 2022 13:27:14 +0100 Subject: [PATCH] add register devices --- ereuse_devicehub/forms.py | 2 +- .../resources/action/views/snapshot.py | 1 + ereuse_devicehub/resources/device/models.py | 19 ++++++++++++++++++- ereuse_devicehub/resources/user/models.py | 3 +++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/ereuse_devicehub/forms.py b/ereuse_devicehub/forms.py index fbb136d8..4b237a8f 100644 --- a/ereuse_devicehub/forms.py +++ b/ereuse_devicehub/forms.py @@ -61,7 +61,7 @@ class LoginForm(FlaskForm): if not user.is_active: self.form_errors.append(self.error_messages['inactive']) - if 'trublo' not in app.blueprints.keys(): + if 'trublo' in app.blueprints.keys(): token_dlt = ( user.get_dlt_keys(self.password.data).get('data', {}).get('api_token') ) diff --git a/ereuse_devicehub/resources/action/views/snapshot.py b/ereuse_devicehub/resources/action/views/snapshot.py index dd967fee..49ea34dd 100644 --- a/ereuse_devicehub/resources/action/views/snapshot.py +++ b/ereuse_devicehub/resources/action/views/snapshot.py @@ -116,6 +116,7 @@ class SnapshotMixin: snapshot.severity = Severity.Warning self.is_server_erase(snapshot) + snapshot.device.register_dlt() return snapshot diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 0f398737..31c80a08 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -1,4 +1,5 @@ import copy +import hashlib import pathlib from contextlib import suppress from fractions import Fraction @@ -9,7 +10,9 @@ from typing import Dict, List, Set from boltons import urlutils from citext import CIText from ereuse_utils.naming import HID_CONVERSION_DOC, Naming -from flask import g, request +from ereuseapi.methods import API +from flask import current_app as app +from flask import g, request, session from more_itertools import unique_everseen from sqlalchemy import BigInteger, Boolean, Column from sqlalchemy import Enum as DBEnum @@ -816,6 +819,20 @@ class Device(Thing): } return types.get(self.type, '') + def register_dlt(self): + if 'trublo' not in app.blueprints.keys() or not self.hid: + return + + chid = hashlib.sha3_256(self.hid.encode('utf-8')).hexdigest() + token_dlt = session.get('token_dlt', ".").split(".")[1] + api_dlt = app.config.get('API_DLT') + if not token_dlt or not api_dlt: + return + + api = API(api_dlt, token_dlt, "ethereum") + + result = api.register_device(chid) + def __lt__(self, other): return self.id < other.id diff --git a/ereuse_devicehub/resources/user/models.py b/ereuse_devicehub/resources/user/models.py index d0287b94..33c77043 100644 --- a/ereuse_devicehub/resources/user/models.py +++ b/ereuse_devicehub/resources/user/models.py @@ -117,6 +117,9 @@ class User(UserMixin, Thing): from modules.trublo.utils import decrypt + if not self.api_keys_dlt: + return {} + data = decrypt(password, self.api_keys_dlt) return json.loads(data)