Files
licensing/README.md
T
2026-10-19 17:08:07 -05:00

160 lines
8.3 KiB
Markdown

# amnesia licensing
The native client now integrates this API; see `../client/LICENSING.md` for
sign-in, watermark and endpoint configuration. Restart the service after this
update. Client activation and verification return `server_time` alongside
`expires_at` so the DLL can use a monotonic session deadline. Verification accepts
an optional boolean `renew: true`, extending a still-valid token by at most the
configured client lifetime, capped at license expiry. It never renews expired or
revoked tokens. `POST /api/client/logout` accepts the bearer token plus `hwid` and
deletes that session. These changes require no database migration.
Python 3.10+ licensing API with SQLite persistence and an Apache-compatible WSGI
production server. The sibling `website` project is its account/admin interface.
The Minecraft client and injector are separate and unchanged.
## What it does
- Username/password accounts, immutable sequential numeric UIDs and admin roles.
- Random, single-use license keys with day-based or lifetime access. Time starts
on redemption. Keys are stored as keyed hashes; full keys appear only on issue.
- First successful native activation ties the **account** to one device.
- HWID mismatches are rejected. Only a keyed HMAC of the supplied SHA-256 device
fingerprint is stored, not raw serial numbers or the incoming fingerprint.
- 15-minute opaque client tokens. Every verify checks account status, current
license validity, expiry and the account/device tie. Recheck every 60 seconds;
activate again after expiry. Activation rotates the account's client token.
- Admin revocation, account suspension, HWID resets, user reset requests and an
audit trail. Revocation/reset/suspension invalidates client sessions immediately.
- PBKDF2-SHA256 password hashes with 600,000 iterations and random salts. HttpOnly
SameSite browser sessions, CSRF checks, exact-origin checks, bounded JSON bodies,
parameterized queries and persistent login rate limits.
HWID checks are a licensing restriction, not hardware attestation or anti-tamper.
A modified consumer can bypass local checks; enforcement belongs on this API.
The consumer fingerprint must be stable. The example uses a namespaced SHA-256
of Windows MachineGuid or Linux machine-id; it does not print the raw identifier.
Changing/reinstalling the OS can require an administrator reset.
## Local start
From the parent `amnesia` directory:
```powershell
python website/run-local.py
```
Open http://127.0.0.1:5173 and create the first local administrator. Create a key
under license manager, then register a member and redeem it in my licenses.
No sample users or passwords are created automatically.
## API contract
All POST bodies are JSON. Browser mutations require the configured Origin and,
after sign-in, `X-CSRF-Token` returned by `/api/session`. Client endpoints use
credentials/Bearer tokens instead of browser cookies or CSRF.
Local `--dev` startup accepts `localhost`, `127.0.0.1` and `::1` aliases only
for the same configured HTTP port. Production keeps exact-origin matching.
Restart the licensing server after changing configuration or API code.
| Method | Route | Purpose |
| --- | --- | --- |
| GET | `/api/health` | Readiness |
| GET | `/api/session` | Current account + CSRF |
| POST | `/api/auth/register` | `{username,password}` |
| POST | `/api/auth/login` | `{username,password}` |
| POST | `/api/auth/logout` | End current browser session |
| GET | `/api/account` | Account, licenses, reset request |
| POST | `/api/licenses/redeem` | `{key}` |
| POST | `/api/account/password` | `{current_password,new_password}`; ends all sessions |
| POST | `/api/account/hwid-reset` | `{reason}`; admin approval required |
| GET | `/api/admin/overview` | Counts, latest 500 accounts/licenses, reset requests, latest 100 audit events |
| POST | `/api/admin/licenses` | `{label,duration_days}`; `null` is lifetime; returns full key once |
| POST | `/api/admin/licenses/{id}/revoke` | Revoke issued or redeemed license |
| POST | `/api/admin/accounts/{uid}/reset-hwid` | Unbind device, approve request, end client sessions |
| POST | `/api/admin/accounts/{uid}/status` | `{enabled:true/false}` |
| POST | `/api/client/activate` | `{username,password,hwid,device_name?}`; binds hardware and issues token |
| POST | `/api/client/verify` | `{hwid}` + `Authorization: Bearer <token>` |
`hwid` is exactly 64 hex characters. Activation returns `token`, `expires_at`,
`recheck_after`, `account:{uid,username}` and license details. Verification returns
`valid:true` and the same identity/expiry metadata without returning a new token.
Errors are `{error,code}` with an appropriate HTTP status. Consumers must deny
access on failed verification and must not cache a success beyond token expiry.
Test the standalone consumer **only after redeeming a license**:
```powershell
python licensing/client_example.py --url http://127.0.0.1:8787/api --username your_name
```
The password is prompted, never put in command arguments. Tokens are kept in
memory. There is no offline license mode or client DLL integration yet.
## Apache / Linux VPS
Use the existing TLS certificate for your real domain. The templates use
`amnesia.example.com`; replace it everywhere before enabling the site. A single
Waitress process listens only on `127.0.0.1:8787`; Apache serves the static website
and proxies `/api/`. No inbound API port needs to be opened.
Example for Debian/Ubuntu (run as your VPS administrator):
```sh
sudo apt install python3 python3-venv apache2
sudo useradd --system --home /var/lib/amnesia-licensing --shell /usr/sbin/nologin amnesia
sudo install -d -o amnesia -g amnesia -m 700 /var/lib/amnesia-licensing
# Copy licensing/ and website/ under /srv/amnesia first.
python3 -m venv /srv/amnesia/licensing/.venv
/srv/amnesia/licensing/.venv/bin/pip install -r /srv/amnesia/licensing/requirements.txt
sudo cp /srv/amnesia/licensing/deploy/amnesia.env.example /etc/amnesia-licensing.env
# Edit AMNESIA_ORIGIN to your exact HTTPS origin (no trailing path).
sudo chmod 600 /etc/amnesia-licensing.env
sudo -u amnesia env AMNESIA_DATA=/var/lib/amnesia-licensing /srv/amnesia/licensing/.venv/bin/python /srv/amnesia/licensing/manage.py create-admin --username your_admin_name
sudo cp /srv/amnesia/licensing/deploy/amnesia-licensing.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable --now amnesia-licensing
sudo a2enmod proxy proxy_http headers ssl
# Adapt deploy/apache-amnesia.conf to your domain and existing certificate.
sudo cp /srv/amnesia/licensing/deploy/apache-amnesia.conf /etc/apache2/sites-available/amnesia.conf
sudo a2ensite amnesia
sudo apache2ctl configtest
sudo systemctl reload apache2
```
Keep code and the virtual environment readable by `amnesia`; make only the state
directory writable by that service user. The environment file is read by systemd.
Production startup deliberately disables `/api/setup`. Do not use `--dev` or the
website preview server on a public interface. TLS makes browser cookies Secure.
Admin provisioning/promotions are server-side; public registration always creates
ordinary members. To add another admin, run `manage.py create-admin` again.
Configuration:
| Variable | Default | Purpose |
| --- | --- | --- |
| `AMNESIA_ORIGIN` | `http://127.0.0.1:5173` | Exact browser origin; set HTTPS on the VPS |
| `AMNESIA_DATA` | `licensing/data` | SQLite database + HMAC secret |
Back up **the database and `hwid.secret` together**. Use SQLite's backup API for a
live database, or stop the service before copying the state directory; do not
copy only the database file while WAL writes are active. Losing/changing the
HMAC secret invalidates key hashes and hardware ties. Do not commit this directory
or put it inside Apache's DocumentRoot. Requests never log passwords, tokens,
raw keys or HWIDs. Standard Apache access logs still contain IPs and paths.
## Verification
```sh
cd licensing
python -m unittest discover -s tests -v
```
Tests use temporary databases and cover accounts/roles, CSRF/origin checks, key
redemption races, HWID mismatch, expiry/revocation, reset approval, password
changes, disabled accounts, token expiry, rate limits and secret storage.
Deployment follows the official [Apache reverse-proxy guide](https://httpd.apache.org/docs/2.4/howto/reverse_proxy.html)
and [Waitress proxy/header guidance](https://docs.pylonsproject.org/projects/waitress/en/stable/reverse-proxy.html).