From 72c8fac29e73305d2949e17ba10e7462cc8bfef8 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 29 Jan 2024 18:49:02 +0100 Subject: [PATCH] fix new api for iota --- ereuse_devicehub/modules/did/views.py | 18 +++--------------- .../modules/dpp/commands/register_user_dlt.py | 1 - ereuse_devicehub/resources/user/models.py | 18 +++++------------- ereuse_devicehub/views.py | 4 ++++ 4 files changed, 12 insertions(+), 29 deletions(-) diff --git a/ereuse_devicehub/modules/did/views.py b/ereuse_devicehub/modules/did/views.py index 63aea371..ed1a4010 100644 --- a/ereuse_devicehub/modules/did/views.py +++ b/ereuse_devicehub/modules/did/views.py @@ -95,22 +95,10 @@ class DidView(View): if not session.get('token_dlt'): return [] - token_dlt = session.get('token_dlt') - api_dlt = app.config.get('API_DLT') - if not token_dlt or not api_dlt: + role = session.get('iota_abac_attributes', {}).get('role', '') + if not role: return [] - - api = API(api_dlt, token_dlt, "ethereum") - - result = api.check_user_roles() - if result.get('Status') != 200: - return [] - - if 'Success' not in result.get('Data', {}).get('status'): - return [] - - rols = result.get('Data', {}).get('data', {}) - self.context['rols'] = [(k, k) for k, v in rols.items() if v] + self.context['rols'] = [(x.strip(), x.strip()) for x in role.split(",")] 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 75c33532..63e2e5b5 100644 --- a/ereuse_devicehub/modules/dpp/commands/register_user_dlt.py +++ b/ereuse_devicehub/modules/dpp/commands/register_user_dlt.py @@ -43,7 +43,6 @@ class RegisterUserDlt: user = User(email=email, password=password) user.individuals.add(Person(name=name)) - # import pdb; pdb.set_trace() try: response = register_user(api_dlt, privateKey=eth_priv_key[2:]) api_token = response.get('data', {}).get('api_token') diff --git a/ereuse_devicehub/resources/user/models.py b/ereuse_devicehub/resources/user/models.py index f7a20ad6..56d8d722 100644 --- a/ereuse_devicehub/resources/user/models.py +++ b/ereuse_devicehub/resources/user/models.py @@ -180,21 +180,13 @@ class User(UserMixin, Thing): if not api_dlt: return [] - api = API(api_dlt, token_dlt, "ethereum") - - result = api.check_user_roles() - if result.get('Status') != 200: - return [] - - if 'Success' not in result.get('Data', {}).get('status'): - return [] - - rols = result.get('Data', {}).get('data', {}) - return [(k, k) for k, v in rols.items() if v] + self.get_abac_did() + role = session.get('iota_abac_attributes', {}).get('role', []) + return [(x.strip(), x.strip()) for x in role.split(",")] def _call_abac(self, path): abac_tk = app.config.get('ABAC_TOKEN') - abac_coockie = app.config.get('ABAC_COOKIE') + # abac_coockie = app.config.get('ABAC_COOKIE') domain = app.config.get('ABAC_URL') eth_pub_key = session.get('eth_pub_key') @@ -204,7 +196,7 @@ class User(UserMixin, Thing): header = { 'Authorization': f'Bearer {abac_tk}', - 'Cookie': abac_coockie + # 'Cookie': abac_coockie } url = f'{domain}{eth_pub_key}/{abac_path}' return requests.get(url, headers=header) diff --git a/ereuse_devicehub/views.py b/ereuse_devicehub/views.py index e55000c9..ef695413 100644 --- a/ereuse_devicehub/views.py +++ b/ereuse_devicehub/views.py @@ -28,6 +28,10 @@ class LoginView(View): template_name = 'ereuse_devicehub/user_login.html' def dispatch_request(self): + if session.get('_user_id'): + next_url = flask.request.args.get('next') + return flask.redirect(next_url or flask.url_for('inventory.devicelist')) + form = LoginForm() if form.validate_on_submit(): # Login and validate the user.