tenkyu 0.2.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.
- tenkyu-0.2.0/LICENSE +21 -0
- tenkyu-0.2.0/PKG-INFO +334 -0
- tenkyu-0.2.0/README.md +311 -0
- tenkyu-0.2.0/pyproject.toml +44 -0
- tenkyu-0.2.0/setup.cfg +4 -0
- tenkyu-0.2.0/src/tenkyu/__init__.py +18 -0
- tenkyu-0.2.0/src/tenkyu/__main__.py +5 -0
- tenkyu-0.2.0/src/tenkyu/audit.py +135 -0
- tenkyu-0.2.0/src/tenkyu/cli.py +202 -0
- tenkyu-0.2.0/src/tenkyu/data/sample_signups.csv +940 -0
- tenkyu-0.2.0/src/tenkyu/decision_log.py +74 -0
- tenkyu-0.2.0/src/tenkyu/guard.py +279 -0
- tenkyu-0.2.0/src/tenkyu/http.py +128 -0
- tenkyu-0.2.0/src/tenkyu/signals.py +147 -0
- tenkyu-0.2.0/src/tenkyu/sqlite_store.py +133 -0
- tenkyu-0.2.0/src/tenkyu/store.py +80 -0
- tenkyu-0.2.0/src/tenkyu/testing.py +150 -0
- tenkyu-0.2.0/src/tenkyu/webhooks.py +92 -0
- tenkyu-0.2.0/src/tenkyu.egg-info/PKG-INFO +334 -0
- tenkyu-0.2.0/src/tenkyu.egg-info/SOURCES.txt +39 -0
- tenkyu-0.2.0/src/tenkyu.egg-info/dependency_links.txt +1 -0
- tenkyu-0.2.0/src/tenkyu.egg-info/entry_points.txt +2 -0
- tenkyu-0.2.0/src/tenkyu.egg-info/requires.txt +3 -0
- tenkyu-0.2.0/src/tenkyu.egg-info/top_level.txt +1 -0
- tenkyu-0.2.0/tests/test_audit.py +138 -0
- tenkyu-0.2.0/tests/test_benchmark.py +85 -0
- tenkyu-0.2.0/tests/test_cli.py +122 -0
- tenkyu-0.2.0/tests/test_conformance.py +114 -0
- tenkyu-0.2.0/tests/test_credits_lists.py +94 -0
- tenkyu-0.2.0/tests/test_custom_signals.py +43 -0
- tenkyu-0.2.0/tests/test_docs.py +18 -0
- tenkyu-0.2.0/tests/test_engine_parity.py +50 -0
- tenkyu-0.2.0/tests/test_fuzz.py +137 -0
- tenkyu-0.2.0/tests/test_guard.py +253 -0
- tenkyu-0.2.0/tests/test_http.py +153 -0
- tenkyu-0.2.0/tests/test_log_report.py +75 -0
- tenkyu-0.2.0/tests/test_readme.py +83 -0
- tenkyu-0.2.0/tests/test_stores.py +381 -0
- tenkyu-0.2.0/tests/test_v2_hardening.py +117 -0
- tenkyu-0.2.0/tests/test_v3_defenses.py +185 -0
- tenkyu-0.2.0/tests/test_webhooks.py +104 -0
tenkyu-0.2.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tenkyu contributors
|
|
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.
|
tenkyu-0.2.0/PKG-INFO
ADDED
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tenkyu
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Stop free-tier and credit farming in AI apps: explainable signup scoring.
|
|
5
|
+
Author: Tenkyu contributors
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/tenkyuai/tenkyu
|
|
8
|
+
Project-URL: Source, https://github.com/tenkyuai/tenkyu
|
|
9
|
+
Project-URL: Issues, https://github.com/tenkyuai/tenkyu/issues
|
|
10
|
+
Keywords: abuse,fraud,free-tier,credits,signup,ai
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Topic :: Security
|
|
17
|
+
Requires-Python: >=3.11
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: pytest; extra == "dev"
|
|
22
|
+
Dynamic: license-file
|
|
23
|
+
|
|
24
|
+
# Tenkyu
|
|
25
|
+
|
|
26
|
+
**Stop free-tier and credit farming in your AI app.** Tenkyu scores every signup and tells you whether to `allow`, `review`, or `block` it, and says exactly why.
|
|
27
|
+
|
|
28
|
+
AI products give away free credits, and people farm them: one person, fifty throwaway emails. Tenkyu is a small Python library that catches the common patterns before you pay for the tokens.
|
|
29
|
+
|
|
30
|
+
> Status: early (v0.2, alpha). Not on PyPI yet: install from GitHub with `pip install git+https://github.com/tenkyuai/tenkyu`. Heuristics only, no machine learning, no network calls.
|
|
31
|
+
|
|
32
|
+
## First, find out what you already lost
|
|
33
|
+
|
|
34
|
+
Export your signups to CSV (an email column and a time column; IP, device id and user agent help) and replay them. Nothing leaves your machine. Try it on the bundled synthetic sample first:
|
|
35
|
+
|
|
36
|
+
```text
|
|
37
|
+
$ python -m tenkyu audit --sample --grant 3
|
|
38
|
+
Using the bundled sample (synthetic signups with known farms, not real users).
|
|
39
|
+
Audited 939 signups (replayed in time order, nothing left this machine)
|
|
40
|
+
allow 689
|
|
41
|
+
review 11
|
|
42
|
+
block 239
|
|
43
|
+
Flagged for review or block: 26.6% (review is a softer check; some honest users land there)
|
|
44
|
+
Free credits these decisions would have withheld: $741.75 at $3 per grant
|
|
45
|
+
Main reason per flagged signup:
|
|
46
|
+
110 this email (or an alias of it) already signed up
|
|
47
|
+
51 N similar-looking emails from this IP
|
|
48
|
+
14 N signups from this IP in Ns
|
|
49
|
+
14 N signups from this IP in Nh
|
|
50
|
+
5 N accounts on this device
|
|
51
|
+
4 automation-style user agent (python-requests)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Then run it on your own export: `python -m tenkyu audit signups.csv --grant 3 --out flagged.csv`. The screen shows counts only; `flagged.csv` holds the flagged rows of your export with Tenkyu's action and reason. Common column names are matched (`created_at`, `timestamp`, `signup_ip`, `visitor_id`, `primary_email_address` and more), unused columns are listed, and `--link card=card_fingerprint` joins accounts on any column you have.
|
|
55
|
+
|
|
56
|
+
## Quickstart
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
from tenkyu import Guard, Signup
|
|
60
|
+
|
|
61
|
+
guard = Guard() # in-memory; reuse one Guard for the life of your process
|
|
62
|
+
UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) Chrome/120.0 Safari/537.36"
|
|
63
|
+
|
|
64
|
+
first = guard.check(Signup(email="john@gmail.com", ip="203.0.113.7", device_id="dev-1", user_agent=UA))
|
|
65
|
+
print(first.action, first.reasons) # allow []
|
|
66
|
+
|
|
67
|
+
# Same person, new alias, hoping for another batch of free credits:
|
|
68
|
+
again = guard.check(Signup(email="j.o.h.n+free2@gmail.com", ip="203.0.113.7", device_id="dev-1", user_agent=UA))
|
|
69
|
+
print(again.action, again.reasons) # block ['this email (or an alias of it) already signed up']
|
|
70
|
+
|
|
71
|
+
if again.action != "allow":
|
|
72
|
+
print("withhold free credits; send to manual review" if again.action == "review" else "reject signup")
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Install from source: `pip install -e .` (no dependencies; Python 3.11 or newer, tested on 3.11, 3.12 and 3.13).
|
|
76
|
+
|
|
77
|
+
## Try it
|
|
78
|
+
|
|
79
|
+
No code needed to see it work:
|
|
80
|
+
|
|
81
|
+
```
|
|
82
|
+
python -m tenkyu demo
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
```text
|
|
86
|
+
scene email action score credits main reason
|
|
87
|
+
------------ ------------------------- ------ ----- ------- -----------
|
|
88
|
+
honest user ana@acme.io ALLOW 0.00 100% -
|
|
89
|
+
alias farmer john@gmail.com ALLOW 0.00 100% -
|
|
90
|
+
alias farmer j.o.h.n+free1@gmail.com BLOCK 0.90 0% this email (or an alias of it) already signed up
|
|
91
|
+
alias farmer john+free2@googlemail.com BLOCK 0.95 0% this email (or an alias of it) already signed up
|
|
92
|
+
bot burst bot0@acme.io REVIEW 0.45 25% automation-style user agent (python-requests)
|
|
93
|
+
bot burst user1x@mailinator.com BLOCK 0.89 0% disposable email domain (mailinator.com)
|
|
94
|
+
bot burst bot2@acme.io BLOCK 0.97 0% 3 signups from this IP in 60s
|
|
95
|
+
bot burst user3x@mailinator.com BLOCK 1.00 0% 4 signups from this IP in 60s
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Score a single signup from a shell or a script (`--db` remembers signups between runs; exit code is 0 allow, 10 review, 20 block, 3 if the `--db` file cannot be opened):
|
|
99
|
+
|
|
100
|
+
```
|
|
101
|
+
python -m tenkyu score --email j.o.h.n+x@gmail.com --ip 203.0.113.7 --db tenkyu.db
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## What it checks
|
|
105
|
+
|
|
106
|
+
| Signal | Catches | Score |
|
|
107
|
+
|---|---|---|
|
|
108
|
+
| `alias_reuse` | `john@gmail.com`, `j.o.h.n+x@gmail.com`, `john@googlemail.com` are one person | 0.90 |
|
|
109
|
+
| `disposable_email` | Mailinator-style throwaway domains (and subdomains) | 0.80 |
|
|
110
|
+
| `pattern_emails` | `farmer1@`, `farmer2@`, `farmer3@` from one IP | 0.75 |
|
|
111
|
+
| `ip_burst` | 3+ signups from one IP in 60 seconds | 0.90 |
|
|
112
|
+
| `ip_velocity` | 3+ (0.50) or 6+ (0.90) signups from one IP in an hour | 0.50-0.90 |
|
|
113
|
+
| `device_reuse` | 2 (0.50) or 3+ (0.65) accounts on one device id | 0.50-0.65 |
|
|
114
|
+
| `automation_agent` | curl, python-requests, headless Chrome, Selenium, and similar | 0.45 |
|
|
115
|
+
| `no_user_agent` / `invalid_email` | missing or malformed input | 0.25 / 0.50 |
|
|
116
|
+
|
|
117
|
+
IPv6 addresses are counted per /64 network, because one network hands out 2^64 addresses and a farmer could otherwise rotate for free. `::ffff:1.2.3.4` counts as `1.2.3.4`.
|
|
118
|
+
|
|
119
|
+
Scores combine as a noisy-or: independent weak signals add up, one strong signal is enough on its own. Score under `0.35` is `allow`, `0.35` to `0.7` is `review`, `0.7` and up is `block`. Every `Decision` carries the list of reasons, so you can show them to your support team or log them.
|
|
120
|
+
|
|
121
|
+
## How well does it work?
|
|
122
|
+
|
|
123
|
+
Short answer: well on lazy farming, not at all on determined farming. We measured it on a **synthetic** data set that we generated ourselves: 2,000 real users and 200 farmers opening 863 extra accounts across five strategies, over one simulated week. "Flagged" means `review` or `block`. This shows how Tenkyu compares with naive rules on behaviours we imagined; it is **not** a measurement of real-world accuracy. Reproduce it with `PYTHONPATH=src python benchmarks/farming_bench.py`.
|
|
124
|
+
|
|
125
|
+
| Method | Precision | Recall | F1 | Real users wrongly flagged |
|
|
126
|
+
|---|---|---|---|---|
|
|
127
|
+
| exact email dedupe | 0.81 | 0.01 | 0.03 | 0.0% |
|
|
128
|
+
| email dedupe + disposable list | 0.81 | 0.20 | 0.32 | 0.0% |
|
|
129
|
+
| Tenkyu (default) | 0.84 | 0.81 | 0.82 | 3.7% |
|
|
130
|
+
|
|
131
|
+
Share of extra accounts caught, by farming strategy:
|
|
132
|
+
|
|
133
|
+
| Strategy | Exact email dedupe | Tenkyu |
|
|
134
|
+
|---|---|---|
|
|
135
|
+
| alias gmail | 3% | 100% |
|
|
136
|
+
| bot burst | 0% | 100% |
|
|
137
|
+
| disposable | 0% | 96% |
|
|
138
|
+
| lookalike | 3% | 78% |
|
|
139
|
+
| residential | 0% | 0% |
|
|
140
|
+
|
|
141
|
+
Read the last row: farmers who use a fresh home IP, a fresh device and an unrelated Gmail address for every account get through completely. That is the "honest limits" section below in numbers. None of the 2,000 real users were blocked outright; 75 (3.7%) went to `review`, and every one was a person sharing a device id with other accounts (a household computer, in this data). Shared device ids alone never reach `block`, by design: one weak signal should not punish a real person. The cost is that a farmer who only reuses one device gets `review`, which releases 25% of the free credits, not 0. Shared office IPs did not trigger anything.
|
|
142
|
+
|
|
143
|
+
The headline F1 depends on how many farmers are the hard kind, and we chose that mix. If half of the farmers used the residential strategy instead of 15%, F1 would be about 0.58, not 0.82 (`PYTHONPATH=src python benchmarks/sensitivity.py`). The same script shows that in this data the device signal adds under one point of recall (0.806 vs 0.798 with a fresh device id on every signup) and accounts for all of the 75 review cases, so it is the signal to doubt first. The data is ours, so this says how the rules behave on our assumptions, not how much a shared device id is worth in your traffic.
|
|
144
|
+
|
|
145
|
+
## Tuning
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
guard = Guard(review_at=0.4, block_at=0.8) # thresholds
|
|
149
|
+
decision = guard.check(signup, record=False) # dry run: score without remembering the signup
|
|
150
|
+
print(decision.to_dict()) # JSON-friendly, includes every signal
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Add your own signal (more in [`docs/signals.md`](https://github.com/tenkyuai/tenkyu/blob/main/docs/signals.md)). It receives `(signup, store, now)` and returns a `Signal` or `None`; a signal that raises is ignored, so it can never break your signup flow:
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
from tenkyu import Signal
|
|
157
|
+
def blocked_country(signup, store, now):
|
|
158
|
+
return Signal("country", 0.6, "signup from an unsupported region") if getattr(signup, "country", None) == "XX" else None
|
|
159
|
+
guard = Guard(extra_signals=[blocked_country])
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## Using it behind a web framework
|
|
163
|
+
|
|
164
|
+
`signup_from_request` builds a `Signup` from your request and gets the client IP right. Full FastAPI and Flask handlers are in [`examples/`](https://github.com/tenkyuai/tenkyu/tree/main/examples/), including how to pass a Fingerprint/Stytch/Castle visitor id as `device_id`.
|
|
165
|
+
|
|
166
|
+
```python
|
|
167
|
+
from tenkyu import Guard, signup_from_request
|
|
168
|
+
|
|
169
|
+
guard = Guard()
|
|
170
|
+
headers = {"User-Agent": "Mozilla/5.0 Chrome/120.0", "X-Forwarded-For": "1.1.1.1, 198.51.100.7"} # a spoofed first entry
|
|
171
|
+
signup = signup_from_request("new@acme.io", headers, remote_addr="10.0.0.1", trusted_proxies=1)
|
|
172
|
+
print(signup.ip, guard.check(signup).action) # 198.51.100.7 allow
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Set `trusted_proxies` to the number of reverse proxies **you** run in front of the app. With the default `0` the `X-Forwarded-For` header is ignored, so nobody can spoof their way around IP limits; with the wrong number you either trust attacker-supplied addresses or see every user as one IP. If honest users get blocked after going live behind a load balancer or CDN, check this setting first: every signup arriving from the proxy's address looks like one IP (in a test, 10 honest users behind one proxy address were blocked from the third on). Entries with a port (`198.51.100.7:51234`, `[2001:db8::7]:443`, as some proxies append) are accepted; an entry that is not an address makes Tenkyu fall back to the socket address. If there is no `X-Forwarded-For`, the standard `Forwarded: for=...` header is read the same way (`for=unknown` or an obfuscated `_token` is not an address, so Tenkyu falls back); when both are present, `X-Forwarded-For` wins. Only set `trusted_proxies` above 0 if your proxies set one of these headers, otherwise a client can send its own.
|
|
176
|
+
|
|
177
|
+
## Graduated credit unlock
|
|
178
|
+
|
|
179
|
+
Don't hand the whole free grant to a borderline signup. Every decision carries a `credit_fraction`: the share of the free credits to release now.
|
|
180
|
+
|
|
181
|
+
```python
|
|
182
|
+
from tenkyu import Guard, Signup
|
|
183
|
+
|
|
184
|
+
guard = Guard(credit_fractions={"review": 0.2}) # defaults: allow 1.0, review 0.25, block 0.0
|
|
185
|
+
d = guard.check(Signup(email="new.user@acme.io", ip="198.51.100.4", user_agent="python-requests/2.31"))
|
|
186
|
+
grant = int(500 * d.credit_fraction) # 500 free credits * 0.2 = 100 now
|
|
187
|
+
print(d.action, grant) # review 100
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Release the rest after the user verifies something that costs a farmer effort (a card on file, a phone number).
|
|
191
|
+
|
|
192
|
+
## Stopping fresh-account farms
|
|
193
|
+
|
|
194
|
+
A farmer with a new inbox, home IP and device for every account shares nothing Tenkyu can link. Three settings, all off by default, limit what such a farm gets:
|
|
195
|
+
|
|
196
|
+
- `links`: pass stable ids your app already has, such as a Stripe card fingerprint or a verified phone. Accounts that share one are flagged as the same person (`link_reuse`). Values are stored only as SHA-256 hashes.
|
|
197
|
+
- `starter_fraction`: signups without a `phone` or `card` link get at most this share of the free grant until they verify.
|
|
198
|
+
- `surge_threshold`: when this many unverified signups were released credits within an hour, the next ones get none until they verify. A real launch day can trip it too, so start high and watch `limit` in the decisions.
|
|
199
|
+
|
|
200
|
+
```python
|
|
201
|
+
from tenkyu import Guard, Signup
|
|
202
|
+
|
|
203
|
+
guard = Guard(starter_fraction=0.5, surge_threshold=50)
|
|
204
|
+
UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) Chrome/120.0 Safari/537.36"
|
|
205
|
+
|
|
206
|
+
new = guard.check(Signup(email="ana@outlook.com", ip="203.0.113.7", user_agent=UA))
|
|
207
|
+
print(new.credit_fraction, new.limit) # 0.5 starter budget until verified
|
|
208
|
+
|
|
209
|
+
paid = guard.check(Signup(email="ben@proton.me", ip="198.51.100.4", user_agent=UA, links={"card": "fp_9xK2"}))
|
|
210
|
+
print(paid.credit_fraction, paid.limit) # 1.0 None
|
|
211
|
+
|
|
212
|
+
again = guard.check(Signup(email="cara@fastmail.com", ip="192.0.2.55", user_agent=UA, links={"card": "fp_9xK2"}))
|
|
213
|
+
print(again.action, again.reasons[0]) # block shares a card with another account
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
## Trying it on live traffic: shadow mode
|
|
217
|
+
|
|
218
|
+
`Guard(shadow=True)` scores and records every signup but always returns `allow` with the full grant. The real decision is in `decision.shadow_action`. Add a `DecisionLog` and read the result with `tenkyu report`. The log keeps a short hash of each email and never the raw email, IP or device id.
|
|
219
|
+
|
|
220
|
+
```python
|
|
221
|
+
from tenkyu import DecisionLog, Guard
|
|
222
|
+
|
|
223
|
+
guard = Guard(shadow=True, log=DecisionLog("decisions.jsonl"))
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
After a week or two:
|
|
227
|
+
|
|
228
|
+
```text
|
|
229
|
+
$ python -m tenkyu report decisions.jsonl --grant 3
|
|
230
|
+
5 signups (shadow mode: nobody was refused; this is what Tenkyu would have done)
|
|
231
|
+
allow 2 40.0%
|
|
232
|
+
review 0 0.0%
|
|
233
|
+
block 3 60.0%
|
|
234
|
+
Same inbox signing up again: 2
|
|
235
|
+
Free grants withheld from flagged signups: 3 = $9.00 at $3 per grant
|
|
236
|
+
Top reasons:
|
|
237
|
+
2 this email (or an alias of it) already signed up
|
|
238
|
+
1 N signups from this IP in Ns
|
|
239
|
+
...
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
## Clerk and Supabase webhooks
|
|
243
|
+
|
|
244
|
+
If Clerk or Supabase Auth creates your users, score them from the signup webhook. `verify_webhook` checks the signature (Svix and Standard Webhooks, stdlib only) and rejects stale or forged requests. The webhook fires after the account exists, so use `credit_fraction` to decide how many free credits to grant. A verified phone and OAuth account ids are passed as `links` automatically.
|
|
245
|
+
|
|
246
|
+
```python
|
|
247
|
+
import json
|
|
248
|
+
|
|
249
|
+
from tenkyu import Guard
|
|
250
|
+
from tenkyu.webhooks import WebhookError, signup_from_clerk, verify_webhook
|
|
251
|
+
|
|
252
|
+
guard = Guard(shadow=True) # start in shadow mode; switch off once the report looks right
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def on_clerk_webhook(headers, body: bytes, secret: str):
|
|
256
|
+
try:
|
|
257
|
+
verify_webhook(secret, headers, body)
|
|
258
|
+
except WebhookError:
|
|
259
|
+
return 400, 0
|
|
260
|
+
signup = signup_from_clerk(json.loads(body))
|
|
261
|
+
if signup is None: # not a user.created event
|
|
262
|
+
return 200, 0
|
|
263
|
+
decision = guard.check(signup)
|
|
264
|
+
return 200, int(500 * decision.credit_fraction) # 500 = your free grant
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
For Supabase, point a database webhook at INSERT on `auth.users` and use `signup_from_supabase(payload)` the same way.
|
|
268
|
+
|
|
269
|
+
## Keeping the disposable-email list fresh
|
|
270
|
+
|
|
271
|
+
Tenkyu bundles a small starter list. Throwaway services rotate domains constantly, so load a fresher list yourself. Tenkyu does not bundle third-party lists, so check the license of whichever one you download.
|
|
272
|
+
|
|
273
|
+
```python
|
|
274
|
+
# not run in tests: needs a file you downloaded
|
|
275
|
+
from tenkyu import Guard, load_domains
|
|
276
|
+
|
|
277
|
+
guard = Guard(extra_disposable=load_domains("disposable-domains.txt")) # one domain per line, # comments ok
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
## Surviving restarts
|
|
281
|
+
|
|
282
|
+
The default store lives in memory, so a restart forgets everyone. `SQLiteStore` keeps the history in a file (standard library only), and several threads or processes on one machine can share it. Each check reads the history and records the signup as one step, so a farmer sending many aliases of one person in parallel gets only the first one through. If another process holds the file's write lock longer than `SQLiteStore(path, timeout=5.0)` seconds, `check` raises `sqlite3.OperationalError` (the CLI prints one line and exits 3). It never quietly allows the signup, so decide in your handler whether to fail open or closed, and lower `timeout` if a signup request must not wait that long.
|
|
283
|
+
|
|
284
|
+
```python
|
|
285
|
+
import os, tempfile
|
|
286
|
+
from tenkyu import Guard, SQLiteStore, Signup
|
|
287
|
+
|
|
288
|
+
path = os.path.join(tempfile.mkdtemp(), "tenkyu.db") # in your app: "tenkyu.db"
|
|
289
|
+
Guard(store=SQLiteStore(path)).check(Signup(email="john@gmail.com", ip="203.0.113.7"))
|
|
290
|
+
|
|
291
|
+
# ...your app restarts; a new Guard on the same file still knows john:
|
|
292
|
+
again = Guard(store=SQLiteStore(path)).check(Signup(email="j.o.h.n+2@gmail.com", ip="198.51.100.9"))
|
|
293
|
+
print(again.action) # block
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
## What the settings cost and save
|
|
297
|
+
|
|
298
|
+
Flags are not the whole story; what matters is how many free grants farmers walk away with. On the same synthetic data (every extra account a farmer opens would get one full grant without Tenkyu):
|
|
299
|
+
|
|
300
|
+
| Setting | Free grants farmers' extra accounts got | ...of which residential | Honest users' average grant | Honest users with the full grant |
|
|
301
|
+
|---|---|---|---|---|
|
|
302
|
+
| Tenkyu (default) | 20% | 100% | 97% | 96% |
|
|
303
|
+
| Guard.for_ai_apps() | 10% | 49% | 49% | 0% |
|
|
304
|
+
| Guard.for_ai_apps() + card link | 0% | 0% | 97% | 96% |
|
|
305
|
+
|
|
306
|
+
- The default setting catches cheap tricks but gives residential farmers (new inbox, home IP and device each time) everything.
|
|
307
|
+
- `Guard.for_ai_apps()` halves what residential farmers get, but honest users also start at half a grant until they verify a phone or card. Unlock the rest on verification.
|
|
308
|
+
- The card-link row assumes every signup carries a card fingerprint, each farmer reuses one card, and each honest user has their own. It only applies to apps that take a card. Run `PYTHONPATH=src python benchmarks/farming_bench.py` to reproduce.
|
|
309
|
+
|
|
310
|
+
## When not to use Tenkyu
|
|
311
|
+
|
|
312
|
+
- **You already use Clerk.** Its [subaddress blocking](https://clerk.com/docs/guides/protect/protections/block-email-subaddresses) and [disposable-email blocking](https://clerk.com/docs/guides/protect/protections/block-disposable-email-addresses) are on every plan and off by default. Turn them on first; that covers two of Tenkyu's signals with no code.
|
|
313
|
+
- **You want a hosted service with device fingerprinting.** [Kaidn](https://pypi.org/project/kaidn/) is a hosted API with a key that returns allow, review or block with reasons, and it lists a free tier of 10,000 events a month.
|
|
314
|
+
|
|
315
|
+
Tenkyu's difference is narrow: it runs inside your process, needs no key, makes no network calls, and takes your own checks as plain functions. Checked against those pages on 2026-11-18; vendors change plans often.
|
|
316
|
+
|
|
317
|
+
## Honest limits
|
|
318
|
+
|
|
319
|
+
- It is a speed bump for farmers, not a wall. At default settings, a farmer who uses a new inbox, home IP and device for every account gets every grant (see "What the settings cost and save"). `Guard.for_ai_apps()` halves that at a cost to honest users; only a link the farmer must reuse (a card, a verified phone) closes it. No heuristic library stops a patient farmer with real identities.
|
|
320
|
+
- Clerk and Supabase webhooks carry no visitor IP, so IP signals are off on that path.
|
|
321
|
+
- The default store is in memory and per process. `SQLiteStore` survives restarts and is shared by processes on one machine, but several servers still each see only their own traffic. A networked store (Redis/Postgres) is on the roadmap; the design note is in [`docs/shared-store.md`](https://github.com/tenkyuai/tenkyu/blob/main/docs/shared-store.md).
|
|
322
|
+
- The disposable-domain list is small and bundled. Extend `tenkyu.signals.DISPOSABLE_DOMAINS` for your needs.
|
|
323
|
+
- Shared IPs (offices, schools, mobile carriers) can look like bursts. Use `review`, not `block`, for borderline cases, and tune the thresholds.
|
|
324
|
+
- Never rely on one signal to punish a real person. Review queues exist for a reason.
|
|
325
|
+
|
|
326
|
+
## Development
|
|
327
|
+
|
|
328
|
+
```
|
|
329
|
+
PYTHONPATH=src python -m pytest -q tests
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
See [CONTRIBUTING.md](https://github.com/tenkyuai/tenkyu/blob/main/CONTRIBUTING.md), [CHANGELOG.md](https://github.com/tenkyuai/tenkyu/blob/main/CHANGELOG.md) and [SECURITY.md](https://github.com/tenkyuai/tenkyu/blob/main/SECURITY.md).
|
|
333
|
+
|
|
334
|
+
MIT licensed.
|