fix new dlt_keys structure

This commit is contained in:
Cayo Puigdefabregas 2024-03-26 18:25:06 +01:00
parent f0710e88ec
commit da8d43f9f6
3 changed files with 17 additions and 42 deletions

View File

@ -70,7 +70,10 @@ class LoginForm(FlaskForm):
self.form_errors.append(self.error_messages['inactive']) self.form_errors.append(self.error_messages['inactive'])
if 'dpp' in app.blueprints.keys(): 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') token_dlt = dlt_keys.get('api_token')
eth_pub_key = dlt_keys.get('eth_pub_key') eth_pub_key = dlt_keys.get('eth_pub_key')
session['token_dlt'] = token_dlt session['token_dlt'] = token_dlt

View File

@ -101,13 +101,10 @@ class DidView(View):
_role = g.user.get_rols_dlt() _role = g.user.get_rols_dlt()
role = session.get('iota_abac_attributes', {}).get('role', '') role = session.get('iota_abac_attributes', {}).get('role', '')
if not role and _role: if not _role:
self.context['rols'] = [(x, x) for x in _role]
return
if not role:
return [] return []
self.context['rols'] = [(x.strip(), x.strip()) for x in role.split(",")] self.context['rols'] = _role
return _role
def get_rol(self): def get_rol(self):
rols = self.context.get('rols', []) rols = self.context.get('rols', [])

View File

@ -3,6 +3,7 @@ import requests
import click import click
from ereuseapi.methods import API
from flask import g, current_app as app from flask import g, current_app as app
from ereuseapi.methods import register_user from ereuseapi.methods import register_user
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
@ -34,9 +35,7 @@ class RegisterUserDlt:
email = data.get("email") email = data.get("email")
name = email.split('@')[0] name = email.split('@')[0]
password = data.get("password") password = data.get("password")
api_dlt = app.config.get('API_DLT') ethereum = {"data": data.get("data")}
eth_priv_key = data.get("eth_priv_key")
eth_pub_key = data.get("eth_pub_key")
user = User.query.filter_by(email=email).first() user = User.query.filter_by(email=email).first()
@ -44,43 +43,19 @@ class RegisterUserDlt:
user = User(email=email, password=password) user = User(email=email, password=password)
user.individuals.add(Person(name=name)) 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) data_eth = json.dumps(ethereum)
user.api_keys_dlt = encrypt(password, data_eth) user.api_keys_dlt = encrypt(password, data_eth)
roles = [] roles = []
try: token_dlt = ethereum["data"]["api_token"]
abac_tk = app.config.get('ABAC_TOKEN') api_dlt = app.config.get('API_DLT')
domain = app.config.get('ABAC_URL') api = API(api_dlt, token_dlt, "ethereum")
eth_pub_key = eth_pub_key result = api.check_user_roles()
header = { if result.get('Status') == 200:
'Authorization': f'Bearer {abac_tk}', if 'Success' in result.get('Data', {}).get('status'):
} rols = result.get('Data', {}).get('data', {})
url = f'{domain}{eth_pub_key}/attributes' roles = [(k, k) for k, v in rols.items() if v]
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"]
user.rols_dlt = json.dumps(roles) user.rols_dlt = json.dumps(roles)