Skip to content

Commit a0e7d0e

Browse files
authored
Merge pull request #36 from securenative/dev
Dev
2 parents 09d3634 + 9d30d9e commit a0e7d0e

File tree

11 files changed

+71
-68
lines changed

11 files changed

+71
-68
lines changed

README.md

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ SecureNative can automatically load your config from *securenative.ini* file or
4646
from securenative.securenative import SecureNative
4747

4848

49+
# 1. Config file path is given by environment variable
4950
securenative = SecureNative.init()
51+
52+
# 2. Config file path is specified directly
53+
securenative = SecureNative.init('path/to/securenative.ini')
5054
```
5155
### Option 2: Initialize via API Key
5256

@@ -88,18 +92,8 @@ from securenative.models.user_traits import UserTraits
8892

8993
securenative = SecureNative.get_instance()
9094

91-
context = SecureNative.context_builder().\
92-
with_ip("127.0.0.1").\
93-
with_client_token("SECURED_CLIENT_TOKEN").\
94-
with_headers({"user-agent", "Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405"}).\
95-
build()
96-
97-
event_options = EventOptionsBuilder(EventTypes.LOG_IN).\
98-
with_user_id("1234").\
99-
with_user_traits(UserTraits("Your Name", "[email protected]", "+1234567890")).\
100-
with_context(context).\
101-
with_properties({"prop1": "CUSTOM_PARAM_VALUE", "prop2": True, "prop3": 3}).\
102-
build()
95+
context = SecureNative.context_builder().with_ip("127.0.0.1").with_client_token("SECURED_CLIENT_TOKEN").with_headers({"user-agent", "Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405"}).build()
96+
event_options = EventOptionsBuilder(EventTypes.LOG_IN).with_user_id("1234").with_user_traits(UserTraits("Your Name", "[email protected]", "+1234567890")).with_context(context).with_properties({"prop1": "CUSTOM_PARAM_VALUE", "prop2": True, "prop3": 3}).build()
10397

10498
securenative.track(event_options)
10599
```
@@ -115,14 +109,9 @@ from securenative.models.user_traits import UserTraits
115109

116110
def track(request):
117111
securenative = SecureNative.get_instance()
118-
context = SecureNative.context_builder().from_http_request(request).build()
119112

120-
event_options = EventOptionsBuilder(EventTypes.LOG_IN).\
121-
with_user_id("1234").\
122-
with_user_traits(UserTraits("Your Name", "[email protected]", "+1234567890")).\
123-
with_context(context).\
124-
with_properties({"prop1": "CUSTOM_PARAM_VALUE", "prop2": True, "prop3": 3}).\
125-
build()
113+
context = SecureNative.context_builder().from_http_request(request).build()
114+
event_options = EventOptionsBuilder(EventTypes.LOG_IN).with_user_id("1234").with_user_traits(UserTraits("Your Name", "[email protected]", "+1234567890")).with_context(context).with_properties({"prop1": "CUSTOM_PARAM_VALUE", "prop2": True, "prop3": 3}).build()
126115

