2023-05-31 14:30:49 +00:00
|
|
|
import flask
|
|
|
|
from ereuseapi.methods import API
|
|
|
|
from flask import Blueprint
|
|
|
|
from flask import current_app as app
|
2023-06-02 09:00:05 +00:00
|
|
|
from flask import g, render_template, request, session
|
2023-05-31 14:30:49 +00:00
|
|
|
from flask.json import jsonify
|
|
|
|
from flask.views import View
|
|
|
|
|
|
|
|
from ereuse_devicehub import __version__
|
2023-06-16 16:05:52 +00:00
|
|
|
from ereuse_devicehub.modules.dpp.models import Dpp
|
2023-05-31 14:30:49 +00:00
|
|
|
from ereuse_devicehub.resources.device.models import Device
|
|
|
|
|
2023-06-16 16:05:52 +00:00
|
|
|
did = Blueprint('did', __name__, url_prefix='/did', template_folder='templates')
|
2023-05-31 14:30:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DidView(View):
|
|
|
|
methods = ['GET', 'POST']
|
2023-06-23 08:22:46 +00:00
|
|
|
template_name = 'anonymous.html'
|
2023-05-31 14:30:49 +00:00
|
|
|
|
|
|
|
def dispatch_request(self, id_dpp):
|
2023-06-02 09:00:05 +00:00
|
|
|
self.dpp = None
|
|
|
|
self.device = None
|
2023-05-31 14:30:49 +00:00
|
|
|
self.get_ids(id_dpp)
|
|
|
|
|
|
|
|
self.context = {
|
|
|
|
'version': __version__,
|
|
|
|
'oidc': 'oidc' in app.blueprints.keys(),
|
|
|
|
'user': g.user,
|
|
|
|
'path': request.path,
|
2023-06-02 09:00:05 +00:00
|
|
|
'last_dpp': None,
|
|
|
|
'before_dpp': None,
|
|
|
|
'rols': [],
|
|
|
|
'rol': None,
|
2023-05-31 14:30:49 +00:00
|
|
|
}
|
|
|
|
self.get_rols()
|
|
|
|
self.get_rol()
|
|
|
|
self.get_device()
|
2023-06-02 09:00:05 +00:00
|
|
|
self.get_last_dpp()
|
|
|
|
self.get_before_dpp()
|
2023-05-31 14:30:49 +00:00
|
|
|
|
2023-06-05 14:00:10 +00:00
|
|
|
if self.accept_json():
|
2023-05-31 14:30:49 +00:00
|
|
|
return jsonify(self.get_result())
|
2023-06-23 08:22:46 +00:00
|
|
|
self.get_template()
|
2023-05-31 14:30:49 +00:00
|
|
|
|
|
|
|
return render_template(self.template_name, **self.context)
|
|
|
|
|
2023-06-23 08:22:46 +00:00
|
|
|
def get_template(self):
|
|
|
|
rol = self.context.get('rol')
|
|
|
|
if not rol:
|
|
|
|
return
|
|
|
|
|
|
|
|
tlmp = {
|
|
|
|
"isOperator": "operator.html",
|
|
|
|
"isVerifier": "verifier.html",
|
|
|
|
}
|
|
|
|
self.template_name = tlmp.get(rol, self.template_name)
|
|
|
|
|
2023-06-05 14:00:10 +00:00
|
|
|
def accept_json(self):
|
|
|
|
if 'json' in request.headers.get('Accept', []):
|
|
|
|
return True
|
|
|
|
if "application/json" in request.headers.get("Content-Type", []):
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2023-05-31 14:30:49 +00:00
|
|
|
def get_ids(self, id_dpp):
|
|
|
|
self.id_dpp = None
|
|
|
|
self.chid = id_dpp
|
|
|
|
|
|
|
|
if len(id_dpp.split(":")) == 2:
|
|
|
|
self.id_dpp = id_dpp
|
|
|
|
self.chid = id_dpp.split(':')[0]
|
|
|
|
|
|
|
|
def get_rols(self):
|
|
|
|
rols = session.get('rols')
|
|
|
|
if not g.user.is_authenticated and not rols:
|
|
|
|
return []
|
|
|
|
|
|
|
|
if rols:
|
2023-06-02 09:00:05 +00:00
|
|
|
self.context['rols'] = rols
|
2023-05-31 14:30:49 +00:00
|
|
|
|
2023-06-02 09:42:38 +00:00
|
|
|
if 'dpp' not in app.blueprints.keys():
|
2023-05-31 14:30:49 +00:00
|
|
|
return []
|
|
|
|
|
|
|
|
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:
|
|
|
|
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]
|
|
|
|
|
|
|
|
def get_rol(self):
|
|
|
|
rols = self.context.get('rols', [])
|
|
|
|
rol = len(rols) == 1 and rols[0][0] or None
|
|
|
|
if 'rol' in request.args and not rol:
|
|
|
|
rol = dict(rols).get(request.args.get('rol'))
|
|
|
|
self.context['rol'] = rol
|
|
|
|
|
|
|
|
def get_device(self):
|
|
|
|
if self.id_dpp:
|
|
|
|
self.dpp = Dpp.query.filter_by(key=self.id_dpp).one()
|
2023-06-02 09:00:05 +00:00
|
|
|
device = self.dpp.device
|
|
|
|
else:
|
|
|
|
device = Device.query.filter_by(chid=self.chid, active=True).first()
|
|
|
|
|
2023-05-31 14:30:49 +00:00
|
|
|
if not device:
|
|
|
|
return flask.abort(404)
|
2023-06-02 09:00:05 +00:00
|
|
|
|
2023-05-31 14:30:49 +00:00
|
|
|
placeholder = device.binding or device.placeholder
|
|
|
|
device_abstract = placeholder and placeholder.binding or device
|
|
|
|
device_real = placeholder and placeholder.device or device
|
|
|
|
self.device = device_abstract
|
2023-06-02 09:00:05 +00:00
|
|
|
components = self.device.components
|
|
|
|
if self.dpp:
|
|
|
|
components = self.dpp.snapshot.components
|
2023-05-31 14:30:49 +00:00
|
|
|
|
|
|
|
self.context.update(
|
|
|
|
{
|
|
|
|
'placeholder': placeholder,
|
|
|
|
'device': self.device,
|
|
|
|
'device_abstract': device_abstract,
|
|
|
|
'device_real': device_real,
|
2023-06-02 09:00:05 +00:00
|
|
|
'components': components,
|
2023-05-31 14:30:49 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-06-02 09:00:05 +00:00
|
|
|
def get_last_dpp(self):
|
|
|
|
dpps = sorted(self.device.dpps, key=lambda x: x.created)
|
|
|
|
self.context['last_dpp'] = dpps and dpps[-1] or ''
|
|
|
|
return self.context['last_dpp']
|
|
|
|
|
|
|
|
def get_before_dpp(self):
|
|
|
|
if not self.dpp:
|
|
|
|
self.context['before_dpp'] = ''
|
|
|
|
return ''
|
|
|
|
|
|
|
|
dpps = sorted(self.device.dpps, key=lambda x: x.created)
|
|
|
|
before_dpp = ''
|
|
|
|
for dpp in dpps:
|
|
|
|
if dpp == self.dpp:
|
|
|
|
break
|
|
|
|
before_dpp = dpp
|
2023-05-31 14:30:49 +00:00
|
|
|
|
2023-06-02 09:00:05 +00:00
|
|
|
self.context['before_dpp'] = before_dpp
|
|
|
|
return before_dpp
|
2023-05-31 14:30:49 +00:00
|
|
|
|
|
|
|
def get_result(self):
|
|
|
|
data = {
|
2023-07-06 10:54:18 +00:00
|
|
|
'document': {},
|
2023-05-31 14:30:49 +00:00
|
|
|
'dpp': self.id_dpp,
|
2023-07-06 10:54:18 +00:00
|
|
|
'algorithm': "sha3_256",
|
2023-05-31 14:30:49 +00:00
|
|
|
}
|
|
|
|
result = {'data': data}
|
|
|
|
|
|
|
|
if self.dpp:
|
2023-07-06 10:54:18 +00:00
|
|
|
data['document'] = self.dpp.snapshot.json_hw
|
2023-06-02 09:00:05 +00:00
|
|
|
last_dpp = self.get_last_dpp()
|
2023-05-31 14:30:49 +00:00
|
|
|
url_last = ''
|
|
|
|
if last_dpp:
|
2023-06-05 14:00:10 +00:00
|
|
|
url_last = 'https://{host}/{did}'.format(
|
2023-06-05 14:03:35 +00:00
|
|
|
did=last_dpp.key, host=app.config.get('HOST')
|
2023-06-05 14:00:10 +00:00
|
|
|
)
|
2023-05-31 14:30:49 +00:00
|
|
|
data['url_last'] = url_last
|
|
|
|
return result
|
|
|
|
|
|
|
|
dpps = []
|
|
|
|
for d in self.device.dpps:
|
2023-07-06 10:54:18 +00:00
|
|
|
rr = {
|
|
|
|
'dpp': d.key,
|
|
|
|
'document': d.snapshot.json_hw,
|
|
|
|
'algorithm': "sha3_256",
|
|
|
|
}
|
2023-05-31 14:30:49 +00:00
|
|
|
dpps.append(rr)
|
|
|
|
return {'data': dpps}
|
|
|
|
|
|
|
|
|
|
|
|
did.add_url_rule('/<string:id_dpp>', view_func=DidView.as_view('did'))
|