Metadata-Version: 2.5
Name: trytond_opnsense_voucher
Version: 8.0.0.6
Summary: Tryton module opnsense_voucher
Author: Clemens Hupka
Maintainer-email: Clemens Hupka <issues@tryton-erp.eu>
License-Expression: GPL-3.0-or-later
License-File: COPYRIGHT
License-File: LICENSE
License-File: icons/LICENSE
Keywords: opnsense_voucher,tryton
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Plugins
Classifier: Framework :: Tryton
Classifier: Topic :: Office/Business
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.10
Requires-Dist: cryptography
Requires-Dist: pillow
Requires-Dist: requests
Requires-Dist: trytond-company<8.1,>=8.0
Requires-Dist: trytond-keycloak-auth<8.1,>=8.0
Requires-Dist: trytond-printing-labels<8.1,>=8.0
Requires-Dist: trytond-web-user<8.1,>=8.0
Requires-Dist: trytond<8.1,>=8.0
Provides-Extra: test
Description-Content-Type: text/x-rst

OPNSense WiFi Voucher
=====================

The ``opnsense_voucher`` module manages OPNsense Captive Portal WiFi
vouchers from within Tryton.  It connects to the voucher API of one or
more OPNsense firewall servers, generates voucher rolls on request,
keeps the local database synchronized with the portal server, and
prints the vouchers as labels.

Features
--------

- **Portal Servers**: Configure multiple OPNsense servers with API
  credentials, one of which can be the default server.
- **Voucher Rolls**: Generate rolls of vouchers with a validity
  duration and a date limit, and hand them out with an audit trail of
  who handed out the roll and when.
- **Voucher Tracking**: Each voucher's username, password, validity,
  start/end times, expiry time and state (unused, valid, expired) is
  mirrored locally, with a dedicated view for vouchers expiring soon.
- **Automatic Synchronization**: A cron job (hourly by default)
  synchronizes rolls and vouchers of servers with *Automatic Sync*
  enabled; stale records removed on the server are deleted locally.
- **Voucher Requests**: A request/approval workflow so that users of
  the *Requester* group can ask for vouchers, and users of the
  *Manager* group approve, ask for feedback or reject the requests.
  Requests exceeding configurable thresholds of the portal server
  (number of tickets, time horizon) require a free text explanation.
- **Label Printing**: Renders each voucher as a printable label
  (QR-code, username, password, roll reference) through the
  ``printing_labels`` module and queues it as a printing job.
- **Portal User Links**: Links each ``web.user`` portal account to the
  internal user it acts as on the HTTP routes, managed by an
  administrator; the *Link Portal Users* wizard proposes the
  unambiguous pairs.

Portal Modules
--------------

