Skip to content

Commit 454617b

Browse files
authored
Merge pull request #42 from securenative/dev
SN-1938 Validate user id
2 parents 0eae997 + dc12c50 commit 454617b

19 files changed

+172
-368
lines changed

README.md

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,12 @@ securenative = SecureNative.init_with_api_key("YOUR_API_KEY")
6464
### Option 3: Initialize via ConfigurationBuilder
6565
```python
6666
from securenative.securenative import SecureNative
67+
from securenative.config.securenative_options import SecureNativeOptions
6768

6869

69-
securenative = SecureNative.init_with_options(SecureNative.config_builder().with_api_key("API_KEY").with_max_events(10).with_log_level("ERROR").build())
70+
options = SecureNativeOptions(api_key="YOUR_API_KEY", auto_send=True, interval=10,
71+
api_url="https://api.securenative-stg.com/collector/api/v1")
72+
securenative = SecureNative.init_with_options(options)
7073
```
7174

7275
## Getting SecureNative instance
@@ -75,7 +78,7 @@ Once initialized, sdk will create a singleton instance which you can get:
7578
from securenative.securenative import SecureNative
7679

7780

78-
secureNative = SecureNative.get_instance()
81+
securenative = SecureNative.get_instance()
7982
```
8083

8184
## Tracking events
@@ -85,15 +88,22 @@ instance. Make sure you build event with the EventBuilder:
8588

8689
```python
8790
from securenative.securenative import SecureNative
88-
from securenative.event_options_builder import EventOptionsBuilder
91+
from securenative.context.securenative_context import SecureNativeContext
92+
from securenative.models.event_options import EventOptions
8993
from securenative.enums.event_types import EventTypes
9094
from securenative.models.user_traits import UserTraits
9195

9296

