Skip to main content

Localhost API agent for Belgian eID cards using pythonbeid.

Project description

eID Agent (localhost API)

Agent local Python pour lire une carte eID belge via pythonbeid et exposer une API HTTP sur 127.0.0.1 pour un front web distant (HTTPS).

Fonctionnalités

  • Bind local strict: 127.0.0.1 uniquement
  • API FastAPI JSON UTF-8
  • Session token en mémoire (TTL, Bearer token)
  • CORS strict configurable par liste d’origines
  • Rate limit simple sur /v1/read
  • Mapping des données eID vers un contrat stable pour préremplissage formulaire

Installation

Package PyPI: pythonbeid-server
Module Python importable: eid_agent
Commande CLI installée: eid-agent

pip install -e .

Avec dépendances de test:

pip install -e ".[dev]"

Exécution

Mode recommandé:

eid-agent

Alternative:

python -m eid_agent

Configuration (variables d’environnement)

  • EID_AGENT_PORT (défaut: 8765)
  • EID_AGENT_ALLOWED_ORIGINS (ex: https://school.example,https://dev.school.example)
  • EID_AGENT_SESSION_TTL_SECONDS (défaut: 120)
  • EID_AGENT_RATE_LIMIT_PER_MINUTE (défaut: 10)
  • EID_AGENT_LOG_LEVEL (défaut: INFO)

HTTPS local optionnel:

  • EID_AGENT_HTTPS=1
  • EID_AGENT_TLS_CERT_PATH
  • EID_AGENT_TLS_KEY_PATH

Si HTTPS est activé sans cert/key valides, l’agent échoue au démarrage.

API v1

Toutes les réponses JSON incluent:

  • ok (bool)
  • timestamp (ISO8601 UTC)
  • error en cas d’échec

Health

GET /v1/health

Session

POST /v1/session

Retourne:

{
  "ok": true,
  "timestamp": "2026-03-03T09:00:00Z",
  "token": "<opaque_token>",
  "expires_in": 120
}

Status (auth requise)

GET /v1/status

Header:

Authorization: Bearer <token>

Read eID (auth requise)

POST /v1/read

Body:

{
  "include_photo": true,
  "fields": [
    "first_names",
    "first_name",
    "last_name",
    "national_number",
    "birth_date",
    "birth_place",
    "nationality",
    "sex",
    "card_number",
    "issuing_municipality",
    "validity_start",
    "validity_end",
    "address_street",
    "address_zip",
    "address_city"
  ]
}

Si fields est omis, l’agent renvoie l’ensemble par défaut (incluant les champs eID usuels + adresse).

Logout (auth requise)

POST /v1/logout

Exemple côté front web

const sessionResp = await fetch("http://127.0.0.1:8765/v1/session", {
  method: "POST"
});
const sessionJson = await sessionResp.json();
const token = sessionJson.token;

const readResp = await fetch("http://127.0.0.1:8765/v1/read", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "Authorization": `Bearer ${token}`
  },
  body: JSON.stringify({
    include_photo: true
  })
});
const readJson = await readResp.json();
console.log(readJson.data);

Important:

  • Le domaine front doit être explicitement autorisé via EID_AGENT_ALLOWED_ORIGINS.
  • Sans origine autorisée, aucun header Access-Control-Allow-Origin n’est envoyé.

Exemple curl

Créer une session:

curl -X POST http://127.0.0.1:8765/v1/session

Lire la carte:

curl -X POST http://127.0.0.1:8765/v1/read ^
  -H "Authorization: Bearer <token>" ^
  -H "Content-Type: application/json" ^
  -d "{\"include_photo\":true}"

Confidentialité et logs

  • Aucun token n’est loggé.
  • Aucune donnée personnelle eID (nom, adresse, NISS, photo) n’est loggée.
  • Logs techniques: démarrage/arrêt, config réseau, erreurs techniques.

Application résidente Windows (zone de notification)

L'agent peut tourner en tâche de fond avec une icône dans la zone de notification.

Installation des dépendances et lancement depuis les sources:

pip install -e ".[tray]"
eid-agent-tray

