ottertrail 0.1.0__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.
- ottertrail-0.1.0/LICENSE +21 -0
- ottertrail-0.1.0/PKG-INFO +146 -0
- ottertrail-0.1.0/README.md +118 -0
- ottertrail-0.1.0/pyproject.toml +95 -0
- ottertrail-0.1.0/pyproject.toml.orig +61 -0
- ottertrail-0.1.0/src/ottertrail/__init__.py +5 -0
- ottertrail-0.1.0/src/ottertrail/_sign_user_id.py +100 -0
- ottertrail-0.1.0/src/ottertrail/py.typed +0 -0
ottertrail-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 The Ottertrail authors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ottertrail
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python SDK for Ottertrail: sign user IDs on your server.
|
|
5
|
+
Keywords: analytics,error-tracking,hmac,identify,ottertrail,web-analytics
|
|
6
|
+
Author: The Ottertrail authors
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
19
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
20
|
+
Classifier: Topic :: Security :: Cryptography
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Requires-Python: >=3.11
|
|
23
|
+
Project-URL: Homepage, https://ottertrail.dev
|
|
24
|
+
Project-URL: Source, https://github.com/ottertrail/ottertrail-python
|
|
25
|
+
Project-URL: Issues, https://github.com/ottertrail/ottertrail-python/issues
|
|
26
|
+
Project-URL: Releases, https://github.com/ottertrail/ottertrail-python/releases
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# ottertrail
|
|
30
|
+
|
|
31
|
+
The Python SDK for [Ottertrail](https://ottertrail.dev), web analytics and error tracking.
|
|
32
|
+
|
|
33
|
+
Version 0.1 has one function, `sign_user_id()`. It signs the ID of a signed-in user on your server, so that Ottertrail can trust that the ID came from you.
|
|
34
|
+
|
|
35
|
+
## Install
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
pip install ottertrail
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Or `uv add ottertrail`. It has no dependencies and includes type hints.
|
|
42
|
+
|
|
43
|
+
## Quick start
|
|
44
|
+
|
|
45
|
+
Ottertrail's tracker can attach a signed-in user's ID to events. Anyone with the project's public key can send any user ID. This SDK signs IDs on your server, so Ottertrail can check them.
|
|
46
|
+
|
|
47
|
+
1. In Ottertrail, open the project's settings. On the General tab, under Keys, press Show beside the secret key, and copy it.
|
|
48
|
+
2. Set `OTTERTRAIL_SECRET_KEY` in your server's environment. Replace `sk_...` with the key you copied, then start or restart your app. Keep the key out of browser code and out of source control:
|
|
49
|
+
|
|
50
|
+
```sh
|
|
51
|
+
export OTTERTRAIL_SECRET_KEY=sk_...
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
3. On your server, sign the signed-in user's ID when you render their page or return their sign-in details:
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from ottertrail import sign_user_id
|
|
58
|
+
|
|
59
|
+
# Reads the key from OTTERTRAIL_SECRET_KEY.
|
|
60
|
+
signature = sign_user_id(user.id)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
4. Update the tracker snippet from the General tab in your page template. Replace `USER_ID` with the signed-in user's ID, and `SIGNATURE` with the signature from step 3. Use your template's HTML escaping for the ID. Leave out both attributes for visitors who are not signed in:
|
|
64
|
+
|
|
65
|
+
```html
|
|
66
|
+
<script defer src="https://your-instance.ottertrail.app/t.js" data-key="pk_..."
|
|
67
|
+
data-user-id="USER_ID" data-user-signature="SIGNATURE"></script>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
In a single-page app, return the user ID and its signature from your server. Once the tracker has loaded, call `window.ottertrail?.identify(userId, signature)` after sign-in, and when a page load restores a signed-in session. Call `window.ottertrail?.reset()` at sign-out.
|
|
71
|
+
|
|
72
|
+
5. Open your site and sign in. The user shows on the project's Users page.
|
|
73
|
+
6. Once your site sends a signature with every user ID, open General in the project's settings. Under Signed user IDs, turn on "Only accept signed user IDs". Events with a missing or wrong signature are still stored, without the user ID.
|
|
74
|
+
7. To check it, sign in as a user who has not used the site before. The user shows on the Users page only when the signature is correct.
|
|
75
|
+
|
|
76
|
+
## `sign_user_id(user_id, secret_key=None)`
|
|
77
|
+
|
|
78
|
+
Returns the signature of `user_id` as 64 lower-case hex characters: the HMAC-SHA256 of the trimmed ID, keyed with the project's secret key.
|
|
79
|
+
|
|
80
|
+
- `user_id`: a string, or an integer from -(2\*\*53 - 1) to 2\*\*53 - 1 (a safe integer in JavaScript). It is trimmed first, as the tracker trims it, with the rules of JavaScript's `trim()`. These are a little different from those of Python's `str.strip()`.
|
|
81
|
+
- `secret_key`: the project's secret key, `sk_` followed by 64 lower-case hex characters, from the project's settings. If you leave it out, the function reads `OTTERTRAIL_SECRET_KEY`.
|
|
82
|
+
|
|
83
|
+
Put the signature in the page beside the ID: in `data-user-signature` on the tracker's script tag, or as the second argument of `ottertrail.identify(id, signature)`.
|
|
84
|
+
|
|
85
|
+
After your site sends signatures, turn on "Only accept signed user IDs" in the project's settings. Events with a missing or wrong signature are still stored, without the user ID.
|
|
86
|
+
|
|
87
|
+
### Example with Flask
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
from flask import Flask, g, render_template_string
|
|
91
|
+
from ottertrail import sign_user_id
|
|
92
|
+
|
|
93
|
+
app = Flask(__name__)
|
|
94
|
+
|
|
95
|
+
PAGE = """<!doctype html>
|
|
96
|
+
<html>
|
|
97
|
+
<head>
|
|
98
|
+
<script defer src="https://your-instance.ottertrail.app/t.js" data-key="pk_..."
|
|
99
|
+
{%- if user %} data-user-id="{{ user.id }}" data-user-signature="{{ signature }}"{% endif %}></script>
|
|
100
|
+
</head>
|
|
101
|
+
<body>Hello</body>
|
|
102
|
+
</html>"""
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@app.get("/")
|
|
106
|
+
def home():
|
|
107
|
+
# Your own sign-in sets g.user.
|
|
108
|
+
user = g.get("user")
|
|
109
|
+
signature = sign_user_id(user.id) if user else None
|
|
110
|
+
# render_template_string() escapes the values.
|
|
111
|
+
return render_template_string(PAGE, user=user, signature=signature)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Run it with the key in the environment:
|
|
115
|
+
|
|
116
|
+
```sh
|
|
117
|
+
OTTERTRAIL_SECRET_KEY=sk_... flask --app server run
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Keep the secret key on the server
|
|
121
|
+
|
|
122
|
+
Anyone who has the secret key can send events with any user ID. Use it only in server code. Never put it in code that runs in a browser, in a page, or in a public repository. The public key (`pk_...`) is the one that goes in the page.
|
|
123
|
+
|
|
124
|
+
### Errors
|
|
125
|
+
|
|
126
|
+
`sign_user_id()` raises a `TypeError` when:
|
|
127
|
+
|
|
128
|
+
- the user ID is not a string or an integer (a `bool` or a `float` is refused too),
|
|
129
|
+
- the secret key is not a string.
|
|
130
|
+
|
|
131
|
+
It raises a `ValueError` when:
|
|
132
|
+
|
|
133
|
+
- the user ID is an integer outside -(2\*\*53 - 1) to 2\*\*53 - 1,
|
|
134
|
+
- the trimmed ID is empty or longer than 200 characters, counted as JavaScript counts them: a character above U+FFFF, such as an emoji, counts as 2,
|
|
135
|
+
- the ID has a control character (such as a line break) or a surrogate,
|
|
136
|
+
- there is no secret key, or it is not `sk_` followed by 64 lower-case hex characters.
|
|
137
|
+
|
|
138
|
+
The error never contains the secret key: not in its message, and not in the local variables of the SDK's code in its traceback, which some error reports show.
|
|
139
|
+
|
|
140
|
+
## Python versions
|
|
141
|
+
|
|
142
|
+
Python 3.11 and later. Each version is tested on Python 3.11, 3.12, 3.13, 3.14, and 3.15.
|
|
143
|
+
|
|
144
|
+
## License
|
|
145
|
+
|
|
146
|
+
[MIT](https://github.com/ottertrail/ottertrail-python/blob/main/LICENSE)
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# ottertrail
|
|
2
|
+
|
|
3
|
+
The Python SDK for [Ottertrail](https://ottertrail.dev), web analytics and error tracking.
|
|
4
|
+
|
|
5
|
+
Version 0.1 has one function, `sign_user_id()`. It signs the ID of a signed-in user on your server, so that Ottertrail can trust that the ID came from you.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pip install ottertrail
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Or `uv add ottertrail`. It has no dependencies and includes type hints.
|
|
14
|
+
|
|
15
|
+
## Quick start
|
|
16
|
+
|
|
17
|
+
Ottertrail's tracker can attach a signed-in user's ID to events. Anyone with the project's public key can send any user ID. This SDK signs IDs on your server, so Ottertrail can check them.
|
|
18
|
+
|
|
19
|
+
1. In Ottertrail, open the project's settings. On the General tab, under Keys, press Show beside the secret key, and copy it.
|
|
20
|
+
2. Set `OTTERTRAIL_SECRET_KEY` in your server's environment. Replace `sk_...` with the key you copied, then start or restart your app. Keep the key out of browser code and out of source control:
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
export OTTERTRAIL_SECRET_KEY=sk_...
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
3. On your server, sign the signed-in user's ID when you render their page or return their sign-in details:
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
from ottertrail import sign_user_id
|
|
30
|
+
|
|
31
|
+
# Reads the key from OTTERTRAIL_SECRET_KEY.
|
|
32
|
+
signature = sign_user_id(user.id)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
4. Update the tracker snippet from the General tab in your page template. Replace `USER_ID` with the signed-in user's ID, and `SIGNATURE` with the signature from step 3. Use your template's HTML escaping for the ID. Leave out both attributes for visitors who are not signed in:
|
|
36
|
+
|
|
37
|
+
```html
|
|
38
|
+
<script defer src="https://your-instance.ottertrail.app/t.js" data-key="pk_..."
|
|
39
|
+
data-user-id="USER_ID" data-user-signature="SIGNATURE"></script>
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
In a single-page app, return the user ID and its signature from your server. Once the tracker has loaded, call `window.ottertrail?.identify(userId, signature)` after sign-in, and when a page load restores a signed-in session. Call `window.ottertrail?.reset()` at sign-out.
|
|
43
|
+
|
|
44
|
+
5. Open your site and sign in. The user shows on the project's Users page.
|
|
45
|
+
6. Once your site sends a signature with every user ID, open General in the project's settings. Under Signed user IDs, turn on "Only accept signed user IDs". Events with a missing or wrong signature are still stored, without the user ID.
|
|
46
|
+
7. To check it, sign in as a user who has not used the site before. The user shows on the Users page only when the signature is correct.
|
|
47
|
+
|
|
48
|
+
## `sign_user_id(user_id, secret_key=None)`
|
|
49
|
+
|
|
50
|
+
Returns the signature of `user_id` as 64 lower-case hex characters: the HMAC-SHA256 of the trimmed ID, keyed with the project's secret key.
|
|
51
|
+
|
|
52
|
+
- `user_id`: a string, or an integer from -(2\*\*53 - 1) to 2\*\*53 - 1 (a safe integer in JavaScript). It is trimmed first, as the tracker trims it, with the rules of JavaScript's `trim()`. These are a little different from those of Python's `str.strip()`.
|
|
53
|
+
- `secret_key`: the project's secret key, `sk_` followed by 64 lower-case hex characters, from the project's settings. If you leave it out, the function reads `OTTERTRAIL_SECRET_KEY`.
|
|
54
|
+
|
|
55
|
+
Put the signature in the page beside the ID: in `data-user-signature` on the tracker's script tag, or as the second argument of `ottertrail.identify(id, signature)`.
|
|
56
|
+
|
|
57
|
+
After your site sends signatures, turn on "Only accept signed user IDs" in the project's settings. Events with a missing or wrong signature are still stored, without the user ID.
|
|
58
|
+
|
|
59
|
+
### Example with Flask
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from flask import Flask, g, render_template_string
|
|
63
|
+
from ottertrail import sign_user_id
|
|
64
|
+
|
|
65
|
+
app = Flask(__name__)
|
|
66
|
+
|
|
67
|
+
PAGE = """<!doctype html>
|
|
68
|
+
<html>
|
|
69
|
+
<head>
|
|
70
|
+
<script defer src="https://your-instance.ottertrail.app/t.js" data-key="pk_..."
|
|
71
|
+
{%- if user %} data-user-id="{{ user.id }}" data-user-signature="{{ signature }}"{% endif %}></script>
|
|
72
|
+
</head>
|
|
73
|
+
<body>Hello</body>
|
|
74
|
+
</html>"""
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@app.get("/")
|
|
78
|
+
def home():
|
|
79
|
+
# Your own sign-in sets g.user.
|
|
80
|
+
user = g.get("user")
|
|
81
|
+
signature = sign_user_id(user.id) if user else None
|
|
82
|
+
# render_template_string() escapes the values.
|
|
83
|
+
return render_template_string(PAGE, user=user, signature=signature)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Run it with the key in the environment:
|
|
87
|
+
|
|
88
|
+
```sh
|
|
89
|
+
OTTERTRAIL_SECRET_KEY=sk_... flask --app server run
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Keep the secret key on the server
|
|
93
|
+
|
|
94
|
+
Anyone who has the secret key can send events with any user ID. Use it only in server code. Never put it in code that runs in a browser, in a page, or in a public repository. The public key (`pk_...`) is the one that goes in the page.
|
|
95
|
+
|
|
96
|
+
### Errors
|
|
97
|
+
|
|
98
|
+
`sign_user_id()` raises a `TypeError` when:
|
|
99
|
+
|
|
100
|
+
- the user ID is not a string or an integer (a `bool` or a `float` is refused too),
|
|
101
|
+
- the secret key is not a string.
|
|
102
|
+
|
|
103
|
+
It raises a `ValueError` when:
|
|
104
|
+
|
|
105
|
+
- the user ID is an integer outside -(2\*\*53 - 1) to 2\*\*53 - 1,
|
|
106
|
+
- the trimmed ID is empty or longer than 200 characters, counted as JavaScript counts them: a character above U+FFFF, such as an emoji, counts as 2,
|
|
107
|
+
- the ID has a control character (such as a line break) or a surrogate,
|
|
108
|
+
- there is no secret key, or it is not `sk_` followed by 64 lower-case hex characters.
|
|
109
|
+
|
|
110
|
+
The error never contains the secret key: not in its message, and not in the local variables of the SDK's code in its traceback, which some error reports show.
|
|
111
|
+
|
|
112
|
+
## Python versions
|
|
113
|
+
|
|
114
|
+
Python 3.11 and later. Each version is tested on Python 3.11, 3.12, 3.13, 3.14, and 3.15.
|
|
115
|
+
|
|
116
|
+
## License
|
|
117
|
+
|
|
118
|
+
[MIT](https://github.com/ottertrail/ottertrail-python/blob/main/LICENSE)
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "ottertrail"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Python SDK for Ottertrail: sign user IDs on your server."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.11"
|
|
9
|
+
dependencies = []
|
|
10
|
+
keywords = [
|
|
11
|
+
"analytics",
|
|
12
|
+
"error-tracking",
|
|
13
|
+
"hmac",
|
|
14
|
+
"identify",
|
|
15
|
+
"ottertrail",
|
|
16
|
+
"web-analytics",
|
|
17
|
+
]
|
|
18
|
+
classifiers = [
|
|
19
|
+
"Development Status :: 3 - Alpha",
|
|
20
|
+
"Intended Audience :: Developers",
|
|
21
|
+
"Operating System :: OS Independent",
|
|
22
|
+
"Programming Language :: Python :: 3",
|
|
23
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
24
|
+
"Programming Language :: Python :: 3.11",
|
|
25
|
+
"Programming Language :: Python :: 3.12",
|
|
26
|
+
"Programming Language :: Python :: 3.13",
|
|
27
|
+
"Programming Language :: Python :: 3.14",
|
|
28
|
+
"Programming Language :: Python :: 3.15",
|
|
29
|
+
"Topic :: Internet :: WWW/HTTP",
|
|
30
|
+
"Topic :: Security :: Cryptography",
|
|
31
|
+
"Typing :: Typed",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
[[project.authors]]
|
|
35
|
+
name = "The Ottertrail authors"
|
|
36
|
+
|
|
37
|
+
[project.urls]
|
|
38
|
+
Homepage = "https://ottertrail.dev"
|
|
39
|
+
Source = "https://github.com/ottertrail/ottertrail-python"
|
|
40
|
+
Issues = "https://github.com/ottertrail/ottertrail-python/issues"
|
|
41
|
+
Releases = "https://github.com/ottertrail/ottertrail-python/releases"
|
|
42
|
+
|
|
43
|
+
[dependency-groups]
|
|
44
|
+
dev = [
|
|
45
|
+
"mypy==2.4.1",
|
|
46
|
+
"pytest==9.2.0",
|
|
47
|
+
"ruff==0.17.6",
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
[build-system]
|
|
51
|
+
requires = ["uv_build==0.13.12"]
|
|
52
|
+
build-backend = "uv_build"
|
|
53
|
+
|
|
54
|
+
[tool.ruff]
|
|
55
|
+
line-length = 100
|
|
56
|
+
target-version = "py311"
|
|
57
|
+
|
|
58
|
+
[tool.ruff.lint]
|
|
59
|
+
select = [
|
|
60
|
+
"E",
|
|
61
|
+
"W",
|
|
62
|
+
"F",
|
|
63
|
+
"I",
|
|
64
|
+
"B",
|
|
65
|
+
"UP",
|
|
66
|
+
"S",
|
|
67
|
+
"C4",
|
|
68
|
+
"SIM",
|
|
69
|
+
"RUF",
|
|
70
|
+
"PT",
|
|
71
|
+
]
|
|
72
|
+
|
|
73
|
+
[tool.ruff.lint.per-file-ignores]
|
|
74
|
+
"tests/**" = [
|
|
75
|
+
"S101",
|
|
76
|
+
"S105",
|
|
77
|
+
"S106",
|
|
78
|
+
]
|
|
79
|
+
|
|
80
|
+
[tool.mypy]
|
|
81
|
+
strict = true
|
|
82
|
+
files = [
|
|
83
|
+
"src",
|
|
84
|
+
"tests",
|
|
85
|
+
]
|
|
86
|
+
|
|
87
|
+
[tool.pytest]
|
|
88
|
+
minversion = "9.0"
|
|
89
|
+
testpaths = ["tests"]
|
|
90
|
+
addopts = [
|
|
91
|
+
"--strict-markers",
|
|
92
|
+
"--strict-config",
|
|
93
|
+
]
|
|
94
|
+
xfail_strict = true
|
|
95
|
+
filterwarnings = ["error"]
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "ottertrail"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Python SDK for Ottertrail: sign user IDs on your server."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
authors = [{ name = "The Ottertrail authors" }]
|
|
9
|
+
requires-python = ">=3.11"
|
|
10
|
+
dependencies = []
|
|
11
|
+
keywords = ["analytics", "error-tracking", "hmac", "identify", "ottertrail", "web-analytics"]
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Development Status :: 3 - Alpha",
|
|
14
|
+
"Intended Audience :: Developers",
|
|
15
|
+
"Operating System :: OS Independent",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
18
|
+
"Programming Language :: Python :: 3.11",
|
|
19
|
+
"Programming Language :: Python :: 3.12",
|
|
20
|
+
"Programming Language :: Python :: 3.13",
|
|
21
|
+
"Programming Language :: Python :: 3.14",
|
|
22
|
+
"Programming Language :: Python :: 3.15",
|
|
23
|
+
"Topic :: Internet :: WWW/HTTP",
|
|
24
|
+
"Topic :: Security :: Cryptography",
|
|
25
|
+
"Typing :: Typed",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
Homepage = "https://ottertrail.dev"
|
|
30
|
+
Source = "https://github.com/ottertrail/ottertrail-python"
|
|
31
|
+
Issues = "https://github.com/ottertrail/ottertrail-python/issues"
|
|
32
|
+
Releases = "https://github.com/ottertrail/ottertrail-python/releases"
|
|
33
|
+
|
|
34
|
+
[dependency-groups]
|
|
35
|
+
dev = ["mypy==2.4.1", "pytest==9.2.0", "ruff==0.17.6"]
|
|
36
|
+
|
|
37
|
+
[build-system]
|
|
38
|
+
requires = ["uv_build==0.13.12"]
|
|
39
|
+
build-backend = "uv_build"
|
|
40
|
+
|
|
41
|
+
[tool.ruff]
|
|
42
|
+
line-length = 100
|
|
43
|
+
target-version = "py311"
|
|
44
|
+
|
|
45
|
+
[tool.ruff.lint]
|
|
46
|
+
select = ["E", "W", "F", "I", "B", "UP", "S", "C4", "SIM", "RUF", "PT"]
|
|
47
|
+
|
|
48
|
+
[tool.ruff.lint.per-file-ignores]
|
|
49
|
+
# Tests use assert, and a key made for tests only.
|
|
50
|
+
"tests/**" = ["S101", "S105", "S106"]
|
|
51
|
+
|
|
52
|
+
[tool.mypy]
|
|
53
|
+
strict = true
|
|
54
|
+
files = ["src", "tests"]
|
|
55
|
+
|
|
56
|
+
[tool.pytest]
|
|
57
|
+
minversion = "9.0"
|
|
58
|
+
testpaths = ["tests"]
|
|
59
|
+
addopts = ["--strict-markers", "--strict-config"]
|
|
60
|
+
xfail_strict = true
|
|
61
|
+
filterwarnings = ["error"]
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import hashlib
|
|
2
|
+
import hmac
|
|
3
|
+
import os
|
|
4
|
+
import re
|
|
5
|
+
|
|
6
|
+
_MAX_USER_ID = 200
|
|
7
|
+
|
|
8
|
+
# As in the JavaScript SDK: a larger number loses digits in a browser.
|
|
9
|
+
_MAX_SAFE_INTEGER = 2**53 - 1
|
|
10
|
+
|
|
11
|
+
# What JavaScript's String.prototype.trim() removes, because the tracker trims the ID with it.
|
|
12
|
+
# Python's str.strip() is different: it also removes U+001C to U+001F and U+0085, and keeps U+FEFF.
|
|
13
|
+
_JS_WHITESPACE = (
|
|
14
|
+
"\t\n\v\f\r \u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a"
|
|
15
|
+
"\u2028\u2029\u202f\u205f\u3000\ufeff"
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
# The Unicode categories Cc (control) and Cs (surrogate). Both are fixed, in every Unicode version.
|
|
19
|
+
_FORBIDDEN = re.compile(r"[\x00-\x1f\x7f-\x9f\ud800-\udfff]")
|
|
20
|
+
|
|
21
|
+
_SECRET_KEY = re.compile(r"sk_[0-9a-f]{64}")
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def sign_user_id(user_id: str | int, secret_key: str | None = None) -> str:
|
|
25
|
+
"""Sign the ID of a signed-in user, so that Ottertrail can trust it.
|
|
26
|
+
|
|
27
|
+
Put the signature in the page beside the ID: in ``data-user-signature`` on the tracker's
|
|
28
|
+
script tag, or as the second argument of ``ottertrail.identify(id, signature)``.
|
|
29
|
+
|
|
30
|
+
Args:
|
|
31
|
+
user_id: The user's ID: a string, or an integer from -(2**53 - 1) to 2**53 - 1. It is
|
|
32
|
+
trimmed, as the tracker trims it.
|
|
33
|
+
secret_key: The project's secret key. If it is None, the function reads the environment
|
|
34
|
+
variable ``OTTERTRAIL_SECRET_KEY``.
|
|
35
|
+
|
|
36
|
+
Returns:
|
|
37
|
+
The HMAC-SHA256 of the trimmed ID, as 64 lower-case hex characters.
|
|
38
|
+
|
|
39
|
+
Raises:
|
|
40
|
+
TypeError: If the ID or the key has the wrong type.
|
|
41
|
+
ValueError: If the ID or the key is not valid, or if there is no key.
|
|
42
|
+
"""
|
|
43
|
+
try:
|
|
44
|
+
text = _clean_user_id(user_id)
|
|
45
|
+
key = _secret_key(secret_key)
|
|
46
|
+
except (TypeError, ValueError) as error:
|
|
47
|
+
# A traceback keeps the local variables of its frames, and some error reports show them.
|
|
48
|
+
# So the error leaves from this frame alone, after the key has left it.
|
|
49
|
+
del secret_key
|
|
50
|
+
raise error.with_traceback(None) from None
|
|
51
|
+
return hmac.new(key, text.encode("utf-8"), hashlib.sha256).hexdigest()
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _clean_user_id(user_id: object) -> str:
|
|
55
|
+
if isinstance(user_id, str):
|
|
56
|
+
text = user_id
|
|
57
|
+
elif isinstance(user_id, int) and not isinstance(user_id, bool):
|
|
58
|
+
if not -_MAX_SAFE_INTEGER <= user_id <= _MAX_SAFE_INTEGER:
|
|
59
|
+
raise ValueError(
|
|
60
|
+
"Ottertrail: a user ID that is an integer must be a safe integer,"
|
|
61
|
+
" from -(2**53 - 1) to 2**53 - 1."
|
|
62
|
+
)
|
|
63
|
+
# int() first: a subclass, such as an IntEnum, can change what str() gives.
|
|
64
|
+
text = str(int(user_id))
|
|
65
|
+
else:
|
|
66
|
+
raise TypeError("Ottertrail: the user ID must be a string or an integer.")
|
|
67
|
+
# The tracker trims the ID before it sends it, so the trimmed text is what the instance checks.
|
|
68
|
+
text = text.strip(_JS_WHITESPACE)
|
|
69
|
+
if not 1 <= _utf16_length(text) <= _MAX_USER_ID:
|
|
70
|
+
raise ValueError(
|
|
71
|
+
f"Ottertrail: the user ID must be 1 to {_MAX_USER_ID} characters after trimming."
|
|
72
|
+
)
|
|
73
|
+
if _FORBIDDEN.search(text):
|
|
74
|
+
raise ValueError(
|
|
75
|
+
"Ottertrail: the user ID must not contain control characters or surrogates."
|
|
76
|
+
)
|
|
77
|
+
return text
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _utf16_length(text: str) -> int:
|
|
81
|
+
"""Count as JavaScript does: a character above U+FFFF is 2."""
|
|
82
|
+
return len(text) + sum(1 for char in text if char > "\uffff")
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
# The messages never include the value: it is a secret.
|
|
86
|
+
def _secret_key(secret_key: object) -> bytes:
|
|
87
|
+
if secret_key is None:
|
|
88
|
+
secret_key = os.environ.get("OTTERTRAIL_SECRET_KEY")
|
|
89
|
+
if secret_key is None or secret_key == "":
|
|
90
|
+
raise ValueError(
|
|
91
|
+
"Ottertrail: no secret key. Pass it to sign_user_id(), or set OTTERTRAIL_SECRET_KEY."
|
|
92
|
+
)
|
|
93
|
+
if not isinstance(secret_key, str):
|
|
94
|
+
raise TypeError("Ottertrail: the secret key must be a string.")
|
|
95
|
+
if not _SECRET_KEY.fullmatch(secret_key):
|
|
96
|
+
raise ValueError(
|
|
97
|
+
"Ottertrail: the secret key is not valid."
|
|
98
|
+
" Use the project's secret key: sk_ followed by 64 lower-case hex characters."
|
|
99
|
+
)
|
|
100
|
+
return secret_key.encode("ascii")
|
|
File without changes
|