127116
securenative.track(event_options)
128117
```
@@ -142,12 +131,7 @@ def track(request):
142131
securenative = SecureNative.get_instance()
143132
context = SecureNative.context_builder().from_http_request(request).build()
144133

145-
event_options = EventOptionsBuilder(EventTypes.LOG_IN).\
146-
with_user_id("1234").\
147-
with_user_traits(UserTraits("Your Name", "[email protected]", "+1234567890")).\
148-
with_context(context).\
149-
with_properties({"prop1": "CUSTOM_PARAM_VALUE", "prop2": True, "prop3": 3}).\
150-
build()
134+
event_options = EventOptionsBuilder(EventTypes.LOG_IN).with_user_id("1234").with_user_traits(UserTraits("Your Name", "[email protected]", "+1234567890")).with_context(context).with_properties({"prop1": "CUSTOM_PARAM_VALUE", "prop2": True, "prop3": 3}).build()
151135

152136
verify_result = securenative.verify(event_options)
153137
verify_result.risk_level # Low, Medium, High

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.2.4
1+
0.2.5

securenative/config/configuration_manager.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import os
2-
from configparser import ConfigParser, NoSectionError
2+
from configparser import ConfigParser
33

44
from securenative.config.configuration_builder import ConfigurationBuilder
5+
from securenative.exceptions.securenative_config_exception import SecureNativeConfigException
56

67

78
class ConfigurationManager(object):
@@ -11,7 +12,10 @@ class ConfigurationManager(object):
1112

1213
@classmethod
1314
def read_resource_file(cls, resource_path):
14-
cls.config.read(resource_path)
15+
try:
16+
cls.config.read(resource_path)
17+
except Exception as e:
18+
raise SecureNativeConfigException("Invalid config file; %s", e)
1519

1620
properties = {}
1721
for key, value in cls.config.defaults().items():
@@ -41,11 +45,10 @@ def _get_env_or_default(cls, properties, key, default):
4145
return default
4246

4347
@classmethod
44-
def load_config(cls):
48+
def load_config(cls, resource_path):
4549
options = ConfigurationBuilder().get_default_securenative_options()
4650

47-
resource_path = cls.DEFAULT_CONFIG_FILE
48-
if os.environ.get(cls.CUSTOM_CONFIG_FILE_ENV_NAME):
51+
if not resource_path:
4952
resource_path = os.environ.get(cls.CUSTOM_CONFIG_FILE_ENV_NAME)
5053

5154
properties = cls.read_resource_file(resource_path)

securenative/event_manager.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -129,23 +129,23 @@ def stop_event_persist(self):
129129
def serialize(obj):
130130
return {
131131
"rid": obj.rid,
132-
"eventType": obj.event_type,
132+
"eventType": obj.event_type if isinstance(obj.event_type, str) else obj.event_type.value,
133133
"userId": obj.user_id,
134134
"userTraits": {
135-
"name": obj.user_traits.name,
136-
"email": obj.user_traits.email,
137-
"phone": obj.user_traits.phone,
138-
"createdAt": obj.user_traits.created_at,
135+
"name": obj.user_traits.name if obj.user_traits else "",
136+
"email": obj.user_traits.email if obj.user_traits else "",
137+
"phone": obj.user_traits.phone if obj.user_traits else "",
138+
"createdAt": obj.user_traits.created_at if obj.user_traits else "",
139139
},
140140
"request": {
141-
"cid": obj.request.cid,
142-
"vid": obj.request.vid,
143-
"fp": obj.request.fp,
144-
"ip": obj.request.ip,
145-
"remoteIp": obj.request.remote_ip,
146-
"method": obj.request.method,
147-
"url": obj.request.url,
148-
"headers": obj.request.headers
141+
"cid": obj.request.cid if obj.request else "",
142+
"vid": obj.request.vid if obj.request else "",
143+
"fp": obj.request.fp if obj.request else "",
144+
"ip": obj.request.ip if obj.request else "",
145+
"remoteIp": obj.request.remote_ip if obj.request else "",
146+
"method": obj.request.method if obj.request else "",
147+
"url": obj.request.url if obj.request else "",
148+
"headers": obj.request.headers if obj.request else None
149149
},
150150
"timestamp": obj.timestamp,
151151
"properties": obj.properties,

securenative/models/sdk_event.py

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

33
from securenative.context.context_builder import ContextBuilder
44
from securenative.models.request_context import RequestContextBuilder
5+
from securenative.models.user_traits import UserTraits
56
from securenative.utils.date_utils import DateUtils
67
from securenative.utils.encryption_utils import EncryptionUtils
78

@@ -14,21 +15,34 @@ def __init__(self, event_options, securenative_options):
1415
else:
1516
self.context = ContextBuilder.default_context_builder().build()
1617

17-
client_token = EncryptionUtils.decrypt(self.context.client_token, securenative_options.api_key)
18+
if self.context.client_token:
19+
client_token = EncryptionUtils.decrypt(self.context.client_token, securenative_options.api_key)
20+
else:
21+
client_token = None
22+
23+
if event_options.user_traits and isinstance(event_options.user_traits, dict):
24+
user_traits = UserTraits(event_options.user_traits.get("name", ""),
25+
event_options.user_traits.get("email", ""),
26+
event_options.user_traits.get("phone", ""),
27+
event_options.user_traits.get("created_at", ""))
28+
elif event_options.user_traits:
29+
user_traits = event_options.user_traits
30+
else:
31+
user_traits = UserTraits()
1832

1933
self.rid = str(uuid.uuid4())
20-
self.event_type = event_options.event.value
21-
self.user_id = event_options.user_id
22-
self.user_traits = event_options.user_traits
34+
self.event_type = event_options.event if event_options.event else ""
35+
self.user_id = event_options.user_id if event_options.user_id else ""
36+
self.user_traits = user_traits
2337
self.request = RequestContextBuilder() \
2438
.with_cid(client_token.cid if client_token else "") \
2539
.with_vid(client_token.vid if client_token else "") \
2640
.with_fp(client_token.fp if client_token else "") \
27-
.with_ip(self.context.ip) \
28-
.with_remote_ip(self.context.remote_ip) \
29-
.with_method(self.context.method) \
30-
.with_url(self.context.url) \
31-
.with_headers(self.context.headers) \
41+
.with_ip(self.context.ip if self.context.ip else "") \
42+
.with_remote_ip(self.context.remote_ip if self.context.remote_ip else "") \
43+
.with_method(self.context.method if self.context.method else "") \
44+
.with_url(self.context.url if self.context.url else "") \
45+
.with_headers(self.context.headers if self.context.headers else None) \
3246
.build()
3347

3448
self.timestamp = DateUtils.to_timestamp(event_options.timestamp)

securenative/securenative.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ def init_with_api_key(cls, api_key):
5454
raise SecureNativeSDKException(u'This SDK was already initialized.')
5555

5656
@classmethod
57-
def init(cls):
58-
options = ConfigurationManager.load_config()
57+
def init(cls, resource_path=None):
58+
options = ConfigurationManager.load_config(resource_path)
5959
return cls.init_with_options(options)
6060

6161
@classmethod

securenative/utils/date_utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ class DateUtils(object):
55

66
@staticmethod
77
def to_timestamp(date):
8-
if not date:
9-
return datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'
10-
return datetime.strptime(date, '%Y-%m-%dT%H:%M:%S.%f'[:-3] + 'Z')
8+
try:
9+
if not date or date is "":
10+
return datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'
11+
return datetime.strptime(date, '%Y-%m-%dT%H:%M:%S.%f'[:-3] + 'Z')
12+
except Exception:
13+
return ""

securenative/utils/version_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ class VersionUtils(object):
22

33
@staticmethod
44
def get_version():
5-
return "0.2.4"
5+
return "0.2.5"

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
long_description_content_type="text/markdown",
2424
install_requires=[
2525
"requests",
26-
"pycrypto",
26+
"pycryptodome",
2727
],
2828
classifiers=[
2929
'Development Status :: 5 - Production/Stable',

tests/api_manager_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class ApiManagerTest(unittest.TestCase):
1919
def setUp(self):
2020
self.context = ContextBuilder(). \
2121
with_ip("127.0.0.1"). \
22-
with_client_token("SECURED_CLIENT_TOKEN"). \
2322
with_headers(
2423
{
2524
"user-agent": "Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405"

0 commit comments

Comments
 (0)