- PHP 93.6%
- CSS 2.7%
- Dockerfile 1.4%
- PLpgSQL 1.4%
- Shell 0.5%
- Other 0.4%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| .idea | ||
| bin | ||
| db | ||
| docker | ||
| lang | ||
| public | ||
| src | ||
| storage | ||
| templates | ||
| .dockerignore | ||
| .gitignore | ||
| composer.json | ||
| composer.lock | ||
| docker-compose.yml | ||
| Dockerfile | ||
| env.example | ||
| README.md | ||
| verify-lic.py | ||
Customer Portal
Small B2B customer portal. Customers log in, see which products they are licensed for, download releases they are entitled to, and download their signed license file. There is no self-service: all customers, licenses and releases are managed by the operator through an interactive CLI (bin/portal.php).
Stack: PHP 8.4+ (PHP-FPM) · Nginx · PostgreSQL · PDO · native sessions · server-rendered PHP templates · Tailwind CSS 4 (standalone CLI) · Docker (one app container with Nginx + PHP-FPM; Postgres is external). No JavaScript framework. One tiny static app.js for the language select.
Concepts
| Thing | Meaning |
|---|---|
| Customer | A company. Has many users and many licenses. Has a UUID that goes into the license file. |
| User | A login (email + password) belonging to one customer. |
| Product | Something you sell. All products are visible to all customers; a license only decides what can be downloaded. |
| Release | Version + release date + notes of a product. Has one or more files (Windows x64, Windows x86, Source…). |
| License | One row per purchase or renewal: customer + product + model + dates. |
| License file | Signed CBOR file, one per customer, regenerated on every change. Contains all of the customer's licenses. Every issued file is kept. |
License models
| Model | Meaning | valid_until |
|---|---|---|
perpetual |
Paid upgrades. The customer keeps using every version released up to the end date, forever. | End of support/updates. Empty = updates for life. |
subscription |
Usable only while the subscription runs. Lost afterwards. | Subscription end. Empty = no end. |
Download rule
One SQL fragment (src/Entitlement.php) decides everything, used by both the pages and the download endpoint:
perpetual: release date ≤valid_until(or no end date)subscription: today is withinvalid_from…valid_until- revoked licenses never count; unpublished releases are never served
Downloads are streamed through PHP from storage/ (outside the web root), checked on every request and logged in download_log.
Layout
public/ only directory served by Nginx
index.php front controller + router
assets/ app.js (committed), app.css (generated by Tailwind)
src/ classes (classmap autoload) + helpers.php
Controllers/
templates/ PHP templates
lang/ translations (en.php, cs.php)
bin/ CLI tools (portal, migrate, keygen, verify-license, check-lang)
db/migrations/ numbered SQL migrations
docker/ nginx.conf, supervisord.conf, zz-env.conf (+ entrypoint.sh, optional)
storage/ NOT web-served
releases/ <product-slug>/<version>/<file>
licenses/ <customer-uuid>/<seq>.lic (+ archive/)
incoming/ drop files here for the CLI to pick up
keys/ <kid>.key (private signing keys, mode 0600)
Development setup
Prerequisites
- Docker with Compose
- A running Postgres container (13 or newer) and superuser access to it
- The standalone Tailwind CLI binary (no Node needed)
1. Configure
cp .env.example .env
# edit .env: DB_DSN host must be your Postgres container's name
mkdir -p storage/{releases,licenses,incoming,keys}
2. Network and database
The app container reaches Postgres over an external docker network called portal.
docker network create portal
docker network connect portal <postgres-container-name>
Create the database and user (use your superuser; citext needs it):
docker exec -i <pg> psql -U postgres -c "CREATE USER portal PASSWORD 'change-me';"
docker exec -i <pg> psql -U postgres -c "CREATE DATABASE portal OWNER portal;"
docker exec -i <pg> psql -U postgres -d portal -c "CREATE EXTENSION IF NOT EXISTS citext;"
Name, user and password must match DB_DSN, DB_USER, DB_PASS in .env.
3. Start and install
docker compose up -d --build
docker compose exec app composer install
docker compose exec app php bin/migrate.php
4. Signing key
docker compose exec app php bin/keygen.php portal-dev
Prints the public key (hex and base64). Put the same kid in .env as SIGNING_KID=portal-dev, then docker compose up -d to apply it.
5. CSS
Download the Tailwind standalone binary for your OS from the tailwindcss GitHub releases page, save as ./tailwindcss, chmod +x. Then, in a separate terminal, leave this running:
./tailwindcss -i src/app.css -o public/assets/app.css --watch
Without it the page is unstyled.
6. First data
alias portal='docker compose exec app php bin/portal.php'
portal
In the CLI: Products → Create, Customers → Create (offers to create the first user and prints the generated password once), Releases → Add release, Licenses → Update customer licenses.
Open http://localhost:8080/login.
Admin CLI
portal opens a menu (arrow keys, Enter, q/← to go back; falls back to numbered prompts without a TTY). Jump straight to a section: portal licenses. Sections: customers users licenses products releases log.
| Task | Where |
|---|---|
| New customer + login | Customers → Create (or Users → Create) |
| Reset a password | Users → Reset password |
| Customer bought / renewed | Licenses → Update customer licenses |
| Revoke a license | Licenses → Update → Revoke |
| New release, several files | Releases → Add release |
| Add a file to a release | Releases → Add file to existing release |
| Hide / show a release | Releases → Publish / unpublish |
| See what was downloaded | Recent downloads |
| Check a customer's file | Licenses → Verify latest issued file |
Every change to licenses shows the resulting license file contents first and only commits after confirmation, in one transaction together with the newly signed file. Files to import (release files) go into storage/incoming/; in dev the repo is mounted so just copy them there. In prod: copy to docker cp file.zip <container>:/app/storage/incoming/storage/incoming with SFTP.
Usernames are emails, passwords are generated (shown once, only the hash is stored). Lost password = reset.
Migrations
Plain SQL files in db/migrations/, named NNN_description.sql, applied in order, each in one transaction, tracked in schema_migrations.
docker compose exec app php bin/migrate.php # apply pending
docker compose exec app php bin/migrate.php status
docker compose exec app php bin/migrate.php baseline 001 # mark an existing DB as already at 001 (runs nothing)
Rules: never edit an applied file (the runner warns), add a new one; no BEGIN/COMMIT inside files.
Translations
Plain PHP arrays in lang/<code>.php. Language: cookie → Accept-Language → en; a logged-in user's choice is stored in users.lang. Missing keys fall back to en, then to the key itself.
Add a language: copy lang/en.php, translate, add the code to I18n::SUPPORTED and I18n::NAMES (plural rules for de/fr already exist in I18n::plural()). Check completeness:
docker compose exec app php bin/check-lang.php
Product names, descriptions and release notes come from the database and are single-language.
License file format (v1)
One CBOR item, COSE_Sign1 (RFC 9052), tag 18, so files start with bytes d2 84. Signed with Ed25519, not encrypted.
18([ protected, {}, payload, signature ])
protected = bstr({ 1: -8, 4: h'<kid, UTF-8>' }) # alg EdDSA, key id
payload = bstr({
"v": 1, "id": "<file uuid>", "seq": 7, "issued": "YYYY-MM-DD",
"customer": { "id": "<customer uuid>", "name": "..." },
"licenses": [
{ "product": "<slug>", "model": "perpetual", "from": "YYYY-MM-DD", "until": "YYYY-MM-DD" },
{ "product": "<slug>", "model": "subscription", "from": "YYYY-MM-DD", "until": "YYYY-MM-DD" }
]
})
Reader rules:
- Signature is over the COSE
Sig_structure["Signature1", protected, h'', payload], using the payload bytes exactly as stored (never re-encode before verifying). Pure Ed25519, 64 bytes. perpetual: use forever; entitled to releases whose authenticated release date ≤until.untilabsent = updates for life.fromis informational.subscription: valid only whilefrom≤ today ≤until.untilabsent = no end.- Several entries per product are allowed (renewals). Perpetual: latest
untilwins, absent beats any date. Subscription: any entry covering today is enough. - "Has support / has not" is derived from the dates, not stored.
seqincreases per customer; the highest is current. Unknown fields must be ignored. Breaking change = bumpv.- Not in the file: private key, database ids, invoice refs, notes.
Verify a file:
docker compose exec app php bin/verify-license.php storage/licenses/<uuid>/000001.lic [public-key-hex]
Pass the public key hex printed by bin/keygen.php to test the way an external reader would; without it the script derives the key from the local key file.
Keys
- Private keys live in
storage/keys/<kid>.key, mode 0600, root only. The web process (php-fpm worker) never reads them; signing happens only in the CLI. - Use different keys for dev and prod. Never put the dev key on a production machine.
- Back up the production key separately from the database. Losing it means a new kid, reissuing every customer's file, and shipping the new public key to the client library.
- Rotation:
php bin/keygen.php <new-kid>, setSIGNING_KID, reissue (Licenses → Reissue). Old files still verify against the old public key.
Production notes
- Build CSS in the image (multi-stage Dockerfile stage that runs the Tailwind CLI with
--minifyand copiesapp.css). Do not mount the repo over/appin prod. - Mount
storage/as a persistent volume. - TLS terminates on your external reverse proxy, which proxies to the container over HTTP. The proxy must send
X-Forwarded-ForandX-Forwarded-Proto. SetAPP_ENV=prod. - In
docker/nginx.conf, inside theindex.phplocation, addfastcgi_param HTTPS on;, and at server level take the real client address from the proxy (needed for login rate limiting and the download log):set_real_ip_from 172.16.0.0/12; # your proxy's network real_ip_header X-Forwarded-For; real_ip_recursive on; - Add headers:
Strict-Transport-Security,X-Frame-Options DENY,Referrer-Policy same-origin, andContent-Security-Policy "default-src 'self'; style-src 'self' 'unsafe-inline'"(templates use a few inline styles). - Optional: run migrations on container start via
docker/entrypoint.sh(callsphp bin/migrate.php upbefore supervisord). It refuses to start on a database without migration history, so runbaseline 001first on an existing database. - File permissions: release and license files must be readable by the php-fpm user (
0644, directories0755). Keys stay0600. - Backups: the database and
storage/(licenses and releases), and the key(s) separately.
Deployment outer NGINX
location /talo/ {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location = /talo { return 301 /talo/; }
Everyday Docker commands
| You changed | Do |
|---|---|
PHP, templates, lang/, bin/, migrations |
nothing (repo is mounted), reload |
.env |
docker compose up -d |
Dockerfile, anything in docker/ |
docker compose up -d --build |
composer.json |
docker compose exec app composer dump-autoload |
| New class file | docker compose exec app composer dump-autoload |
| Container acting up | docker compose restart app, docker compose logs -f app |
Testing checklist
Log in as customer A and check:
- A release dated after the end of a perpetual license is shown locked, with no buttons;
/download/file/<id>returns 404 - A subscription that has ended: product is under Inactive, its releases return 404
- A product the customer never bought: dashboard lists it as inactive, product page opens, downloads return 404
- Another customer's file id, a nonexistent id, and
release_files.file_name = '../../.env'all return 404 (path traversal) - Logged out:
/download/...redirects to login;POST /logoutwithout CSRF token returns 403 - Rapid
POST /logingets rate-limited by Nginx - Revoking a license issues a new file with that entry gone; the old file stays on disk
php bin/verify-license.phpon a file with one flipped byte printsSIGNATURE INVALID
Troubleshooting
| Symptom | Likely cause |
|---|---|
| Unstyled page | Tailwind build not running |
getenv('DB_DSN') false in web requests, CLI works |
missing docker/zz-env.conf (clear_env = no) in the image |
| Login succeeds, bounced back to login | APP_ENV=dev missing on plain HTTP (secure cookie dropped) |
Class "X" not found |
composer dump-autoload |
could not translate host name |
Postgres container not on the portal network |
could not find driver |
image built without pdo_pgsql, rebuild |
| 502 | php-fpm not running: docker compose logs app |
CLI prints ^[[A when pressing arrows |
terminal not passing escape sequences; use docker compose exec -T for the numbered fallback |
| Accented characters rejected in the CLI | terminal without UTF-8; the CLI sets iutf8, retype the line |
| Entrypoint container restart loop | migration failed or entrypoint.sh has CRLF line endings; docker compose logs app |