Skip to content

Commit e6a9e41

Browse files
committed
params as object
added is_sdk_enabled adjusted tests added logs
1 parent 43c9d61 commit e6a9e41

11 files changed

Lines changed: 45 additions & 33 deletions

securenative/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from securenative.config import sdk_version
22
import securenative.event_types
33
from securenative.sdk import SecureNative, SecureNativeOptions
4-
from securenative.event_options import Event, User, CustomParam
4+
from securenative.event_options import Event, User
55
from securenative.singleton import init, flush, track, verify_webhook, verify

securenative/event_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(self, api_key, options=SecureNativeOptions(), http_client=HttpClien
2222
self.options = options
2323
self.queue = list()
2424

25-
if self.options.auto_send:
25+
if self.options.auto_send and self.options.is_sdk_enabled:
2626
interval_seconds = max(options.interval // 1000, 1)
2727
threading.Timer(interval_seconds, self.flush).start()
2828

securenative/event_manager_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
from securenative import Event, event_types
77
from securenative.event_manager import EventManager
8-
from securenative.event_options import User, CustomParam
8+
from securenative.event_options import User
99

1010

1111
class EventManagerTests(unittest.TestCase):
1212

1313
def build_event(self):
1414
return Event(event_type=event_types.login, user=User(user_id='1', user_email='1@2.com', user_name='1 2'),
15-
params=[CustomParam('key', 'val')])
15+
params={'key': 'val'})
1616

1717
def test_send_sync(self):
1818
api_key = 'key'
@@ -35,7 +35,7 @@ def test_send_async(self):
3535

3636
manager = EventManager(api_key=api_key, http_client=client)
3737
manager.send_async(event, resource)
38-
self.assertTrue(wg.wait(manager.options.interval//1000 * 2))
38+
self.assertTrue(wg.wait(manager.options.interval // 1000 * 2))
3939

4040
def assert_cb(self, wg, event, url, key, body):
4141
self.assertEqual(url, 'https://api.securenative.com/my/custom/resource')

securenative/event_options.py

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def __init__(self, user_id=u'', user_email=u'', user_name=u''):
1313

1414
class Event:
1515
def __init__(self, event_type, user=User(), ip=u'127.0.0.1', remote_ip=u'127.0.0.1', user_agent=u'unknown',
16-
sn_cookie_value=None, params=None):
16+
sn_cookie_value=None, params={}):
1717
self.event_type = event_type
1818
self.user = user
1919
self.remote_ip = remote_ip
@@ -22,20 +22,9 @@ def __init__(self, event_type, user=User(), ip=u'127.0.0.1', remote_ip=u'127.0.0
2222
self.params = params
2323
self.cid = ''
2424
self.fp = ''
25-
self.params = list()
26-
27-
if params is not None:
28-
if not isinstance(params, list):
29-
raise ValueError(
30-
'custom params should be a list of CustomParams, i.e: [CustomParams(key, value), ...])')
31-
if len(params) > 0 and not isinstance(params[0], CustomParam):
32-
raise ValueError(
33-
'custom params should be a list of CustomParams, i.e: [CustomParams(key, value), ...])')
34-
35-
self.params = params
3625

3726
if self.params is None:
38-
self.params = list()
27+
self.params = {}
3928

4029
if sn_cookie_value is not None:
4130
self.cid, self.fp = _parse_cookie(sn_cookie_value)
@@ -59,11 +48,5 @@ def as_dict(self):
5948
"vid": self.vid,
6049
"userAgent": self.user_agent,
6150
"device": {},
62-
"params": [{p.key: p.value} for p in self.params]
51+
"params": self.params
6352
}
64-
65-
66-
class CustomParam:
67-
def __init__(self, key, value):
68-
self.key = key
69-
self.value = value

securenative/integration_test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import securenative
77
from securenative import SecureNativeOptions
8-
from securenative.event_options import CustomParam, User, Event
8+
from securenative.event_options import User, Event
99

1010

1111
class IntegrationTest(unittest.TestCase):
@@ -29,5 +29,4 @@ def build_event(self, id, type, ip):
2929
return Event(event_type=type,
3030
ip=ip,
3131
user=User(user_id=id, user_email='python-sdk@securenative.com', user_name='python sdk')
32-
# params=[CustomParam('key', 'val')]
3332
)

securenative/logger.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import logging
2+
3+
enable_sn_logging = False
4+
5+
6+
def sn_logging(msg):
7+
if enable_sn_logging:
8+
logging.info(msg)

securenative/sdk.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from securenative.config import _max_allowed_params
44
from securenative.event_manager import EventManager
5+
from securenative.logger import sn_logging
56
from securenative.sdk_options import SecureNativeOptions
67
from securenative.utils import verify_signature
78

@@ -14,12 +15,21 @@ def __init__(self, api_key, options=SecureNativeOptions()):
1415
self._api_key = api_key
1516
self._options = options
1617
self._event_manager = EventManager(self._api_key, self._options)
18+
if self._options and self._options.debug_mode:
19+
enable_sn_logging = self._options.debug_mode
20+
sn_logging("sn logging was activated")
1721

1822
def track(self, event):
23+
sn_logging("Track event call")
24+
if not self._options.is_sdk_enabled:
25+
return
1926
_validate_event(event)
2027
self._event_manager.send_async(event, 'collector/api/v1/track')
2128

2229
def verify(self, event):
30+
sn_logging("Verify event call")
31+
if not self._options.is_sdk_enabled:
32+
return _default_verify_result()
2333
_validate_event(event)
2434

2535
try:
@@ -31,10 +41,11 @@ def verify(self, event):
3141
else:
3242
return _default_verify_result()
3343

34-
except Exception as ex:
44+
except Exception:
3545
return _default_verify_result
3646

3747
def verify_webhook(self, hmac_header, body):
48+
sn_logging("Verify webhook was called")
3849
return verify_signature(self._api_key, body, hmac_header)
3950

4051
def flush(self):

securenative/sdk_options.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33

44

55
class SecureNativeOptions:
6-
def __init__(self, api_url=_securenative_prod, interval=1000, max_events=1000, timeout=1500, auto_send=True):
6+
def __init__(self, api_url=_securenative_prod, interval=1000, max_events=1000, timeout=1500, auto_send=True,
7+
is_sdk_enabled=True, debug_mode=False):
78
super().__init__()
89
self.timeout = timeout
910
self.max_events = max_events
1011
self.api_url = api_url
1112
self.interval = interval
1213
self.auto_send = auto_send
14+
self.is_sdk_enabled = is_sdk_enabled
15+
self.debug_mode = debug_mode

securenative/singleton.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import securenative
2+
from securenative.logger import sn_logging
13
from securenative.sdk import SecureNative
24
from securenative.sdk_options import SecureNativeOptions
35

@@ -8,6 +10,11 @@ def init(api_key, options=SecureNativeOptions()):
810
global _sn_sdk
911
if _sn_sdk is None:
1012
_sn_sdk = SecureNative(api_key, options)
13+
securenative.logger.enable_sn_logging = options.debug_mode
14+
else:
15+
sn_logging('This SDK was already initialized.')
16+
raise ValueError(
17+
u'This SDK was already initialized.')
1118

1219

1320
def track(event):

securenative/sn_crypto.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from binascii import unhexlify, hexlify
44

55
BLOCK_SIZE = 16
6-
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(s) % BLOCK_SIZE)
76

87

98
def encrypt(text, cipherKey):
@@ -18,4 +17,8 @@ def decrypt(encrypted, cipherKey):
1817
iv = content[:BLOCK_SIZE]
1918
cipherText = content[BLOCK_SIZE:]
2019
aes = AES.new(cipherKey, AES.MODE_CBC, iv)
21-
return aes.decrypt(cipherText).decode('utf-8').strip()
20+
return aes.decrypt(cipherText).decode('utf-8').strip()
21+
22+
23+
def pad(s):
24+
return s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(s) % BLOCK_SIZE)

0 commit comments

Comments
 (0)