Simpl-Resultat/src/shared/licenseExpiry.ts
le king fu d2ded515fd
All checks were successful
PR Check — Frontend / frontend (pull_request) Successful in 1m43s
PR Check — Rust / rust (pull_request) Successful in 9m11s
fix(license): name key-entry errors, keep the edition off the activation token, flag revoked keys
Rust (license_commands.rs)
- LicenseError gains InvalidPrefix, InvalidSignature, Malformed and
  UnknownEdition. validate_with_key returns it (ExpiredSignature maps
  to LicenseExpired), and store_license, read_license and
  validate_license_key reject with the same JSON as the network
  commands. store_license_in and read_license_in take a directory, so
  the Storage mapping is tested at the source.
- D12: the edition comes from the license key alone.
  resolve_license_entitlements no longer takes the activation token,
  and current_entitlements reads only license.key
  (entitlements_from_dir). A token that had expired (2-year server
  lifetime) or came from another machine (restored data) used to drop
  a valid key to free, while a key copied without its token kept its
  edition. get_activation_status and persist_activation_token keep the
  strict token validation, and the key keeps its strict validation
  (CWE-613).

Front
- LicenseCard names the load error, and LicenseKeyForm (extracted so a
  static render reaches it) names a refused key, through
  parseLicenseError: no raw backend string reaches the card.
- D13: a license_revoked answer is recorded in LicenseContext for the
  whole session, because the card unmounts when the user leaves
  Settings > Users. Only a different key clears it. Card-level
  message, alert icon, Activate disabled through selectCanActivate.
- Machine counter n/limit, "Not activated", activating and
  deactivating labels, dates in fr-CA/en-CA, and "No expiry date"
  beyond 50 years (licenseExpiry.ts, for the 100-year keys of
  maximus-api#63).
- license.removeLicense and license.machines.activateError removed;
  the price authFailed message points to activating the machine.

Tests: 15 new Rust tests (resolution from a data directory, named
variants, the 100-year key of #363, a key without exp) replace 3
machine-binding tests; vitest covers the card and form renders, the
context revocation, the hook wiring and the locales.
test: reproduces #347 (an expired token of this machine resolved a
valid Base key to free on 5d549d6).

Docs: CHANGELOG FR/EN, guide section 12 and in-app docs,
architecture.md, ADR 0017 amendment for #347, qa-update-cycle.md,
api-contract-prices.md.

Fixes #347
Fixes #360
Refs #363

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-29 21:39:01 -04:00

28 lines
1.1 KiB
TypeScript

// Display rule of the license expiry date on the License card (#347, D9).
//
// Keys issued since maximus-api#63 last 100 years: a date in 2126 is noise, and the
// key does not end in any practical sense. Beyond the horizon the card shows
// "No expiry date"; closer dates show in the app language.
/** Beyond this horizon, an expiry date is shown as "no expiry date". */
export const NO_EXPIRY_HORIZON_YEARS = 50;
const SECONDS_PER_YEAR = 365.25 * 24 * 3600;
export type LicenseExpiryDisplay = { kind: "noExpiry" } | { kind: "date"; text: string };
/**
* How the card shows `expiresAt` (seconds since the epoch) at `nowSeconds`: no date
* beyond the horizon, otherwise the date formatted for `locale` (`fr-CA` or `en-CA`,
* chosen by the card from the app language).
*/
export function licenseExpiryDisplay(
expiresAt: number,
nowSeconds: number,
locale: string,
): LicenseExpiryDisplay {
if (expiresAt - nowSeconds > NO_EXPIRY_HORIZON_YEARS * SECONDS_PER_YEAR) {
return { kind: "noExpiry" };
}
return { kind: "date", text: new Date(expiresAt * 1000).toLocaleDateString(locale) };
}