# Licensing

The `Licensing` module issues licence keys for `FulfilmentType::LicenceKey` variants. Tables: `licence_products` (per
variant: `source` pattern | pool, `pattern`, `activation_limit`, `valid_days`, `updates_months`, `hosted`), `licence_pool_keys`
(imported keys, `licence_id` once given), `licences` (one per unit: the key, its terms copied at issue, `hosted`, `revoked_at`),
`licence_activations` (instance, `deactivated_at`). `LicensingSettings` keeps the shop's Ed25519 key pair (sodium; the
secret encrypted), made on first use.

`Licensing\Contracts\Licences`: `issue(orderId)` (idempotent, missing units only, same delivery rule as downloads;
pool keys are taken under a lock; an empty pool leaves units for `release()`, the daily `licensing:release`) fires
`LicencesIssued`; `revokeOrder()` on cancellation or return (`IssueLicences` listens to `OrderStatusChanged`);
`forOrder()`, `forCustomer()`, `missing()`; `freeSeat()` scoped to the owner; `activate()` (under a lock on the licence;
the same instance is no new seat), `validate()`, `deactivate()` return a `LicenceAnswer`; `sign()` and `publicKey()`.

The API (`Routes/api.php`, `LicenceApiController`, throttled, no session) answers JSON with `X-Signature` = base64
Ed25519 of the exact body; 200 ok, 403 refused, 404 unknown key, 422 bad request. Admin: `LicenceProductController`
(`catalog.manage`) and `LicenceAdminController` (`orders.view`, revoke with `orders.manage`), menu **Licențe**. The
storefront shows keys on the order page and `account.licences`, and `Listeners\SendLicencesMail` e-mails them.

Tests: `tests/Feature/Licensing/LicencesTest.php`.

A **hosted** licence product (`hosted`, *Licență găzduită (SaaS)* on the product's licence screen) sells a shop on our
SaaS host: the key is valid for `valid_days` and gives no versions to download, so its `updates_until` stays empty
whatever `updates_months` says, and the API's `licence` object carries `"hosted": true`. The package server serves a
hosted key nothing (DISTRIBUTION.md › Which versions a key may download).
