From da8d43f9f670a055b205eb71986edf02a94e6e50 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 26 Mar 2024 18:25:06 +0100 Subject: [PATCH] fix new dlt_keys structure --- ereuse_devicehub/forms.py | 5 ++- ereuse_devicehub/modules/did/views.py | 9 ++-- .../modules/dpp/commands/register_user_dlt.py | 45 +++++-------------- 3 files changed, 17 insertions(+), 42 deletions(-) diff --git a/ereuse_devicehub/forms.py b/ereuse_devicehub/forms.py index d9a1a577..dbf52a1d 100644 --- a/ereuse_devicehub/forms.py +++ b/ereuse_devicehub/forms.py @@ -70,7 +70,10 @@ class LoginForm(FlaskForm): self.form_errors.append(self.error_messages['inactive']) if 'dpp' in app.blueprints.keys(): - dlt_keys = user.get_dlt_keys(self.password.data) + dlt_keys = user.get_dlt_keys( + self.password.data + ).get('data', {}) + token_dlt = dlt_keys.get('api_token') eth_pub_key = dlt_keys.get('eth_pub_key') session['token_dlt'] = token_dlt diff --git a/ereuse_devicehub/modules/did/views.py b/ereuse_devicehub/modules/did/views.py index c6307e6d..fec13bc9 100644 --- a/ereuse_devicehub/modules/did/views.py +++ b/ereuse_devicehub/modules/did/views.py @@ -101,13 +101,10 @@ class DidView(View): _role = g.user.get_rols_dlt() role = session.get('iota_abac_attributes', {}).get('role', '') - if not role and _role: - self.context['rols'] = [(x, x) for x in _role] - return - - if not role: + if not _role: return [] - self.context['rols'] = [(x.strip(), x.strip()) for x in role.split(",")] + self.context['rols'] = _role + return _role def get_rol(self): rols = self.context.get('rols', []) diff --git a/ereuse_devicehub/modules/dpp/commands/register_user_dlt.py b/ereuse_devicehub/modules/dpp/commands/register_user_dlt.py index 17d62cc6..85034acf 100644 --- a/ereuse_devicehub/modules/dpp/commands/register_user_dlt.py +++ b/ereuse_devicehub/modules/dpp/commands/register_user_dlt.py @@ -3,6 +3,7 @@ import requests import click +from ereuseapi.methods import API from flask import g, current_app as app from ereuseapi.methods import register_user from ereuse_devicehub.db import db @@ -34,9 +35,7 @@ class RegisterUserDlt: email = data.get("email") name = email.split('@')[0] password = data.get("password") - api_dlt = app.config.get('API_DLT') - eth_priv_key = data.get("eth_priv_key") - eth_pub_key = data.get("eth_pub_key") + ethereum = {"data": data.get("data")} user = User.query.filter_by(email=email).first() @@ -44,43 +43,19 @@ class RegisterUserDlt: user = User(email=email, password=password) user.individuals.add(Person(name=name)) - try: - response = register_user(api_dlt, privateKey=eth_priv_key[2:]) - api_token = response.get('data', {}).get('api_token') - except Exception: - api_token = "" - - ethereum = { - "eth_pub_key": eth_pub_key, - "eth_priv_key": eth_priv_key, - "api_token": api_token - } data_eth = json.dumps(ethereum) user.api_keys_dlt = encrypt(password, data_eth) roles = [] - try: - abac_tk = app.config.get('ABAC_TOKEN') - domain = app.config.get('ABAC_URL') - eth_pub_key = eth_pub_key + token_dlt = ethereum["data"]["api_token"] + api_dlt = app.config.get('API_DLT') + api = API(api_dlt, token_dlt, "ethereum") + result = api.check_user_roles() - header = { - 'Authorization': f'Bearer {abac_tk}', - } - url = f'{domain}{eth_pub_key}/attributes' - r = requests.get(url, headers=header) - attributes = {} - for j in r.json(): - k = j.get('attributeURI', '').split('/')[-1].split("#")[-1] - v = j.get('attributeValue', '').strip() - if not (k and v): - continue - attributes[k] = v - - if attributes.get('role'): - roles.append(attributes.get('role')) - except Exception: - roles = ["operator"] + if result.get('Status') == 200: + if 'Success' in result.get('Data', {}).get('status'): + rols = result.get('Data', {}).get('data', {}) + roles = [(k, k) for k, v in rols.items() if v] user.rols_dlt = json.dumps(roles)