mbuzz 0.1.0__tar.gz → 0.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (26) hide show
  1. {mbuzz-0.1.0 → mbuzz-0.1.1}/PKG-INFO +1 -1
  2. {mbuzz-0.1.0 → mbuzz-0.1.1}/pyproject.toml +1 -1
  3. {mbuzz-0.1.0 → mbuzz-0.1.1}/src/mbuzz/middleware/flask.py +24 -2
  4. mbuzz-0.1.1/src/mbuzz/utils/session_id.py +39 -0
  5. mbuzz-0.1.1/tests/test_session_id.py +146 -0
  6. {mbuzz-0.1.0 → mbuzz-0.1.1}/.gitignore +0 -0
  7. {mbuzz-0.1.0 → mbuzz-0.1.1}/README.md +0 -0
  8. {mbuzz-0.1.0 → mbuzz-0.1.1}/src/mbuzz/__init__.py +0 -0
  9. {mbuzz-0.1.0 → mbuzz-0.1.1}/src/mbuzz/api.py +0 -0
  10. {mbuzz-0.1.0 → mbuzz-0.1.1}/src/mbuzz/client/__init__.py +0 -0
  11. {mbuzz-0.1.0 → mbuzz-0.1.1}/src/mbuzz/client/conversion.py +0 -0
  12. {mbuzz-0.1.0 → mbuzz-0.1.1}/src/mbuzz/client/identify.py +0 -0
  13. {mbuzz-0.1.0 → mbuzz-0.1.1}/src/mbuzz/client/session.py +0 -0
  14. {mbuzz-0.1.0 → mbuzz-0.1.1}/src/mbuzz/client/track.py +0 -0
  15. {mbuzz-0.1.0 → mbuzz-0.1.1}/src/mbuzz/config.py +0 -0
  16. {mbuzz-0.1.0 → mbuzz-0.1.1}/src/mbuzz/context.py +0 -0
  17. {mbuzz-0.1.0 → mbuzz-0.1.1}/src/mbuzz/cookies.py +0 -0
  18. {mbuzz-0.1.0 → mbuzz-0.1.1}/src/mbuzz/middleware/__init__.py +0 -0
  19. {mbuzz-0.1.0 → mbuzz-0.1.1}/src/mbuzz/utils/__init__.py +0 -0
  20. {mbuzz-0.1.0 → mbuzz-0.1.1}/src/mbuzz/utils/identifier.py +0 -0
  21. {mbuzz-0.1.0 → mbuzz-0.1.1}/tests/__init__.py +0 -0
  22. {mbuzz-0.1.0 → mbuzz-0.1.1}/tests/test_api.py +0 -0
  23. {mbuzz-0.1.0 → mbuzz-0.1.1}/tests/test_client.py +0 -0
  24. {mbuzz-0.1.0 → mbuzz-0.1.1}/tests/test_config.py +0 -0
  25. {mbuzz-0.1.0 → mbuzz-0.1.1}/tests/test_context.py +0 -0
  26. {mbuzz-0.1.0 → mbuzz-0.1.1}/tests/test_middleware.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mbuzz
3
- Version: 0.1.0
3
+ Version: 0.1.1
4
4
  Summary: Multi-touch attribution SDK for Python
5
5
  Project-URL: Homepage, https://mbuzz.co
6
6
  Project-URL: Documentation, https://mbuzz.co/docs/getting-started
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "mbuzz"
7
- version = "0.1.0"
7
+ version = "0.1.1"
8
8
  description = "Multi-touch attribution SDK for Python"
9
9
  readme = "README.md"
10
10
  license = "MIT"
@@ -8,6 +8,7 @@ from ..config import config
8
8
  from ..context import RequestContext, set_context, clear_context
9
9
  from ..cookies import VISITOR_COOKIE, SESSION_COOKIE, VISITOR_MAX_AGE, SESSION_MAX_AGE
10
10
  from ..utils.identifier import generate_id
11
+ from ..utils.session_id import generate_deterministic, generate_from_fingerprint
11
12
  from ..client.session import create_session
12
13
 
13
14
 
@@ -55,8 +56,29 @@ def _get_or_create_visitor_id() -> str:
55
56
 
56
57
 
57
58
  def _get_or_create_session_id() -> str:
