Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] encrypted_ props #254

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
from . import exceptions
from ._utils import AttributeDict as _AttributeDict

from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
from cryptography.hazmat.backends import default_backend



# pylint: disable=too-many-instance-attributes
# pylint: disable=too-many-arguments
Expand All @@ -31,6 +37,7 @@ def __init__(
static_folder='static',
url_base_pathname='/',
compress=True,
encryption_keystring='',
**kwargs):

# pylint-disable: too-many-instance-attributes
Expand Down Expand Up @@ -122,6 +129,23 @@ def add_url(name, view_func, methods=('GET',)):
self._cached_layout = None
self.routes = []

self.fernet_key = None

if encryption_keystring != '':
if len(encryption_keystring) < 30:
raise Exception(
''
'encryption_keystring must be at least 30 characters.')
else:
hkdf = HKDF(
algorithm=hashes.SHA256(),
length=32,
salt=None,
info=None,
backend=default_backend()
)
self.fernet_key = hkdf.derive(encryption_keystring)

@property
def layout(self):
return self._layout
Expand Down Expand Up @@ -513,6 +537,18 @@ def wrap_func(func):
def add_context(*args, **kwargs):

output_value = func(*args, **kwargs)

if output.component_property.startswith("encrypted_"):
if not self.fernet_key:
raise Exception(
''
'You must provide an encryption_keystring '
'to use encrypted_* properties.')
f = Fernet(self.fernet_key)
output_value_str = json.dumps(output_value,
cls=plotly.utils.PlotlyJSONEncoder)
output_value = f.encrypt(bytes(output_value_str))

response = {
'response': {
'props': {
Expand Down Expand Up @@ -547,6 +583,14 @@ def dispatch(self):
c['property'] == component_registration['property'] and
c['id'] == component_registration['id']
][0])
if component_registration['property'].startswith("encrypted_"):
if not self.fernet_key:
raise Exception(
''
'You must provide an encryption_keystring '
'to use encrypted_* properties.')
f = Fernet(self.fernet_key)
args[-1] = json.loads(f.decrypt(bytes(args[-1])))

for component_registration in self.callback_map[target_id]['state']:
args.append([
Expand Down