The module declares its two portal modules of the Flask Portal in
``flask_board.cfg`` (issue #843), so the flask_board "Sync Module
Definitions" wizard discovers them and an administrator links and
configures them per board:

- ``fe_voucher`` (frontend, login required): the requester
  self-service for the voucher roll requests (create, submit, track
  and respond to feedback), grouped in the menu section ``voucher``
  (priority 4500).  It talks to the requester HTTP routes of the
  module.
- ``be_voucher`` (back office, login admin): the decision workflow
  (approve, request feedback, reject, generate), the daily roll queue,
  the *Today* panel and the statistics dashboard, grouped in the menu
  section ``voucher_admin`` (priority 4600).  It accesses the
  ``opnsense.voucher.*`` models directly.

The declaration carries the menu entries the portal registers (the
declaration order is the default order of the entries) and the
defaults of the module configuration variables the portal reads
(``FE_VOUCHER_RATE_LIMIT``, ``VOUCHER_BE_GROUPS`` and the
``BE_VOUCHER_*`` rate limit, statistics and expiry settings); an
administrator overrides them per board.  See
``flask_board/doc/index.rst`` for the declaration contract.

Security Notes
--------------

- The API key and the API secret of a portal server and the passwords
  of the vouchers are **encrypted at rest** with an AES-256-GCM master
  key read from the trytond configuration (section
  ``[opnsense_voucher]``) and **never** stored in the database, so a
  database dump, a backup and an over-privileged database account only
  reveal ciphertext.  Configure ``encryption_key`` before storing
  credentials: without it the module refuses to store or read a
  credential instead of storing it unprotected.
- Only users of the *OPNsense Voucher Admin* group may read the API
  key, the API secret and the voucher passwords; Tryton removes those
  fields from the views of the *Manager* and *Requester* groups and
  refuses an explicit read or export of them.  The API client, the
  synchronization and the label printing read the credentials
  server-side.
- Upgrading an installation that stored the credentials in clear text
  requires running the migration script ``migration/8.0.0.2.py`` once,
  after configuring the master key; see the *Configuration* chapter of
  the module documentation.
- Upgrading an installation to 8.0.0.4 requires running the migration
  script ``migration/8.0.0.4.py`` once, after the module update: it
  verifies that the company rule groups are still *global* (the company
  scoping of issue #699 is AND-ed with the other rules) and reports the
  rule groups of the models of the module that no module data file
  defines — an unexpected global one refuses every user of its model
  (issue #818).  Review the reported groups and delete the unexpected
  ones in the client, never with raw SQL (the evaluated rule domains
  are cached per process and user).
- Keep *Verify SSL* enabled unless the portal server uses a
  self-signed certificate.
- Requesters see only their own voucher requests; this is enforced by
  a record rule in addition to the model access rules.
- Portal servers, rolls, vouchers and requests are scoped to the
  company of their server: a user only reaches the records of the
  companies of the session (plus the company-less, i.e. shared, ones),
  and the default server is resolved within those companies too.  The
  *OPNsense Voucher Admin* group and the Tryton administrator group are
  exempt from that scoping (issue #818), so an administrator is never
  locked out of a record of another company, while the *Manager* and
  *Requester* groups stay scoped.  To manage a record of another
  company, either hold one of the two administrator groups or switch
  the session company (*Current Company* / *Company Filter* of the user
  preferences) to one of the record.
- Voucher data becomes read-only locally once the vouchers were
  created on the server; it is then managed by the portal server.
- Portal HTTP requests accept two credentials (issue #809): a Keycloak
  access token, verified by the ``keycloak_auth`` module and resolved
  to an **active** ``res.user`` by an exact match on ``res.user.login``,
  or a ``web_user`` session mapped to an internal user **only** by an
  explicit link (``opnsense.voucher.portal.user``).  Neither path
  derives an identity from an email address, so a portal account cannot
  impersonate another user, and the Keycloak path provisions nothing --
  an identity that no active user has is refused.  The Keycloak path is
  opt-in through the credential itself: a session key is hexadecimal
  and can never carry the dots of a JSON Web Token, so a portal keeps
  using its session keys until it migrates.  Map every portal account
  to a dedicated ``res.user`` holding only the *Requester* group, on
  either path.

Dependencies
------------

- Tryton modules: ``ir``, ``res``, ``company``, ``keycloak_auth``,
  ``printing_labels``, ``web_user``.
- Python packages: ``requests`` (OPNsense API client), ``pillow``
  (label rendering), ``cryptography`` (AES-256-GCM encryption of the
  credentials at rest) and, through ``keycloak_auth``, ``PyJWT``
  (verification of the Keycloak access tokens).

Models
------

- ``opnsense.voucher.server`` — ``OPNSenseVoucherServer``: connection
  and behaviour configuration of an OPNsense portal server.
- ``opnsense.voucher.roll`` — ``OPNSenseVoucherRoll``: a roll of
  vouchers with validity data and hand-out audit fields.
- ``opnsense.voucher.voucher`` — ``OPNSenseVoucher``: a single
  captive portal voucher.
- ``opnsense.voucher.request`` — ``OPNSenseVoucherRequest``: a
  voucher request in the request/approval workflow.
- ``opnsense.voucher.portal.user`` — ``OPNSenseVoucherPortalUser``:
  the link between a ``web.user`` portal account and the ``res.user``
  it acts as.

Cron Jobs
---------

- ``opnsense.voucher.server|cron_sync``: runs hourly and synchronizes
  all active servers with *Automatic Sync* enabled.

Wizards
-------

- ``opnsense.voucher.roll.sync`` — *Sync Rolls*: synchronize the rolls
  of a portal server on demand.
- ``opnsense.voucher.roll.create`` — *Create Roll*: generate a new
  voucher roll directly on the portal server.
- ``opnsense.voucher.roll.print`` — *Print Roll*: render the vouchers
  of a roll as labels and queue printing jobs.
- ``opnsense.voucher.portal.user.link`` — *Link Portal Users*: propose
  and create the links between the portal accounts and the internal
  users they act as.
