borrowing
The service is being finished in services/borrowing (contract: docs/dev/phase2-contracts.md, Borrowing
service; specs: features.md "Borrowing sheet", security.md).
- Implemented in code:
cmd/borrowing/main.go(HTTP server, outbox relay, member erasure consumer, leased jobs), sheet lifecycle, external persons with encrypted PII and sealed ID photos, identity extraction and QR detection through vision, reminders, retention (archive then crypto-shred), CSV export, MongoDB indexes. - Not wired yet: no
docker-compose.ymlentry, no/v1/borrowingroute inGATEWAY_ROUTES, no web screen (/app/borrowingshows a "coming soon" placeholder). Theopenapi.yamlexists (API reference); tests cover the API, sheets, persons, store, jobs and erasure.
Role: replaces the paper borrowing sheets. It manages borrowers (members or external persons), the sheet lifecycle (items, checkout photos, on-screen signature, approval, partial returns), reminders and the identity data of external borrowers, encrypted and isolated from the rest of the system.
Limits: does not own item status: it publishes checkout/return events that inventory applies. Sheet photos and signatures go through the files service; identity document photos never do (they stay in this service, sealed, in a dedicated bucket). No deposit. Identity extraction and QR detection are delegated to vision, whose results are always editable suggestions.
Model
- Borrower: a member (
kind = member, Keycloak ID, display name given by the client, no identity document) or an external person (kind = external,ExternalPersonaggregate with its own ULID, optionalmember_idlink). Accepted document types:fr_id_card,epita_card,other. - Sheet statuses:
draft→pending_approval→checked_out→partially_returned→returned, pluscancelled(only fromdraftorpending_approval). - Person statuses:
active→archived(photo and contact deleted) →erased(key shredded, stub kept). - Limits in code: 100 items per sheet, 20 photos per list, due date at most 366 days ahead, 10 photos per QR detection.
| Step | Behaviour in code |
|---|---|
| Create | POST /sheets: borrower, site_id, item_ids (may be empty: photos are uploaded and detect-items run on the draft), due_at, purpose, optional project_id. Each item is read from inventory with the forwarded principal; it must be on the sheet site, available and not held by another active sheet. Status draft. |
| Edit | PATCH /sheets/:id (drafts only): items, checkout_photo_ids, due date, purpose. Photos must be files owned by atl:borrowing:<site>:sheet/<id> (checked with the files service). |
| Detect | POST /sheets/:id/detect-items with file_ids: downloads the photos from files, sends them to vision /v1/detect/qr, resolves short codes with inventory /v1/inventory/resolve/:code, returns codes, suggestions and unresolved codes. Allowed on drafts (checkout) and active sheets (return). |
| Submit | POST /sheets/:id/submit with signature_file_id and signer_name: at least one item (422 item_ids: required), signature file must belong to the sheet; an external borrower must be active with an ID photo. Status pending_approval. |
| Approve | POST /sheets/:id/approve: requires a human user holding borrowing.sheet.approve, or the member borrower themself. Items are checked again, status checked_out, events approved + checked_out. |
| Return | POST /sheets/:id/returns with item_ids (open items only), photo_ids, condition_note: partial returns allowed (partially_returned); all back → returned + returned. |
| Cancel | POST /sheets/:id/cancel before checkout → cancelled. |
Borrower and signer names stored on a sheet are sealed with the borrower subject key (member ID or person ID),
so erasing a member or shredding a person turns them into a pseudonym (Personne externe #xxxxxx for persons).
Authorization
ARNs: atl:borrowing:<site>:sheet/<id> (parents: site, optional project) and atl:borrowing:*:person/<id>
(parents: the person's sites). Catalogue permissions: borrowing.sheet.read/create/update/delete,
borrowing.sheet.approve, borrowing.sheet.read_all (elevated), borrowing.identity.read (sensitive),
borrowing.person.manage.
- The member borrower and the creator of a sheet always pass sheet checks (read, update, cancel, return).
- Reads fall back to
read_allandapprove(approvers); lists use the Lookup scope ofread(plusread_allin admin mode);?mine=truelists the caller's own sheets. - Identity data and ID photo:
borrowing.identity.readand a passkey step-up younger than 15 minutes (step_up_requiredotherwise). Every access is audited; responses areCache-Control: no-store. borrowing.sheet.deleteis declared in the catalogue but no route uses it.- Sheet files (files delegate): the files service delegates denied checks on
atl:borrowing:<site>:sheet/<id>toPOST /internal/v1/attachments/authorize({owner, parents, action}, callerX-Atl-Principalrequired; 204 allowed, 403 denied, 404 unknown owner). Read: same rule asGET /sheets/:id. Write: the member borrower or the creator while the sheet isdraft,pending_approval(checkout photos, signature),checked_outorpartially_returned(return photos); otherwiseborrowing.sheet.update./internal/...routes are not routed by the gateway.
HTTP API
All routes are under /v1/borrowing and require an authenticated principal (API reference). Lists are
cursor paginated (limit, cursor).
| Method | Path | Permission | Notes |
|---|---|---|---|
| POST | /identity/extract | person.manage | multipart image + kind (auto, fr_id_card, epita_card); forwards to vision, stores nothing, audited |
| GET | /persons | person.manage (Lookup) | filters q (blind index), status, site |
| POST | /persons | person.manage | JSON or multipart with optional photo |
| GET / PATCH | /persons/:id | person.manage | view without identity data |
| PUT | /persons/:id/photo | person.manage | replaces the ID photo, previous one deleted |
| GET | /persons/:id/identity | identity.read + step-up | decrypted names, birth date, contact |
| GET | /persons/:id/photo | identity.read + step-up | decrypted photo streamed (never a presigned URL) |
| GET | /sheets | sheet.read (Lookup) | filters status, site, project_id, borrower_kind, member_id, person_id, item_id, q, overdue, mine |
| POST | /sheets | sheet.create | person.manage on the person for an external borrower |
| GET | /sheets/export.csv | sheet.read_all | same filters, max 50 000 rows, audited |
| GET / PATCH | /sheets/:id | sheet.read / sheet.update | |
| POST | /sheets/:id/submit, /cancel, /returns, /detect-items | sheet.update | |
| POST | /sheets/:id/approve | sheet.approve | or self-approval by the member borrower |
| POST | /internal/v1/attachments/authorize | see "Sheet files" above | internal (files delegate), not under /v1/borrowing |
Photos (identity and detection) are limited to BORROWING_MAX_PHOTO_BYTES and must be JPEG, PNG, WebP or HEIC.
Dependencies
- Services: authz (Check / Lookup), inventory (item lookup and QR resolution, forwarded principal),
files (sheet photos and signatures: metadata and download, forwarded principal), vision (identity
extraction, QR detection, service principal signed with
PRINCIPAL_SIGNING_KEY; disabled outside production when the key is unset). - Infrastructure: MongoDB, Kafka, S3 bucket
atelier-identity, KEK source (env or Barbican).
Kafka
Events are written with the state through the event store and the outbox, then published by the outbox relay;
the topic is derived from the event type (borrowing.sheet.* → borrowing.sheet.v1).
| Direction | Topic | Events |
|---|---|---|
| out | borrowing.sheet.v1 | created, updated, submitted, approved, checked_out (sheet_id, site_id, item_ids), items_returned (sheet_id, item_ids), returned, cancelled |
| out | borrowing.person.v1 | created, updated (PII sealed in the payload), photo_replaced, reactivated, archived, shredded |
| out | borrowing.reminder.v1 | due_soon, overdue (identifiers, channels, due date, open items; no names) |
| out | audit.records.v1 | every write, extraction, identity read and export |
| in | members.member.v1 | members.member.erased (consumer group borrowing-erasure) |
Inventory consumes checked_out and items_returned from borrowing.sheet.v1 (see flows).
Reminders target in_app for members and the stored contact channels for external persons; no consumer exists yet
(notifications service planned).
Data
MongoDB database borrowing:
| Collection | Content |
|---|---|
sheets | current sheet state (names sealed, search_terms blind index) |
persons | external persons: PII fields sealed, photo object key, retention dates |
member_erasures | member erasures deferred while the member still holds items |
events, outbox | event log and outbox (shared libraries) |
data_keys | per-subject data keys wrapped by the KEK (cryptox.Keyring) |
locks | job leases and outbox relay lock |
Encryption (see encryption): envelope encryption, one data key per subject
(person ID, or member ID for member names), wrapped by the KEK. The ID photo is sealed with the person key and
stored as opaque bytes at persons/<person-id>/<ulid> in the atelier-identity bucket; it is decrypted only to
stream it back. Crypto-shredding: destroying a subject key makes its names, birth date and photo unreadable.
Search: borrower names are encrypted, so q uses a blind index: folded tokens and prefixes (3+ characters)
hashed with HMAC-SHA256 keyed by BORROWING_SEARCH_KEY. Tokens are removed on shredding and member erasure.
Jobs
Run every BORROWING_JOB_INTERVAL under a MongoDB lease (one replica at a time), with the service principal.
| Job | Behaviour |
|---|---|
borrowing-reminders | due_soon once per sheet within BORROWING_REMINDER_DUE_SOON of the due date; overdue once BORROWING_REMINDER_OVERDUE_AFTER after it. Sheets of borrowers without a usable contact are marked as handled without event. |
borrowing-erasures | retries deferred member erasures |
borrowing-settle | starts retention of persons with no unresolved sheet left (recovers a failed settle) |
borrowing-archive | when archive_at is reached: deletes the ID photo, email and phone; status archived |
borrowing-shred | when shred_at is reached: shreds the person key, deletes any photo, clears the name index, keeps a stub (erased) |
Retention: when a person has no unresolved sheet left (after the last return or cancellation), archive_at = now + BORROWING_ID_PHOTO_RETENTION and shred_at = now + BORROWING_ARCHIVE_RETENTION (both from that moment).
A new sheet for the person clears both dates (legitimate interest) and reactivates an archived person.
Member erasure: on members.member.erased, drafts and pending sheets are cancelled, the member key held by
borrowing is shredded and the sheets are flagged erased; deferred while the member still holds items.
Configuration
| Variable | Default | Description |
|---|---|---|
HTTP_ADDR | :8087 | Listen address |
METRICS_ADDR | :9187 | Prometheus listener |
PRINCIPAL_PUBLIC_KEY | required | Verifies principal tokens |
PRINCIPAL_SIGNING_KEY | empty | Service principal for vision; required in production, vision features disabled otherwise |
AUTHZ_URL | http://localhost:8081 | authz |
INVENTORY_URL | http://localhost:8085 | inventory |
FILES_URL | http://localhost:8084 | files |
VISION_URL | http://localhost:8090 | vision |
MONGO_URI | mongodb://localhost:27017/?directConnection=true | MongoDB |
MONGO_DATABASE | borrowing | Database |
KAFKA_BROKERS | localhost:19092 | Brokers |
BORROWING_IDENTITY_BUCKET | atelier-identity | Bucket of sealed ID photos |
BORROWING_SEARCH_KEY | derived from CRYPTO_KEK | 32 base64 bytes; required with a Barbican KEK |
BORROWING_MAX_PHOTO_BYTES | 10485760 (10 MiB) | Max photo size |
BORROWING_ID_PHOTO_RETENTION | 24h | Delay before photo and contact deletion |
BORROWING_ARCHIVE_RETENTION | 43800h (5 years) | Delay before crypto-shredding; must be ≥ photo retention |
BORROWING_REMINDER_DUE_SOON | 24h | Due soon reminder offset |
BORROWING_REMINDER_OVERDUE_AFTER | 1h | Overdue reminder offset |
BORROWING_JOB_INTERVAL | 15m | Job period |
Plus the common variables (APP_ENV, LOG_LEVEL, OTEL_EXPORTER_OTLP_ENDPOINT), the S3 variables (S3_ENDPOINT,
S3_ACCESS_KEY, S3_SECRET_KEY, S3_REGION) and the KEK variables (CRYPTO_KEK_SOURCE, CRYPTO_KEK or the
Barbican ones), see environment variables.
Differences from the contract
- Extra events
borrowing.sheet.updatedandborrowing.sheet.submitted, and an explicitsubmitstep (signature) before approval. - Retention starts after the last return or cancellation; archiving also deletes the contact; the archive delay is counted from the settle time, not from archiving.
- Not in the contract: blind index search, CSV export, member erasure consumer, person photo replacement.