Skip to content

Commit

Permalink
Add JWE decryption to security/encryption
Browse files Browse the repository at this point in the history
Not yet used but needed for the LMS metadata in annotation feature.
  • Loading branch information
marcospri committed Sep 11, 2023
1 parent 72d4f86 commit 9486204
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
7 changes: 7 additions & 0 deletions h/security/encryption.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import base64
import json
import os

from Cryptodome.Hash import SHA512
from Cryptodome.Protocol.KDF import HKDF
from jose import jwe
from passlib.context import CryptContext

DEFAULT_ENTROPY = 32
Expand Down Expand Up @@ -69,3 +71,8 @@ def token_urlsafe(nbytes=None):
nbytes = DEFAULT_ENTROPY
tok = os.urandom(nbytes)
return base64.urlsafe_b64encode(tok).rstrip(b"=").decode("ascii")


def decrypt_jwe_dict(secret: bytes, payload: str) -> dict:
"""Decrypts the JWE payloads into a dictionary."""
return json.loads(jwe.decrypt(payload, secret.ljust(32)[:32]))
28 changes: 27 additions & 1 deletion tests/h/security/encryption_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
from hypothesis import strategies as st
from passlib.context import CryptContext

from h.security.encryption import derive_key, password_context, token_urlsafe
from h.security.encryption import (
decrypt_jwe_dict,
derive_key,
password_context,
token_urlsafe,
)

REASONABLE_INFO = st.text(alphabet=string.printable)
REASONABLE_KEY_MATERIAL = st.binary(min_size=8, max_size=128)
Expand Down Expand Up @@ -152,3 +157,24 @@ def test_token_urlsafe_no_args():

assert isinstance(tok, str)
assert len(tok) > 32


class TestDecryptDict:
def test_decrypt_dict(self, secret, jwe, json):
plain_text_dict = decrypt_jwe_dict(secret, "payload")

jwe.decrypt.assert_called_once_with("payload", secret.ljust(32))
json.loads.assert_called_once_with(jwe.decrypt.return_value)
assert plain_text_dict == json.loads.return_value

@pytest.fixture
def secret(self):
return b"VERY SECRET"

@pytest.fixture
def json(self, patch):
return patch("h.security.encryption.json")

@pytest.fixture
def jwe(self, patch):
return patch("h.security.encryption.jwe")

0 comments on commit 9486204

Please sign in to comment.