9397
securenative = SecureNative.get_instance()
9498

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()
99+
context = SecureNativeContext(client_token="SECURE_CLIENT_TOKEN",
100+
ip="127.0.0.1",
101+
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"})
102+
event_options = EventOptions(event=EventTypes.LOG_IN,
103+
user_id="1234",
104+
user_traits=UserTraits("Your Name", "[email protected]", "+1234567890"),
105+
context=context,
106+
properties={"custom_param1": "CUSTOM_PARAM_VALUE", "custom_param2": True, "custom_param3": 3})
97107

98108
securenative.track(event_options)
99109
```
@@ -102,16 +112,21 @@ You can also create request context from requests:
102112

103113
```python
104114
from securenative.securenative import SecureNative
105-
from securenative.event_options_builder import EventOptionsBuilder
115+
from securenative.context.securenative_context import SecureNativeContext
116+
from securenative.models.event_options import EventOptions
106117
from securenative.enums.event_types import EventTypes
107118
from securenative.models.user_traits import UserTraits
108119

109120

110121
def track(request):
111122
securenative = SecureNative.get_instance()
112123

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()
124+
context = SecureNativeContext.from_http_request(request)
125+
event_options = EventOptions(event=EventTypes.LOG_IN,
126+
user_id="1234",
127+
user_traits=UserTraits("Your Name", "[email protected]", "+1234567890"),
128+
context=context,
129+
properties={"custom_param1": "CUSTOM_PARAM_VALUE", "custom_param2": True, "custom_param3": 3})
115130

116131
securenative.track(event_options)
117132
```
@@ -122,16 +137,21 @@ def track(request):
122137

123138
```python
124139
from securenative.securenative import SecureNative
125-
from securenative.event_options_builder import EventOptionsBuilder
140+
from securenative.models.event_options import EventOptions
141+
from securenative.context.securenative_context import SecureNativeContext
126142
from securenative.enums.event_types import EventTypes
127143
from securenative.models.user_traits import UserTraits
128144

129145

130146
def track(request):
131147
securenative = SecureNative.get_instance()
132-
context = SecureNative.context_builder().from_http_request(request).build()
133148

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()
149+
context = SecureNativeContext.from_http_request(request)
150+
event_options = EventOptions(event=EventTypes.LOG_IN,
151+
user_id="1234",
152+
user_traits=UserTraits("Your Name", "[email protected]", "+1234567890"),
153+
context=context,
154+
properties={"custom_param1": "CUSTOM_PARAM_VALUE", "custom_param2": True, "custom_param3": 3})
135155

136156
verify_result = securenative.verify(event_options)
137157
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.3.0
1+
0.3.1

securenative/config/configuration_builder.py

Lines changed: 0 additions & 67 deletions
This file was deleted.

securenative/config/configuration_manager.py

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

4-
from securenative.config.configuration_builder import ConfigurationBuilder
4+
from securenative.config.securenative_options import SecureNativeOptions
55
from securenative.exceptions.securenative_config_exception import SecureNativeConfigException
66

77

@@ -32,10 +32,6 @@ def _get_resource_path(cls, env_name):
3232

3333
return os.environ.get(cls.DEFAULT_CONFIG_FILE)
3434

35-
@staticmethod
36-
def config_builder():
37-
return ConfigurationBuilder.default_config_builder()
38-
3935
@classmethod
4036
def _get_env_or_default(cls, properties, key, default):
4137
if os.environ.get(key):
@@ -46,21 +42,25 @@ def _get_env_or_default(cls, properties, key, default):
4642

4743
@classmethod
4844
def load_config(cls, resource_path):
49-
options = ConfigurationBuilder().get_default_securenative_options()
45+
options = SecureNativeOptions()
5046

5147
if not resource_path:
5248
resource_path = os.environ.get(cls.CUSTOM_CONFIG_FILE_ENV_NAME)
5349

5450
properties = cls.read_resource_file(resource_path)
5551

56-
return ConfigurationBuilder(). \
57-
with_api_key(cls._get_env_or_default(properties, "SECURENATIVE_API_KEY", options.api_key)). \
58-
with_api_url(cls._get_env_or_default(properties, "SECURENATIVE_API_URL", options.api_url)). \
59-
with_interval(cls._get_env_or_default(properties, "SECURENATIVE_INTERVAL", options.interval)). \
60-
with_max_events(cls._get_env_or_default(properties, "SECURENATIVE_MAX_EVENTS", options.max_events)). \
61-
with_timeout(cls._get_env_or_default(properties, "SECURENATIVE_TIMEOUT", options.timeout)). \
62-
with_auto_send(cls._get_env_or_default(properties, "SECURENATIVE_AUTO_SEND", options.auto_send)). \
63-
with_disable(cls._get_env_or_default(properties, "SECURENATIVE_DISABLE", options.disable)). \
64-
with_log_level(cls._get_env_or_default(properties, "SECURENATIVE_LOG_LEVEL", options.log_level)). \
65-
with_fail_over_strategy(cls._get_env_or_default(
66-
properties, "SECURENATIVE_FAILOVER_STRATEGY", options.fail_over_strategy))
52+
return SecureNativeOptions(api_key=cls._get_env_or_default(properties, "SECURENATIVE_API_KEY", options.api_key),
53+
api_url=cls._get_env_or_default(properties, "SECURENATIVE_API_URL", options.api_url),
54+
interval=cls._get_env_or_default(properties, "SECURENATIVE_INTERVAL",
55+
options.interval),
56+
max_events=cls._get_env_or_default(properties, "SECURENATIVE_MAX_EVENTS",
57+
options.max_events),
58+
timeout=cls._get_env_or_default(properties, "SECURENATIVE_TIMEOUT", options.timeout),
59+
auto_send=cls._get_env_or_default(properties, "SECURENATIVE_AUTO_SEND",
60+
options.auto_send),
61+
disable=cls._get_env_or_default(properties, "SECURENATIVE_DISABLE", options.disable),
62+
log_level=cls._get_env_or_default(properties, "SECURENATIVE_LOG_LEVEL",
63+
options.log_level),
64+
fail_over_strategy=cls._get_env_or_default(properties,
65+
"SECURENATIVE_FAILOVER_STRATEGY",
66+
options.fail_over_strategy))

securenative/config/securenative_options.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ class SecureNativeOptions(object):
66
def __init__(self, api_key=None, api_url="https://api.securenative.com/collector/api/v1", interval=1000,
77
max_events=1000, timeout=1500, auto_send=True, disable=False, log_level="CRITICAL",
88
fail_over_strategy=FailOverStrategy.FAIL_OPEN.value):
9+
10+
if fail_over_strategy != FailOverStrategy.FAIL_OPEN.value and \
11+
fail_over_strategy != FailOverStrategy.FAIL_CLOSED.value:
12+
self.fail_over_strategy = FailOverStrategy.FAIL_OPEN.value
13+
else:
14+
self.fail_over_strategy = fail_over_strategy
15+
916
self.api_key = api_key
1017
self.api_url = api_url
1118
self.interval = interval
@@ -14,4 +21,3 @@ def __init__(self, api_key=None, api_url="https://api.securenative.com/collector
1421
self.auto_send = auto_send
1522
self.disable = disable
1623
self.log_level = log_level
17-
self.fail_over_strategy = fail_over_strategy

securenative/context/context_builder.py

Lines changed: 0 additions & 68 deletions
This file was deleted.

securenative/context/securenative_context.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
from securenative.utils.request_utils import RequestUtils
2+
from securenative.utils.utils import Utils
3+
4+
15
class SecureNativeContext(object):
26

37
def __init__(self, client_token=None, ip=None, remote_ip=None, headers=None, url=None, method=None, body=None):
@@ -8,3 +12,22 @@ def __init__(self, client_token=None, ip=None, remote_ip=None, headers=None, url
812
self.url = url
913
self.method = method
1014
self.body = body
15+
16+
@staticmethod
17+
def from_http_request(request):
18+
try:
19+
client_token = request.cookies[RequestUtils.SECURENATIVE_COOKIE]
20+
except Exception:
21+
client_token = None
22+
23+
try:
24+
headers = dict(request.headers)
25+
except Exception:
26+
headers = None
27+
28+
if Utils.is_null_or_empty(client_token):
29+
client_token = RequestUtils.get_secure_header_from_request(headers)
30+
31+
return SecureNativeContext(client_token, RequestUtils.get_client_ip_from_request(request),
32+
RequestUtils.get_remote_ip_from_request(request), headers, request.url,
33+
request.method, None)

securenative/event_options_builder.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)