Skip to content

Commit 701ece7

Browse files
author
Sebastian Molenda
authored
Decouple and add configurability of crypto instance (#168)
* Decouple and add configurability of crypto instance * Fix naming
1 parent 1b44c1f commit 701ece7

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

pubnub/crypto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class PubNubCryptodome(PubNubCrypto):
1616
fallback_mode = None
1717

1818
def __init__(self, pubnub_config):
19-
self.pubnub_configuration = pubnub_config
19+
super().__init__(pubnub_config)
2020
self.mode = pubnub_config.cipher_mode
2121
self.fallback_mode = pubnub_config.fallback_cipher_mode
2222

pubnub/crypto_core.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33

44
class PubNubCrypto:
5+
def __init__(self, pubnub_config):
6+
self.pubnub_configuration = pubnub_config
7+
58
@abstractmethod
69
def encrypt(self, key, msg):
710
pass

pubnub/pnconfiguration.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from Cryptodome.Cipher import AES
22
from pubnub.enums import PNHeartbeatNotificationOptions, PNReconnectionPolicy
33
from pubnub.exceptions import PubNubException
4+
from pubnub.crypto import PubNubCrypto
45

56

67
class PNConfiguration(object):
@@ -43,6 +44,8 @@ def __init__(self):
4344
self.heartbeat_default_values = True
4445
self._presence_timeout = PNConfiguration.DEFAULT_PRESENCE_TIMEOUT
4546
self._heartbeat_interval = PNConfiguration.DEFAULT_HEARTBEAT_INTERVAL
47+
self.cryptor = None
48+
self.file_cryptor = None
4649

4750
def validate(self):
4851
PNConfiguration.validate_not_empty_string(self.uuid)
@@ -102,15 +105,20 @@ def crypto(self):
102105
return self.crypto_instance
103106

104107
def _init_cryptodome(self):
105-
from .crypto import PubNubCryptodome
106-
self.crypto_instance = PubNubCryptodome(self)
108+
if not self.cryptor:
109+
from pubnub.crypto import PubNubCryptodome
110+
self.cryptor = PubNubCryptodome
111+
self.crypto_instance = self.cryptor(self)
107112

108113
def _init_file_crypto(self):
109114
from .crypto import PubNubFileCrypto
110-
self.file_crypto_instance = PubNubFileCrypto(self)
115+
if not self.file_cryptor:
116+
from pubnub.crypto import PubNubFileCrypto
117+
self.file_cryptor = PubNubFileCrypto
118+
self.file_crypto_instance = self.file_cryptor(self)
111119

112120
@property
113-
def file_crypto(self):
121+
def file_crypto(self) -> PubNubCrypto:
114122
if not self.file_crypto_instance:
115123
self._init_file_crypto()
116124

tests/unit/test_crypto.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from pubnub.pubnub import PubNub
2-
from pubnub.crypto import PubNubCryptodome
3-
from tests.helper import pnconf_file_copy, hardcoded_iv_config_copy
2+
from pubnub.crypto import PubNubCryptodome, PubNubCrypto
3+
from tests.helper import pnconf_file_copy, hardcoded_iv_config_copy, pnconf_env_copy
4+
45

56
crypto = PubNubCryptodome(pnconf_file_copy())
67
crypto_hardcoded_iv = PubNubCryptodome(hardcoded_iv_config_copy())
@@ -62,3 +63,19 @@ def test_encrypt_and_decrypt_file(self, file_for_upload, file_upload_test_data):
6263

6364
decrypted_file = pubnub.decrypt(KEY, encrypted_file)
6465
assert file_upload_test_data["FILE_CONTENT"] == decrypted_file.decode("utf-8")
66+
67+
68+
class TestPubNubCryptoInterface:
69+
def test_get_default_crypto(self):
70+
config = pnconf_env_copy()
71+
assert isinstance(config.crypto, PubNubCrypto)
72+
assert isinstance(config.crypto, PubNubCryptodome)
73+
74+
def test_get_custom_crypto(self):
75+
class CustomCryptor(PubNubCrypto):
76+
pass
77+
78+
config = pnconf_env_copy()
79+
config.cryptor = CustomCryptor
80+
assert isinstance(config.crypto, PubNubCrypto)
81+
assert isinstance(config.crypto, CustomCryptor)

0 commit comments

Comments
 (0)