58
- """Get session ID from cookie or generate new one."""
59
- return request.cookies.get(SESSION_COOKIE) or generate_id()
59
+ """Get session ID from cookie or generate deterministic one."""
60
+ existing = request.cookies.get(SESSION_COOKIE)
61
+ if existing:
62
+ return existing
63
+
64
+ existing_visitor_id = request.cookies.get(VISITOR_COOKIE)
65
+ if existing_visitor_id:
66
+ return generate_deterministic(existing_visitor_id)
67
+ else:
68
+ return generate_from_fingerprint(_get_client_ip(), _get_user_agent())
69
+
70
+
71
+ def _get_client_ip() -> str:
72
+ """Get client IP from request headers."""
73
+ forwarded = request.headers.get("X-Forwarded-For", "")
74
+ if forwarded:
75
+ return forwarded.split(",")[0].strip()
76
+ return request.remote_addr or "unknown"
77
+
78
+
79
+ def _get_user_agent() -> str:
80
+ """Get user agent from request."""
81
+ return request.headers.get("User-Agent", "unknown")
60
82
 
61
83
 
62
84
  def _set_request_context(visitor_id: str, session_id: str) -> None:
@@ -0,0 +1,39 @@
1
+ """Deterministic session ID generation."""
2
+
3
+ import hashlib
4
+ import secrets
5
+ import time
6
+
7
+ SESSION_TIMEOUT_SECONDS = 1800
8
+ SESSION_ID_LENGTH = 64
9
+ FINGERPRINT_LENGTH = 32
10
+
11
+
12
+ def generate_deterministic(visitor_id: str, timestamp: int | None = None) -> str:
13
+ """Generate session ID for returning visitors."""
14
+ if timestamp is None:
15
+ timestamp = int(time.time())
16
+ time_bucket = timestamp // SESSION_TIMEOUT_SECONDS
17
+ raw = f"{visitor_id}_{time_bucket}"
18
+ return hashlib.sha256(raw.encode()).hexdigest()[:SESSION_ID_LENGTH]
19
+
20
+
21
+ def generate_from_fingerprint(
22
+ client_ip: str,
23
+ user_agent: str,
24
+ timestamp: int | None = None
25
+ ) -> str:
26
+ """Generate session ID for new visitors using IP+UA fingerprint."""
27
+ if timestamp is None:
28
+ timestamp = int(time.time())
29
+ fingerprint = hashlib.sha256(
30
+ f"{client_ip}|{user_agent}".encode()
31
+ ).hexdigest()[:FINGERPRINT_LENGTH]
32
+ time_bucket = timestamp // SESSION_TIMEOUT_SECONDS
33
+ raw = f"{fingerprint}_{time_bucket}"
34
+ return hashlib.sha256(raw.encode()).hexdigest()[:SESSION_ID_LENGTH]
35
+
36
+
37
+ def generate_random() -> str:
38
+ """Generate random session ID (fallback)."""
39
+ return secrets.token_hex(32)
@@ -0,0 +1,146 @@
1
+ """Tests for deterministic session ID generation."""
2
+
3
+ import pytest
4
+ from mbuzz.utils.session_id import (
5
+ generate_deterministic,
6
+ generate_from_fingerprint,
7
+ generate_random,
8
+ )
9
+
10
+
11
+ class TestGenerateDeterministic:
12
+ sample_visitor_id = "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2"
13
+ sample_timestamp = 1735500000
14
+
15
+ def test_returns_64_char_hex_string(self):
16
+ result = generate_deterministic(self.sample_visitor_id, self.sample_timestamp)
17
+ assert len(result) == 64
18
+ assert all(c in "0123456789abcdef" for c in result)
19
+
20
+ def test_is_consistent(self):
21
+ result1 = generate_deterministic(self.sample_visitor_id, self.sample_timestamp)
22
+ result2 = generate_deterministic(self.sample_visitor_id, self.sample_timestamp)
23
+ assert result1 == result2
24
+
25
+ def test_same_within_time_bucket(self):
26
+ # bucket = timestamp / 1800
27
+ # 1735500000 / 1800 = 964166
28
+ # 1735500599 / 1800 = 964166 (last second of bucket)
29
+ timestamp1 = 1735500000
30
+ timestamp2 = 1735500001
31
+ timestamp3 = 1735500599
32
+
33
+ result1 = generate_deterministic(self.sample_visitor_id, timestamp1)
34
+ result2 = generate_deterministic(self.sample_visitor_id, timestamp2)
35
+ result3 = generate_deterministic(self.sample_visitor_id, timestamp3)
36
+
37
+ assert result1 == result2
38
+ assert result1 == result3
39
+
40
+ def test_different_across_time_buckets(self):
41
+ timestamp1 = 1735500000
42
+ timestamp2 = 1735501800 # Next bucket
43
+
44
+ result1 = generate_deterministic(self.sample_visitor_id, timestamp1)
45
+ result2 = generate_deterministic(self.sample_visitor_id, timestamp2)
46
+
47
+ assert result1 != result2
48
+
49
+ def test_different_for_different_visitors(self):
50
+ result1 = generate_deterministic("visitor_a", self.sample_timestamp)
51
+ result2 = generate_deterministic("visitor_b", self.sample_timestamp)
52
+
53
+ assert result1 != result2
54
+
55
+
56
+ class TestGenerateFromFingerprint:
57
+ sample_ip = "203.0.113.42"
58
+ sample_user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36"
59
+ sample_timestamp = 1735500000
60
+
61
+ def test_returns_64_char_hex_string(self):
62
+ result = generate_from_fingerprint(
63
+ self.sample_ip, self.sample_user_agent, self.sample_timestamp
64
+ )
65
+ assert len(result) == 64
66
+ assert all(c in "0123456789abcdef" for c in result)
67
+
68
+ def test_is_consistent(self):
69
+ result1 = generate_from_fingerprint(
70
+ self.sample_ip, self.sample_user_agent, self.sample_timestamp
71
+ )
72
+ result2 = generate_from_fingerprint(
73
+ self.sample_ip, self.sample_user_agent, self.sample_timestamp
74
+ )
75
+ assert result1 == result2
76
+
77
+ def test_same_within_time_bucket(self):
78
+ timestamp1 = 1735500000
79
+ timestamp2 = 1735500001
80
+
81
+ result1 = generate_from_fingerprint(
82
+ self.sample_ip, self.sample_user_agent, timestamp1
83
+ )
84
+ result2 = generate_from_fingerprint(
85
+ self.sample_ip, self.sample_user_agent, timestamp2
86
+ )
87
+
88
+ assert result1 == result2
89
+
90
+ def test_different_across_time_buckets(self):
91
+ timestamp1 = 1735500000
92
+ timestamp2 = 1735501800
93
+
94
+ result1 = generate_from_fingerprint(
95
+ self.sample_ip, self.sample_user_agent, timestamp1
96
+ )
97
+ result2 = generate_from_fingerprint(
98
+ self.sample_ip, self.sample_user_agent, timestamp2
99
+ )
100
+
101
+ assert result1 != result2
102
+
103
+ def test_different_for_different_ips(self):
104
+ result1 = generate_from_fingerprint(
105
+ "192.168.1.1", self.sample_user_agent, self.sample_timestamp
106
+ )
107
+ result2 = generate_from_fingerprint(
108
+ "192.168.1.2", self.sample_user_agent, self.sample_timestamp
109
+ )
110
+
111
+ assert result1 != result2
112
+
113
+ def test_different_for_different_user_agents(self):
114
+ result1 = generate_from_fingerprint(
115
+ self.sample_ip, "Mozilla/5.0 Chrome", self.sample_timestamp
116
+ )
117
+ result2 = generate_from_fingerprint(
118
+ self.sample_ip, "Mozilla/5.0 Safari", self.sample_timestamp
119
+ )
120
+
121
+ assert result1 != result2
122
+
123
+
124
+ class TestGenerateRandom:
125
+ def test_returns_64_char_hex_string(self):
126
+ result = generate_random()
127
+ assert len(result) == 64
128
+ assert all(c in "0123456789abcdef" for c in result)
129
+
130
+ def test_returns_unique_ids(self):
131
+ result1 = generate_random()
132
+ result2 = generate_random()
133
+ assert result1 != result2
134
+
135
+
136
+ class TestCrossMethod:
137
+ def test_deterministic_and_fingerprint_produce_different_ids(self):
138
+ visitor_id = "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2"
139
+ ip = "203.0.113.42"
140
+ user_agent = "Mozilla/5.0"
141
+ timestamp = 1735500000
142
+
143
+ deterministic = generate_deterministic(visitor_id, timestamp)
144
+ fingerprint = generate_from_fingerprint(ip, user_agent, timestamp)
145
+
146
+ assert deterministic != fingerprint
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes