-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
You're not supposed to use bytes directly as the key. You're not supposed to do this: >>> from joserfc import jwt >>> >>> encoded = jwt.encode({"alg": "HS256"}, {"k": "value"}, b"secret_key") >>> token = jwt.decode(encoded, b"secret_key") As of joserfc 0.11.0 this raises a `DeprecationWarning`: `"Please use a Key object instead of bytes or string."` https://github.com/authlib/joserfc/blob/bf3eaf55a689f2170191ed3562d4815c4b0ebdd0/src/joserfc/jwk.py#L82-L86 Instead, you're supposed to do this: >>> from joserfc import jwt >>> from joserfc.jwk import OctKey >>> >>> key = OctKey.import_key(b"secret_key") >>> >>> encoded = jwt.encode({"alg": "HS256"}, {"k": "value"}, key) >>> token = jwt.decode(encoded, key) I also had to refactor `encryption_test.py` to fix a subtle issue with its `patch()` fixtures and pytest fixture execution order: the `encryption()` fixture (which instantiates the `h_vialib.secure.encryption.Encryption` object that is to be tested) was being executed **before** the various `patch()` fixtures to patch `joserfc` and `json`. This means that if `Encryption.__init__()` uses `joserfc` then it'll use the real `joserfc` and then, after `__init__()` has executed, the `patch()` fixtures execute and `joserfc` is replaced with a mock after it has already been used. Fix this by turning the patch fixtures into `autouse=True`. This fixes the issue because `autouse=True` fixtures are always executed before other fixtures. `patch()` fixtures should always be `autouse=True`. The `TestEncryption` class actually has some tests that are supposed to use a patched `joserfc` and some tests that are supposed to use the real `joserfc`. This isn't possible with an `autouse=True` patch fixture because the patch fixture will now be used for every test in the class. This forces me to split the patched- and not-patched tests into two separate classes. This is a good thing: it's confusing to have patched and not-patched tests in the same class, easy for the reader to miss that some tests are patched and some aren't. It's better to separate these two distinct types of test into two clearly labelled classes.
- Loading branch information
Showing
4 changed files
with
52 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,24 @@ | ||
import json | ||
|
||
from joserfc import jwe | ||
from joserfc.jwk import OctKey | ||
|
||
|
||
class Encryption: | ||
JWE_ALGORITHM = "dir" | ||
JWE_ENCRYPTION = "A128CBC-HS256" | ||
|
||
def __init__(self, secret: bytes): | ||
self._secret = secret.ljust(32)[:32] | ||
self._key = OctKey.import_key(secret.ljust(32)[:32]) | ||
|
||
def encrypt_dict(self, payload: dict) -> str: | ||
"""Encrypt a dictionary as a JWE.""" | ||
protected = {"alg": self.JWE_ALGORITHM, "enc": self.JWE_ENCRYPTION} | ||
return jwe.encrypt_compact( | ||
protected, json.dumps(payload).encode("utf-8"), self._secret | ||
protected, json.dumps(payload).encode("utf-8"), self._key | ||
) | ||
|
||
def decrypt_dict(self, payload: str) -> dict: | ||
"""Decrypts payloads created by `encrypt_dict`.""" | ||
data = jwe.decrypt_compact(payload, self._secret).plaintext | ||
data = jwe.decrypt_compact(payload, self._key).plaintext | ||
return json.loads(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters