Skip to content

Commit 1d2525e

Browse files
author
Inbal Tako
committed
SN-1938 Validate user id
1 parent 4524362 commit 1d2525e

File tree

7 files changed

+42
-142
lines changed

7 files changed

+42
-142
lines changed

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, request.body)

securenative/event_options_builder.py

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

securenative/models/event_options.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
from securenative.exceptions.securenative_invalid_options_exception import SecureNativeInvalidOptionsException
2+
3+
14
class EventOptions(object):
5+
MAX_PROPERTIES_SIZE = 10
26

37
def __init__(self, event, user_id, user_traits=None,
48
context=None, properties=None, timestamp=None):
9+
if properties is not None and len(properties) > self.MAX_PROPERTIES_SIZE:
10+
raise SecureNativeInvalidOptionsException(
11+
"You can have only up to {} custom properties", self.MAX_PROPERTIES_SIZE)
12+
513
self.event = event
614
self.user_id = user_id
715
self.user_traits = user_traits

securenative/securenative.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from securenative.api_manager import ApiManager
22
from securenative.config.configuration_builder import ConfigurationBuilder
33
from securenative.config.configuration_manager import ConfigurationManager
4-
from securenative.context.context_builder import ContextBuilder
54
from securenative.event_manager import EventManager
65
from securenative.exceptions.securenative_config_exception import SecureNativeConfigException
76
from securenative.exceptions.securenative_sdk_Illegal_state_exception import SecureNativeSDKIllegalStateException
@@ -67,14 +66,6 @@ def get_instance(cls):
6766
def get_options(self):
6867
return self._options
6968

70-
@staticmethod
71-
def config_builder():
72-
return ConfigurationBuilder.default_config_builder()
73-
74-
@staticmethod
75-
def context_builder():
76-
return ContextBuilder.default_context_builder()
77-
7869
def track(self, event_options):
7970
return self._api_manager.track(event_options)
8071

tests/api_manager_test.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
from securenative.api_manager import ApiManager
66
from securenative.config.configuration_manager import ConfigurationManager
7-
from securenative.context.context_builder import ContextBuilder
7+
from securenative.context.securenative_context import SecureNativeContext
88
from securenative.enums.event_types import EventTypes
99
from securenative.enums.risk_level import RiskLevel
1010
from securenative.event_manager import EventManager
11-
from securenative.event_options_builder import EventOptionsBuilder
1211
from securenative.exceptions.securenative_invalid_options_exception import SecureNativeInvalidOptionsException
1312
from securenative.models.event_options import EventOptions
1413
from securenative.models.user_traits import UserTraits
@@ -18,12 +17,8 @@
1817
class ApiManagerTest(unittest.TestCase):
1918

2019
def setUp(self):
21-
self.context = ContextBuilder(). \
22-
with_ip("127.0.0.1"). \
23-
with_headers(
24-
{
25-
"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"
26-
}).build()
20+
self.context = SecureNativeContext(ip="127.0.0.1", headers={
21+
"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"})
2722

2823
self.event_options = EventOptions(EventTypes.LOG_IN, "USER_ID",
2924
UserTraits("USER_NAME", "USER_EMAIL", "+12012673412"), context=self.context,
@@ -77,8 +72,7 @@ def test_securenative_invalid_options_exception(self):
7772

7873
try:
7974
with self.assertRaises(SecureNativeInvalidOptionsException):
80-
api_manager.track(EventOptionsBuilder(
81-
EventTypes.LOG_IN).with_properties(properties).build())
75+
api_manager.track(EventOptions(EventTypes.LOG_IN, "User-ID"))
8276
finally:
8377
event_manager.stop_event_persist()
8478

tests/context_builder_test.py

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

33
import requests_mock
44

5-
from securenative.context.context_builder import ContextBuilder
5+
from securenative.context.securenative_context import SecureNativeContext
66

77

88
class ContextBuilderTest(unittest.TestCase):
@@ -21,7 +21,7 @@ def test_create_context_from_request(self):
2121
request.headers = {
2222
"x-securenative": "71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a"}
2323

24-
context = ContextBuilder.from_http_request(request).build()
24+
context = SecureNativeContext.from_http_request(request)
2525

2626
self.assertEqual(context.client_token,
2727
"71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a")
@@ -45,9 +45,9 @@ def test_create_context_from_request_with_cookie(self):
4545
"REMOTE_ADDR": "51.68.201.122"
4646
}
4747
request.cookies = {"_sn":
48-
"71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a"}
48+
"71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a"}
4949

50-
context = ContextBuilder.from_http_request(request).build()
50+
context = SecureNativeContext.from_http_request(request)
5151

5252
self.assertEqual(context.client_token,
5353
"71532c1fad2c7f56118f7969e401f3cf080239140d208e7934e6a530818c37e544a0c2330a487bcc6fe4f662a57f265a3ed9f37871e80529128a5e4f2ca02db0fb975ded401398f698f19bb0cafd68a239c6caff99f6f105286ab695eaf3477365bdef524f5d70d9be1d1d474506b433aed05d7ed9a435eeca357de57817b37c638b6bb417ffb101eaf856987615a77a")
@@ -58,7 +58,7 @@ def test_create_context_from_request_with_cookie(self):
5858
self.assertIsNone(context.body)
5959

6060
def test_create_default_context_builder(self):
61-
context = ContextBuilder.default_context_builder().build()
61+
context = SecureNativeContext()
6262

6363
self.assertIsNone(context.client_token)
6464
self.assertIsNone(context.ip)
@@ -69,15 +69,8 @@ def test_create_default_context_builder(self):
6969
self.assertIsNone(context.body)
7070

7171
def test_create_custom_context_with_context_builder(self):
72-
context = ContextBuilder.default_context_builder(). \
73-
with_url("/some-url"). \
74-
with_client_token("SECRET_TOKEN"). \
75-
with_ip("10.0.0.0"). \
76-
with_body("{ \"name\": \"YOUR_NAME\" }"). \
77-
with_method("Get"). \
78-
with_remote_ip("10.0.0.1"). \
79-
with_headers({"header1": "value1"}). \
80-
build()
72+
context = SecureNativeContext("SECRET_TOKEN", "10.0.0.0", "10.0.0.1", {"header1": "value1"}, "/some-url", "Get",
73+
"{ \"name\": \"YOUR_NAME\" }")
8174

8275
self.assertEqual(context.url, "/some-url")
8376
self.assertEqual(context.client_token, "SECRET_TOKEN")

0 commit comments

Comments
 (0)