Menu de l'icône:

  • Vérifier lecteur / carte (notification avec l'état)
  • Ouvrir le health check dans le navigateur
  • Ouvrir le dossier de configuration
  • Quitter

Particularités:

  • Une seule instance: si le port est déjà occupé, un message d'erreur s'affiche et l'application se ferme.
  • Logs fichier rotatifs: %LOCALAPPDATA%\eid-agent\logs\eid-agent.log
  • Configuration persistante: créer un fichier .env dans %LOCALAPPDATA%\eid-agent\ (mêmes variables EID_AGENT_* que ci-dessus), par exemple:
EID_AGENT_ALLOWED_ORIGINS=https://school.example
EID_AGENT_PORT=8765

Construire l'exécutable et l'installeur Windows

Prérequis: Inno Setup 6 (winget install JRSoftware.InnoSetup).

powershell -ExecutionPolicy Bypass -File packaging\build.ps1

Le script:

  1. installe les dépendances de build (.[tray,build], PyInstaller),
  2. génère l'icône (packaging\make_icon.py),
  3. construit l'exécutable sans console dist\eid-agent-tray\eid-agent-tray.exe,
  4. compile l'installeur packaging\output\eid-agent-setup-<version>.exe.

Pour construire uniquement l'exécutable: packaging\build.ps1 -SkipInstaller.

L'installeur (français/anglais) installe l'application, crée un raccourci dans le menu Démarrer et propose une option "Lancer eID Agent à l'ouverture de session Windows" (clé Run HKCU, retirée à la désinstallation).

Tests

pytest -q

Les tests API n’utilisent pas de matériel eID et injectent un backend simulé.

Publication PyPI (GitHub Actions)

Le workflow CI/CD est fourni ici:

  • .github/workflows/publish-pypi.yml

Il publie automatiquement sur PyPI avec Trusted Publishing (OIDC) quand un tag v* est poussé.

Configuration initiale (une seule fois)

  1. Sur PyPI, créer un Trusted Publisher pour ce projet avec:
    • Owner GitHub
    • Repo GitHub
    • Workflow: publish-pypi.yml
    • Environment: pypi
  2. Sur GitHub, créer l'environnement pypi (Settings > Environments).

Release

  1. Mettre à jour la version dans pyproject.toml (ex: 1.0.1).
  2. Commit/push.
  3. Créer et pousser le tag correspondant:
git tag v1.0.1
git push origin v1.0.1

Le workflow vérifie que le tag correspond à la version (v<version>), build le package, puis publie sur PyPI.

Page web d'exemple

Un exemple de formulaire auto-rempli est disponible ici:

  • examples/web/index.html

Servir la page (PowerShell)

  1. Démarrer l'agent avec une origine CORS qui correspond au serveur statique:
$env:EID_AGENT_ALLOWED_ORIGINS="http://127.0.0.1:8080"
python -m eid_agent
  1. Dans un second terminal, servir la page:
python -m http.server 8080 --bind 127.0.0.1 --directory examples/web
  1. Ouvrir:
  • http://127.0.0.1:8080

Puis cliquer sur le bouton Lire eID et remplir.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pythonbeid_server-1.1.0.tar.gz (20.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pythonbeid_server-1.1.0-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file pythonbeid_server-1.1.0.tar.gz.

File metadata

  • Download URL: pythonbeid_server-1.1.0.tar.gz
  • Upload date:
  • Size: 20.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pythonbeid_server-1.1.0.tar.gz
Algorithm Hash digest
SHA256 8cb7b65c96c44ea138da5b611bbffd470fb7a76a1dbf0d9a0d2fd2a1cead8c8c
MD5 c797fa247a1797326633675ae9b19e93
BLAKE2b-256 09d89ffbc403770a09194768e4a890336835ffb29d27e7c79606efdff1d915cd

See more details on using hashes here.

Provenance

The following attestation bundles were made for pythonbeid_server-1.1.0.tar.gz:

Publisher: publish-pypi.yml on Lapin-Blanc/pythonbeid-server

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pythonbeid_server-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pythonbeid_server-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 53285e2dfb8add6ad20b74bae67ec50ca7802f2d2165ad508aed87725228a858
MD5 b10c6d0a463be78c47400838ec146a82
BLAKE2b-256 87dfddfb836ca113a7e33c815d370bcf263ca9cb30eacdc029d17369ab683dae

See more details on using hashes here.

Provenance

The following attestation bundles were made for pythonbeid_server-1.1.0-py3-none-any.whl:

Publisher: publish-pypi.yml on Lapin-Blanc/pythonbeid-server

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page