Metadata-Version: 2.5
Name: trytond_etude_api
Version: 8.0.0.3
Summary: Etude Form Server — REST API v1 and API Tokens
Author: Clemens Hupka
Maintainer-email: Clemens Hupka <clemens.hupka@backend.at>
License-Expression: GPL-3.0-or-later
License-File: COPYRIGHT
License-File: LICENSE
Keywords: api,form,rest,submission,survey,token,tryton
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Environment :: Plugins
Classifier: Framework :: Tryton
Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
Classifier: Topic :: Office/Business
Requires-Python: >=3.10
Requires-Dist: trytond-etude-collect<8.1,>=8.0
Requires-Dist: trytond-etude<8.1,>=8.0
Requires-Dist: trytond<8.1,>=8.0
Provides-Extra: test
Requires-Dist: trytond-etude-i18n<8.1,>=8.0; extra == 'test'
Description-Content-Type: text/x-rst

Etude API Module
================

The ``etude_api`` module opens the forms of the ``etude`` group to integrations
that hold no Tryton session: it serves a versioned REST API that fetches the
definition a form serves, submits a response - completely or partially - and
resumes a partial one, and it issues the API tokens those calls authenticate
with.
It depends on ``etude_collect``, which stores the responses and runs the submit
of a form, and it adds no model to the collection: a token points at a form, it
does not change it.

The API is served by trytond
----------------------------

No additional web server is required.  The routes are registered on the WSGI
application of trytond under ``/<database_name>/etude/api/v1/``, they are
declared with ``with_pool`` and ``with_transaction`` and they return
``trytond.protocols.wrappers.Response`` objects, exactly like the routes of
``chorus_api``.  A call authenticates with the secret of an API token carried
as a bearer token; it reads and writes inside the transaction of the request,
so what it collects is one transaction of the deployment rather than a sequence
of them.

The contract of the version is a definition fetch, a submit and a resume:
``GET /forms/<code>`` reads the definition the form serves and the policy it is
served with, ``POST /forms/<code>/responses`` answers it - in one call when the
body says the response is complete, as a draft the client resumes when it does
not - ``GET /responses/<id>`` reads a response back, ``PUT /responses/<id>``
saves the answers of a draft and ``POST /responses/<id>/submit`` submits one.

A form which was translated is served in the language of the request - the
language of the user a token acts as, the ones its ``Accept-Language`` header
asks for and the language the form declares - and the response a call starts
captures it.  The localisation of forms is the ``etude_i18n`` companion of the
group, which this module reads through the pool rather than depends on: without
it, the definition is served as it was published.

A refused call leaves nothing behind
------------------------------------

What a call writes is one transaction together with the check that precedes it,
and a step that fails rolls the whole call back: the response it started, the
answers it stored and the records the bindings of the form wrote are gone.
That rollback is what makes the pipeline of a submit hold through the API the
way it holds through the client of a participant - a form whose binding fails
leaves no partial record behind, whoever answered it.

The failures are told apart the way a caller reads them: ``401`` when no token
authenticates the request, ``403`` when a valid token may not run it - another
form, another user's response, a missing scope, a write the rights of its user
refuse - ``400`` when the definition of the form refuses it, and ``404`` when
the form or the response is not there.  Every failure is answered as the JSON
of its error, with the message a deployment translates and the status a client
reads.

A token answers one form, as one user
-------------------------------------

An API token is what an integration is: it names the form it may reach - the
whole of what it may reach - the operations it is granted on it (``read``,
``write`` and ``admin``, the last implying the others) and the user it acts as.
The responses a token collects are attributed to that user, and every read and
write is checked against the access rights and the record rules of that user,
so that a token never does more than the user it acts as may do.

A response is reached through the form it answers and through the user who
answered it: a token is scoped to one form, and it reads and writes only the
responses its own user answered, so that holding a token for a form is not
holding every response of it.

The secret of a token is generated when the record is created and shown once;
only its SHA-256 digest is stored, so a token that was not copied when it was
issued can not be read back.  A token that is not needed any more is revoked by
clearing ``active`` rather than deleted, because the responses it answered stay
attributed to its user.

Issuing a token is granting a programmatic way into a form, so a token is read,
written, created and deleted by the administrators of a deployment and of the
group alone.

See ``doc/index.rst`` for the module documentation and ``ETUDE.md`` in
``modules-cch/`` for the architecture of the whole group.
