Compare commits
52
Commits
809cf63994
..
v1.2.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4316da578f | ||
|
|
1e38e6e4ee | ||
|
|
37dc36f1f2 | ||
|
|
2d984fd665 | ||
|
|
ac8bd9acb4 | ||
|
|
95b4cf79cf | ||
|
|
a34220bf2d | ||
|
|
b28fcdc774 | ||
|
|
88e89b9567 | ||
|
|
a91ddf7aa8 | ||
|
|
0f90aad938 | ||
|
|
dd053a50c8 | ||
|
|
c77af98a68 | ||
|
|
d0957e90d0 | ||
|
|
a50eb830f4 | ||
|
|
c642763035 | ||
|
|
b869f70922 | ||
|
|
9e8b01b135 | ||
|
|
456579880e | ||
|
|
25aedb431e | ||
|
|
25bab43b21 | ||
|
|
cbc94c8722 | ||
|
|
d1b0807d38 | ||
|
|
225f6e4b93 | ||
|
|
f97e0aed45 | ||
|
|
9dbd9d7a11 | ||
|
|
33ff9a6789 | ||
|
|
22286aa2b1 | ||
|
|
d89c0d03c9 | ||
|
|
c642adb85b | ||
|
|
ed05093370 | ||
|
|
6b6f43bca9 | ||
|
|
a6b7f2002b | ||
|
|
df7dc76405 | ||
|
|
409842694d | ||
|
|
33579a50a5 | ||
|
|
71c002b8b6 | ||
|
|
5a452c46af | ||
|
|
d52460834a | ||
|
|
901df79d86 | ||
|
|
7d67ce3681 | ||
|
|
65242ac3c7 | ||
|
|
318329a683 | ||
|
|
f29a3c3617 | ||
|
|
cd4f0179fc | ||
|
|
906d48702c | ||
|
|
050434325a | ||
|
|
102cc425b9 | ||
|
|
339d55d92f | ||
|
|
4659f3abc5 | ||
|
|
3499a3e356 | ||
|
|
194fd26693 |
@@ -0,0 +1,28 @@
|
||||
# Production — copier vers .env.prod (jamais commité) et adapter.
|
||||
# Utilisé avec : docker compose -f docker-compose.prod.yml --env-file .env.prod [pull|up -d]
|
||||
|
||||
# Image (registre Gitea). VERSION=latest par défaut ; pincer pour figer, ex. :
|
||||
# VERSION=v0.1.0 (le tag release inclut le "v", cf. release.yml)
|
||||
REGISTRY_IMAGE=gitea.thirdshop.fr/thirdshop/kazier
|
||||
VERSION=latest
|
||||
|
||||
# Port d'écoute exposé sur l'hôte (derrière Caddy/Nginx/TLS en prod publique)
|
||||
PORT=8080
|
||||
|
||||
# Postgres (mots de passe forts, générés, uniques à la prod)
|
||||
POSTGRES_DB=kazier
|
||||
POSTGRES_USER=kazier
|
||||
POSTGRES_PASSWORD=CHANGE_ME_GENERER_UN_SECRET_LONG
|
||||
|
||||
# Backend
|
||||
MAX_FILE_SIZE_MB=50
|
||||
OCR_LANG=fra+eng
|
||||
|
||||
# Sécurité — générer un secret long et aléatoire, ex. :
|
||||
# openssl rand -base64 48
|
||||
AUTH_SECRET=CHANGE_ME_GENERER_UN_SECRET_LONG
|
||||
|
||||
# Premier admin (créé UNIQUEMENT si la table users est vide au premier boot).
|
||||
# L'env n'écrase jamais un compte existant.
|
||||
ADMIN_USERNAME=admin
|
||||
ADMIN_PASSWORD=CHANGE_ME_MOT_DE_PASSE_INITIAL_FORT
|
||||
@@ -0,0 +1,244 @@
|
||||
# Pipeline Gitea Actions pour Kazier — lint + test + build + image registre.
|
||||
#
|
||||
# Prérequis act_runner (une seule fois) :
|
||||
# - Runner act_runner enregistré contre l'instance Gitea, labels `ubuntu-24.04` dispo.
|
||||
# - Job `docker` : s'exécute directement sur l'hôte du runner et utilise le
|
||||
# daemon Docker local (pas de conteneur `docker:27`, pas de DinD,
|
||||
# pas de `--privileged`, pas de montage `-v /var/run/docker.sock` : ce
|
||||
# dernier faisait échouer la création du conteneur sous act_runner avec
|
||||
# `Duplicate mount point: /var/run/docker.sock`, le runner injectant déjà
|
||||
# ce mount). L'hôte doit donc avoir un daemon Docker sain + le CLI docker.
|
||||
# - Gitea app.ini : [actions] ENABLED=true, DEFAULT_ACTIONS_URL=https://github.com
|
||||
# - Droits d'écriture du runner sur son répertoire de travail/cache.
|
||||
#
|
||||
# Secrets (Settings > Actions) :
|
||||
# - CONTAINER_REGISTRY_TOKEN PAT scope write:package (registre conteneur)
|
||||
# - CONTAINER_REGISTRY_USER utilisateur propriétaire du PAT ci-dessus
|
||||
|
||||
name: CI
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
tags: ["v*"]
|
||||
pull_request:
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
env:
|
||||
GO_VERSION: "1.26"
|
||||
ANDROID_IMAGE: cimg/android:2024.01-node
|
||||
CGO_ENABLED: "0"
|
||||
# Registre conteneur Gitea (ex: cname de l'instance)
|
||||
REGISTRY_HOST: gitea.thirdshop.fr
|
||||
|
||||
jobs:
|
||||
lint-backend:
|
||||
runs-on: ubuntu-24.04
|
||||
container: golang:${{ env.GO_VERSION }}-alpine
|
||||
steps:
|
||||
# golang:alpine n'a ni node ni git — requis par les actions JS
|
||||
# (checkout/cache) exécutées dans le container par act_runner.
|
||||
- name: Install node + git (actions JS)
|
||||
run: apk add --no-cache nodejs git
|
||||
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
token: ${{ secrets.GITEA_TOKEN }}
|
||||
|
||||
- uses: actions/cache@v4
|
||||
with:
|
||||
path: |
|
||||
/go/pkg/mod
|
||||
~/.cache/go-build
|
||||
key: go-${{ runner.os }}-${{ hashFiles('backend/go.mod', 'backend/go.sum') }}
|
||||
restore-keys: |
|
||||
go-${{ runner.os }}-
|
||||
|
||||
- run: go mod download
|
||||
working-directory: backend
|
||||
- run: go vet ./...
|
||||
working-directory: backend
|
||||
- run: go install honnef.co/go/tools/cmd/staticcheck@2026.2.1 && staticcheck ./...
|
||||
working-directory: backend
|
||||
|
||||
lint-mobile:
|
||||
runs-on: ubuntu-24.04
|
||||
container: ${{ env.ANDROID_IMAGE }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
token: ${{ secrets.GITEA_TOKEN }}
|
||||
- uses: actions/cache@v4
|
||||
with:
|
||||
path: |
|
||||
~/.gradle/caches
|
||||
~/.gradle/wrapper
|
||||
key: gradle-${{ runner.os }}-${{ hashFiles('mobile-kotlin/gradle/libs.versions.toml', 'mobile-kotlin/gradle.properties', 'mobile-kotlin/settings.gradle.kts', 'mobile-kotlin/build.gradle.kts', 'mobile-kotlin/app/build.gradle.kts') }}
|
||||
restore-keys: |
|
||||
gradle-${{ runner.os }}-
|
||||
- run: ./gradlew :app:lint -q
|
||||
working-directory: mobile-kotlin
|
||||
|
||||
test-backend:
|
||||
runs-on: ubuntu-24.04
|
||||
container: golang:${{ env.GO_VERSION }}-alpine
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:17-alpine
|
||||
env:
|
||||
POSTGRES_DB: kazier_test
|
||||
POSTGRES_USER: kazier
|
||||
POSTGRES_PASSWORD: kazier
|
||||
options: >-
|
||||
--health-cmd "pg_isready -U kazier"
|
||||
--health-interval 5s
|
||||
--health-timeout 3s
|
||||
--health-retries 10
|
||||
steps:
|
||||
- name: Install node + git (actions JS)
|
||||
run: apk add --no-cache nodejs git
|
||||
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
token: ${{ secrets.GITEA_TOKEN }}
|
||||
- uses: actions/cache@v4
|
||||
with:
|
||||
path: |
|
||||
/go/pkg/mod
|
||||
~/.cache/go-build
|
||||
key: go-${{ runner.os }}-${{ hashFiles('backend/go.mod', 'backend/go.sum') }}
|
||||
restore-keys: |
|
||||
go-${{ runner.os }}-
|
||||
- run: |
|
||||
set -euo pipefail
|
||||
# dbtest fait un skip silencieux si Postgres est injoignable : on
|
||||
# attend explicitement que le service accepte les connexions pour
|
||||
# que le job échoue (pas skip) si PG ne monte jamais.
|
||||
apk add --no-cache postgresql-client >/dev/null
|
||||
for i in $(seq 1 30); do
|
||||
if pg_isready -h postgres -U kazier -d kazier_test; then
|
||||
exit 0
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
echo "Postgres jamais prêt après 30s" >&2
|
||||
exit 1
|
||||
- run: go test ./...
|
||||
working-directory: backend
|
||||
env:
|
||||
TEST_DATABASE_URL: postgres://kazier:kazier@postgres:5432/kazier_test?sslmode=disable
|
||||
|
||||
test-mobile:
|
||||
runs-on: ubuntu-24.04
|
||||
container: ${{ env.ANDROID_IMAGE }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
token: ${{ secrets.GITEA_TOKEN }}
|
||||
- uses: actions/cache@v4
|
||||
with:
|
||||
path: |
|
||||
~/.gradle/caches
|
||||
~/.gradle/wrapper
|
||||
key: gradle-${{ runner.os }}-${{ hashFiles('mobile-kotlin/gradle/libs.versions.toml', 'mobile-kotlin/gradle.properties', 'mobile-kotlin/settings.gradle.kts', 'mobile-kotlin/build.gradle.kts', 'mobile-kotlin/app/build.gradle.kts') }}
|
||||
restore-keys: |
|
||||
gradle-${{ runner.os }}-
|
||||
- run: ./gradlew :app:testDebugUnitTest -q
|
||||
working-directory: mobile-kotlin
|
||||
|
||||
build-backend:
|
||||
runs-on: ubuntu-24.04
|
||||
container: golang:${{ env.GO_VERSION }}-alpine
|
||||
needs: [lint-backend, test-backend]
|
||||
steps:
|
||||
- name: Install node + git (actions JS)
|
||||
run: apk add --no-cache nodejs git
|
||||
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
token: ${{ secrets.GITEA_TOKEN }}
|
||||
- run: |
|
||||
set -euo pipefail
|
||||
export VERSION="$(cat VERSION)"
|
||||
CGO_ENABLED=0 GOOS=linux go build \
|
||||
-trimpath \
|
||||
-ldflags "-s -w -X main.version=${VERSION}" \
|
||||
-o bin/kazier-server \
|
||||
cmd/server/main.go
|
||||
./bin/kazier-server --version
|
||||
working-directory: backend
|
||||
# v4 demande le service d'artefacts v2 (@actions/artifact v2) non fourni
|
||||
# par Gitea (détecté comme GHES → GHESNotSupportedError). v3 reste le
|
||||
# protocole legacy supporté par l'API artefacts de Gitea.
|
||||
- uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: kazier-server
|
||||
path: backend/bin/kazier-server
|
||||
if-no-files-found: error
|
||||
|
||||
docker:
|
||||
runs-on: ubuntu-24.04
|
||||
# `build-backend` n'est qu'un garde-fou : le job docker ne télécharge pas
|
||||
# son artifact, le Dockerfile recompile le binaire lui-même (build local
|
||||
# autonome et reproductible). Le `needs` interdit juste de construire une
|
||||
# image si lint + test + build échouent.
|
||||
needs: [build-backend]
|
||||
# Push branches uniquement : pas sur PR (pas de build d'image à la review),
|
||||
# pas sur tag (c'est release.yml qui construit, évite une course dans le
|
||||
# registre entre le job `docker` et `release-docker`).
|
||||
if: github.event_name == 'push' && github.ref_type == 'branch'
|
||||
# S'exécute directement sur l'hôte du runner (pas de `container: docker:27`) :
|
||||
# le CLI docker de l'hôte parle au daemon local sans montage de socket.
|
||||
# L'ancien montage explicite `-v /var/run/docker.sock:/var/run/docker.sock`
|
||||
# via `container.options` faisait échouer la création du conteneur sous
|
||||
# act_runner (`Duplicate mount point: /var/run/docker.sock` — le runner
|
||||
# injecte déjà ce mount).
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
token: ${{ secrets.GITEA_TOKEN }}
|
||||
|
||||
- name: Sanity daemon Docker hôte
|
||||
run: |
|
||||
set -euo pipefail
|
||||
docker info >/dev/null
|
||||
echo "daemon Docker hôte joignable"
|
||||
|
||||
- name: Login registre Gitea
|
||||
env:
|
||||
CONTAINER_REGISTRY_TOKEN: ${{ secrets.CONTAINER_REGISTRY_TOKEN }}
|
||||
CONTAINER_REGISTRY_USER: ${{ secrets.CONTAINER_REGISTRY_USER }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
: "${CONTAINER_REGISTRY_TOKEN:?CONTAINER_REGISTRY_TOKEN manquant (PAT scope write:package)}"
|
||||
: "${CONTAINER_REGISTRY_USER:?CONTAINER_REGISTRY_USER manquant (propriétaire du PAT)}"
|
||||
echo "$CONTAINER_REGISTRY_TOKEN" | docker login "$REGISTRY_HOST" -u "$CONTAINER_REGISTRY_USER" --password-stdin
|
||||
# Le registre impose un namespace minuscule (github.repository_owner
|
||||
# peut être `Thirdshop` avec une majuscule → tag Docker invalide).
|
||||
OWNER_LC="$(printf '%s' "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')"
|
||||
echo "REGISTRY_IMAGE=${REGISTRY_HOST}/${OWNER_LC}/kazier" >> "$GITHUB_ENV"
|
||||
echo "SHA_TAG=sha-$(printf '%s' "$GITHUB_SHA" | cut -c1-7)" >> "$GITHUB_ENV"
|
||||
|
||||
- name: Build + push (registre Gitea)
|
||||
run: |
|
||||
set -euo pipefail
|
||||
# --pull : base alpine/golang fraîche ; --cache-from : réutilise les
|
||||
# layers du dernier build `main` si dispo (build plus rapide).
|
||||
docker pull "$REGISTRY_IMAGE:main" 2>/dev/null || true
|
||||
docker build \
|
||||
--pull \
|
||||
--cache-from "$REGISTRY_IMAGE:main" \
|
||||
--build-arg VERSION="$(cat VERSION)" \
|
||||
--build-arg BUILD_DATE="$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
|
||||
-t "$REGISTRY_IMAGE:main" \
|
||||
-t "$REGISTRY_IMAGE:$SHA_TAG" \
|
||||
./backend
|
||||
docker run --rm "$REGISTRY_IMAGE:$SHA_TAG" --version
|
||||
docker push "$REGISTRY_IMAGE:main"
|
||||
docker push "$REGISTRY_IMAGE:$SHA_TAG"
|
||||
@@ -0,0 +1,165 @@
|
||||
# Pipeline Gitea Actions pour Kazier — Release (tags vX.Y.Z uniquement).
|
||||
#
|
||||
# Construit l'APK signé (assembleRelease), crée la Release Gitea avec l'APK en
|
||||
# asset (API, équivalent de `glab release create`), puis pousse l'image
|
||||
# kazier-server sur le registre conteneur Gitea (tag + latest).
|
||||
# Déploiement perso uniquement : pas de push Docker Hub.
|
||||
#
|
||||
# Prérequis act_runner : job `release-docker` s'exécute directement sur
|
||||
# l'hôte et utilise le daemon Docker local (pas de conteneur `docker:27`,
|
||||
# pas de DinD, pas de montage `-v /var/run/docker.sock` — ce dernier faisait
|
||||
# échouer la création du conteneur sous act_runner avec
|
||||
# `Duplicate mount point: /var/run/docker.sock`, le runner injectant déjà ce
|
||||
# mount). L'hôte doit avoir un daemon Docker sain + le CLI docker.
|
||||
#
|
||||
# Secrets (Settings > Actions) :
|
||||
# - RELEASE_KEYSTORE_B64 base64 du keystore release Android (jamais en clair)
|
||||
# - SIGNING_STORE_PASSWORD SIGNING_KEY_ALIAS SIGNING_KEY_PASSWORD
|
||||
# - RELEASE_API_TOKEN PAT scope write:repository (API : création Release)
|
||||
# - CONTAINER_REGISTRY_TOKEN PAT scope write:package (registre conteneur Gitea)
|
||||
# - CONTAINER_REGISTRY_USER utilisateur propriétaire du PAT ci-dessus
|
||||
|
||||
name: Release
|
||||
|
||||
on:
|
||||
push:
|
||||
tags: ["v*"]
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
env:
|
||||
ANDROID_IMAGE: cimg/android:2024.01-node
|
||||
# API Gitea (host de l'instance)
|
||||
API_HOST: gitea.thirdshop.fr
|
||||
|
||||
jobs:
|
||||
release-apk:
|
||||
runs-on: ubuntu-24.04
|
||||
container: ${{ env.ANDROID_IMAGE }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
token: ${{ secrets.GITEA_TOKEN }}
|
||||
- uses: actions/cache@v4
|
||||
with:
|
||||
path: |
|
||||
~/.gradle/caches
|
||||
~/.gradle/wrapper
|
||||
key: gradle-${{ runner.os }}-${{ hashFiles('mobile-kotlin/gradle/libs.versions.toml', 'mobile-kotlin/gradle.properties', 'mobile-kotlin/settings.gradle.kts', 'mobile-kotlin/build.gradle.kts', 'mobile-kotlin/app/build.gradle.kts') }}
|
||||
restore-keys: |
|
||||
gradle-${{ runner.os }}-
|
||||
|
||||
- name: Signer + assembler l'APK release
|
||||
working-directory: mobile-kotlin
|
||||
shell: bash
|
||||
env:
|
||||
RELEASE_KEYSTORE_B64: ${{ secrets.RELEASE_KEYSTORE_B64 }}
|
||||
SIGNING_STORE_PASSWORD: ${{ secrets.SIGNING_STORE_PASSWORD }}
|
||||
SIGNING_KEY_ALIAS: ${{ secrets.SIGNING_KEY_ALIAS }}
|
||||
SIGNING_KEY_PASSWORD: ${{ secrets.SIGNING_KEY_PASSWORD }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
# Nettoie keystore + mots de passe même en cas d'échec du build.
|
||||
cleanup() { rm -f app/release.keystore app/signing.properties; }
|
||||
trap cleanup EXIT
|
||||
if [ -z "${RELEASE_KEYSTORE_B64:-}" ]; then
|
||||
echo "RELEASE_KEYSTORE_B64 manquant — release non signée." >&2
|
||||
exit 1
|
||||
fi
|
||||
: "${SIGNING_STORE_PASSWORD:?SIGNING_STORE_PASSWORD manquant}"
|
||||
: "${SIGNING_KEY_ALIAS:?SIGNING_KEY_ALIAS manquant}"
|
||||
: "${SIGNING_KEY_PASSWORD:?SIGNING_KEY_PASSWORD manquant}"
|
||||
echo "$RELEASE_KEYSTORE_B64" | base64 -d > app/release.keystore
|
||||
cat > app/signing.properties <<EOF
|
||||
storeFile=release.keystore
|
||||
storePassword=${SIGNING_STORE_PASSWORD}
|
||||
keyAlias=${SIGNING_KEY_ALIAS}
|
||||
keyPassword=${SIGNING_KEY_PASSWORD}
|
||||
EOF
|
||||
./gradlew :app:assembleRelease -q
|
||||
|
||||
- name: Créer la Release Gitea + upload APK
|
||||
shell: bash
|
||||
env:
|
||||
RELEASE_API_TOKEN: ${{ secrets.RELEASE_API_TOKEN }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
VERSION="$(cat VERSION)"
|
||||
TAG="v${VERSION}"
|
||||
API="https://${API_HOST}/api/v1"
|
||||
AUTH="Authorization: token ${RELEASE_API_TOKEN:?RELEASE_API_TOKEN manquant (PAT scope write:repository)}"
|
||||
NOTES_JSON="$(python3 -c 'import json;print(json.dumps(open("CHANGELOG.md", encoding="utf-8").read()))')"
|
||||
# Idempotent : si la release du tag existe déjà (re-tag, retry du job),
|
||||
# la réutiliser au lieu d'échouer en 409.
|
||||
if RELEASE_JSON="$(curl -sSf -H "${AUTH}" "${API}/repos/${GITHUB_REPOSITORY}/releases/tags/${TAG}" 2>/dev/null)"; then
|
||||
echo "Release ${TAG} existante — réutilisation."
|
||||
else
|
||||
PAYLOAD_JSON="$(TAG="${TAG}" BODY="$NOTES_JSON" python3 -c 'import json,os;print(json.dumps({"tag_name":os.environ["TAG"],"name":"Kazier "+os.environ["TAG"],"body":json.loads(os.environ["BODY"])}))')"
|
||||
RELEASE_JSON="$(curl -sSf -X POST "${API}/repos/${GITHUB_REPOSITORY}/releases" \
|
||||
-H "${AUTH}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "${PAYLOAD_JSON}")"
|
||||
fi
|
||||
RELEASE_ID="$(printf '%s' "${RELEASE_JSON}" | python3 -c 'import json,sys;print(json.load(sys.stdin)["id"])')"
|
||||
: "${RELEASE_ID:?id de release introuvable dans la réponse API}"
|
||||
# Asset idempotent : remplace l'APK existant de même nom s'il y en a un.
|
||||
APK_PATH="mobile-kotlin/app/build/outputs/apk/release/app-release.apk"
|
||||
EXISTING_ASSET_ID="$(curl -sSf -H "${AUTH}" "${API}/repos/${GITHUB_REPOSITORY}/releases/${RELEASE_ID}/assets" \
|
||||
| python3 -c 'import json,sys;assets=json.load(sys.stdin);print(next((str(a["id"]) for a in assets if a.get("name")=="app-release.apk"),""))')"
|
||||
if [ -n "${EXISTING_ASSET_ID}" ]; then
|
||||
echo "Asset existant ${EXISTING_ASSET_ID} — suppression avant re-upload."
|
||||
curl -sSf -X DELETE -H "${AUTH}" "${API}/repos/${GITHUB_REPOSITORY}/releases/${RELEASE_ID}/assets/${EXISTING_ASSET_ID}" > /dev/null
|
||||
fi
|
||||
curl -sSf -X POST \
|
||||
"${API}/repos/${GITHUB_REPOSITORY}/releases/${RELEASE_ID}/assets?name=app-release.apk" \
|
||||
-H "${AUTH}" \
|
||||
-F "attachment=@${APK_PATH}"
|
||||
|
||||
release-docker:
|
||||
runs-on: ubuntu-24.04
|
||||
# Directement sur l'hôte du runner (même raison que CI/docker : le montage
|
||||
# explicite du socket via `container.options` doublonne celui injecté par
|
||||
# act_runner → `Duplicate mount point`).
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
token: ${{ secrets.GITEA_TOKEN }}
|
||||
|
||||
- name: Sanity daemon Docker hôte
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
docker info >/dev/null
|
||||
echo "daemon Docker hôte joignable"
|
||||
|
||||
- name: Login registre Gitea
|
||||
shell: bash
|
||||
env:
|
||||
CONTAINER_REGISTRY_TOKEN: ${{ secrets.CONTAINER_REGISTRY_TOKEN }}
|
||||
CONTAINER_REGISTRY_USER: ${{ secrets.CONTAINER_REGISTRY_USER }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
: "${CONTAINER_REGISTRY_TOKEN:?CONTAINER_REGISTRY_TOKEN manquant (PAT scope write:package)}"
|
||||
: "${CONTAINER_REGISTRY_USER:?CONTAINER_REGISTRY_USER manquant (propriétaire du PAT)}"
|
||||
echo "$CONTAINER_REGISTRY_TOKEN" | docker login "$API_HOST" -u "$CONTAINER_REGISTRY_USER" --password-stdin
|
||||
OWNER_LC="$(printf '%s' "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')"
|
||||
echo "REGISTRY_IMAGE=${API_HOST}/${OWNER_LC}/kazier" >> "$GITHUB_ENV"
|
||||
|
||||
- name: "Build + push (registre Gitea : vX.Y.Z + latest)"
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
VERSION="$(cat VERSION)"
|
||||
docker pull "$REGISTRY_IMAGE:latest" 2>/dev/null || true
|
||||
docker build \
|
||||
--pull \
|
||||
--cache-from "$REGISTRY_IMAGE:latest" \
|
||||
--build-arg VERSION="${VERSION}" \
|
||||
--build-arg BUILD_DATE="$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
|
||||
-t "$REGISTRY_IMAGE:v${VERSION}" \
|
||||
-t "$REGISTRY_IMAGE:latest" \
|
||||
./backend
|
||||
docker run --rm "$REGISTRY_IMAGE:v${VERSION}" --version
|
||||
docker push "$REGISTRY_IMAGE:v${VERSION}"
|
||||
docker push "$REGISTRY_IMAGE:latest"
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
# Go backend
|
||||
backend/bin/
|
||||
backend/uploads/*
|
||||
!backend/uploads/.gitkeep
|
||||
|
||||
# Env local (jamais commité — cf. backend/.env.example)
|
||||
.env
|
||||
backend/.env
|
||||
|
||||
# Uploads locaux transitoires (racine / backend)
|
||||
uploads/
|
||||
backend/uploads/*
|
||||
|
||||
# Android / Gradle
|
||||
mobile-kotlin/local.properties
|
||||
mobile-kotlin/.gradle/
|
||||
mobile-kotlin/.kotlin/
|
||||
**/build/
|
||||
*.iml
|
||||
.idea/
|
||||
|
||||
# Signature release Android (jamais commité)
|
||||
signing.properties
|
||||
*.jks
|
||||
*.keystore
|
||||
!signing.properties.example
|
||||
|
||||
# Artifacts
|
||||
*.apk
|
||||
dist/
|
||||
@@ -1,8 +1,8 @@
|
||||
# VaultDrop
|
||||
# Kazier
|
||||
|
||||
## Project Status
|
||||
|
||||
`backend/` (Go) et `mobile-kotlin/` (Kotlin/Android) ont du scaffolding en place. Le client mobile est **local-first** : il reflète le SAF dans une base Room (v7, migrations, DAO) et pousse les **métadonnées** de ses mutations locales via un **outbox transactionnel** (`pending_operations` + worker WorkManager → `POST /sync/ops`). Pas d'upload physique, pas d'OCR ni de recherche serveur côté client en V1.
|
||||
`backend/` (Go) et `mobile-kotlin/` (Kotlin/Android) ont du scaffolding en place. Le client mobile est **local-first** : il reflète le SAF dans une base Room (v22, migrations, DAO) et pousse les **métadonnées** de ses mutations locales via un **outbox transactionnel** (`pending_operations` + worker WorkManager → `POST /sync/ops`). Upload physique ciblée (pipeline OCR : `POST /files/upload` + `resourceId`), streaming lecture des fichiers distants (`GET /files/:id/content`, jamais persisté + vignettes `GET /files/:id/preview` en cache), OCR serveur consommé côté client, recherche locale via Room FTS (+ facettes, + recherche serveur exposée), export chiffré (`POST /export`).
|
||||
|
||||
## Architecture
|
||||
|
||||
@@ -38,11 +38,11 @@ Tests mobiles : JVM unit tests dans `app/src/test` (JUnit + Robolectric + Room i
|
||||
- `config/` — env (`godotenv`, optionnel) + defaults: `PORT`, `DATABASE_URL`, `UPLOAD_DIR`, `MAX_FILE_SIZE_MB`, `OCR_LANG`, `AUTH_SECRET` (secret des tokens paseto), `ADMIN_USERNAME`/`ADMIN_PASSWORD` (bootstrap du premier admin)
|
||||
- `models/` — domain entities (users, devices, documents/resources, clients)
|
||||
- `service/` — business logic (permissions, upload, create folder, move, **sync outbox + snapshot**, bootstrap admin)
|
||||
- `handlers/` — HTTP handlers (health, devices register, **auth/login**, **users resolve + change password**, files CRUD/upload/search, folders, **sync/ops + sync/permissions, ocr/jobs** — réels) ; middleware `RequireAuth`
|
||||
- `repository/` — Postgres persistence (`repository.Resources` : insert/list/get/soft-delete scoping `owner_id`, search, move, rename, root-name unique index, `ListOwned` pour le snapshot ; `repository.Devices` : Upsert, Exists, MarkUser ; `repository.Operations` : trace outbox idempotente `(device_id, operation_id)` ; `repository.OcrJobs` : jobs queued→processing→done/failed ; `repository.Users` : GetByUsernameNormalized, ResolveExact, UpdatePassword) ; IDs sont TEXT 32-hex, `NewID()` = `crypto/rand` 16 octets hex (jamais UUID conversion, cf. `docs/api-v1.md`)
|
||||
- `db/` — package migrations (`golang-migrate/v4`, embarquées via `embed` dans `db/migrations/*.sql`, 000001→000008) : `db.MigrateDatabase(url)` au boot du serveur ; test harness `db/migrations_test.go` (up → assertions schéma → down, `TEST_DATABASE_URL`, skip si PG indisponible) ; `dbtest/` — helper cross-package pour les tests repo/handlers (crée la DB test si absente, reset schema, migrate ; skip si PG down)
|
||||
- `handlers/` — HTTP handlers (health, devices register/list/revoke, **auth/login**, **users resolve + change password (révoque les tokens) + usage**, files CRUD/upload/search (+facettes) , folders, **sync/ops + sync/permissions, ocr/jobs, files/:id/content, files/:id/preview, files/:id/shares, files/:id/links, files/:id/placements, files/:id/versions, files/:id/facts, files/:id/relations, trash, duplicates, export, groups CRUD + membres** — réels) ; middleware `RequireAuth` (+ révocation `tv`, devices si `EnforceDeviceExists`)
|
||||
- `repository/` — Postgres persistence (`repository.Resources` : insert/list/get/soft-delete scoping `owner_id`, search, move, rename, root-name unique index, `ListOwned` pour le snapshot, `OpenContent` (bytes streaming + lazy healing `has_content`), `ListShares`/`ListLinks` (owner-only), `SetHasContent` ; `repository.Devices` : Upsert, Exists, MarkUser ; `repository.Operations` : trace outbox idempotente `(device_id, operation_id)` ; `repository.OcrJobs` : jobs queued→processing→done/failed ; `repository.Users` : GetByUsernameNormalized, ResolveExact, UpdatePassword ; `repository.Shares` : Upsert/UpsertGroup/Revoke, CreateLink/RevokeLink, ListActiveForResource, ListActiveLinksForResource, snapshot `ListEffectivePermissions` (grants user + groupe) ; `repository.Groups` : Create/ListForUser/AddMember/RemoveMember/Delete (V2-6, un niveau, sans imbrication) ; `repository.Placements` : EnsureLocation/Report/ListForResource (V2 multi-device) ; IDs sont TEXT 32-hex, `NewID()` = `crypto/rand` 16 octets hex (jamais UUID conversion, cf. `docs/api-v1.md`)
|
||||
- `db/` — package migrations (`golang-migrate/v4`, embarquées via `embed` dans `db/migrations/*.sql`, 000001→000016) : `db.MigrateDatabase(url)` au boot du serveur ; test harness `db/migrations_test.go` (up → assertions schéma → down, `TEST_DATABASE_URL`, skip si PG indisponible) ; `dbtest/` — helper cross-package pour les tests repo/handlers (crée la DB test si absente, reset schema, migrate ; skip si PG down)
|
||||
- `ocr/` — OCR engine behind an interface (Tesseract system call, `OCR_LANG` défaut `fra+eng`)
|
||||
- `pkg/api/` — response helpers (`response.go`) ; `pkg/auth/` — tokens **paseto v4-local** (subject = `user_id`, claim = `device_id`, TTL 7j, voir `docs/api-v1.md`) ; `pkg/passwd/` — hashing/vérification bcrypt (timing-equal)
|
||||
- `pkg/api/` — response helpers (`response.go`) ; `pkg/auth/` — tokens **paseto v4-local** (subject = `user_id`, claim = `device_id`, TTL 7j, voir `docs/api-v1.md`) ; `pkg/passwd/` — hashing/vérification argon2id (comparaison `subtle`, délai égalisé)
|
||||
- File uploads stored in `backend/uploads/`
|
||||
- Standard JSON response envelope: `{ "data": ..., "meta": { "page": ..., "total": ... } }`
|
||||
- Error format: `{ "error": { "code": "...", "message": "..." } }`
|
||||
@@ -50,41 +50,43 @@ Tests mobiles : JVM unit tests dans `app/src/test` (JUnit + Robolectric + Room i
|
||||
|
||||
## Frontend Structure (mobile-kotlin)
|
||||
|
||||
- Entry point: `app/src/main/java/com/vaultdrop/mobile/VaultDropApplication.kt` (Hilt + `HiltWorkerFactory`) + `MainActivity.kt` (Hilt) ; navigation Compose dans `ui/navigation/` (`NavGraph.kt`, `VaultDropApp.kt`)
|
||||
- Entry point: `app/src/main/java/com/kazier/mobile/KazierApplication.kt` (Hilt + `HiltWorkerFactory`) + `MainActivity.kt` (Hilt) ; navigation Compose dans `ui/navigation/` (`NavGraph.kt`, `KazierApp.kt`)
|
||||
- Data layer — `data/`:
|
||||
- `data/local/` — Room SQLite (DB `dot.db`, **version 7**, `Migrations.kt` : tables `folders`, `files`, `user_preferences`, `pending_operations`) : entités Folder/File/UserPreference/PendingOperation, DAO, tri (`FileOrdering`) ; flag `processed` sur `files` (mode review « traiter », local au device, **gate de sync** : un fichier pas encore traité n'est jamais poussé) ; outbox transactionnel `pending_operations` (v7)
|
||||
- `data/remote/` — Retrofit/Moshi : `ApiService.kt` + `dto/Dtos.kt` = **contrat HTTP** (`{ data, meta }`, erreurs `{ error: { code, message } }`) ; `ApiClient.kt` normalise les réponses ; interceptors OkHttp (`AuthInterceptor`, `ServerUrlInterceptor`)
|
||||
- `data/repository/` — `FolderRepository`, `FileRepository`, `AuthRepository`, `OutboxRepository` (enqueue `create_resource`/`move_resource`/`delete_resource`, écrivains dans la même transaction Room que la mutation parente ; pour un fichier SAF, la mutation parente du `create_resource` est le **garder** de la review (`markProcessed`), et `hasCreateOperation` en garde l'unicité)
|
||||
- `data/local/` — Room SQLite (DB `dot.db`, **version 22**, `Migrations.kt` : tables `folders`, `files` (+ `category`, `processed`, `content`, `ocr_text`, `ocr_attempts`, `ocr_queued_at`, `bytes_uploaded`, `bytes_upload_attempts`, `has_content_server`, `wanted_offline`, `trashed_at`), `user_preferences`, `pending_operations` (+ `device_id`, `parent_resource_id`), `scan_sessions`, `scan_pages`, `resource_permissions`, `resource_facts`, `files_fts` (FTS4 + triggers `FtsTriggers`), `resource_links`, `collections`) : entités + DAO, tri (`FileOrdering`), schéma exporté (`app/schemas/`, m22) ; flag `processed` sur `files` (mode review « traiter », local au device, **gate de sync** : un fichier pas encore traité n'est jamais poussé) ; outbox transactionnel `pending_operations`
|
||||
- `data/remote/` — Retrofit/Moshi : `ApiService.kt` + `dto/Dtos.kt` = **contrat HTTP** (`{ data, meta }`, erreurs `{ error: { code, message } }`) ; `ApiClient.kt` normalise les réponses ; interceptors OkHttp (`AuthInterceptor`, `ServerUrlInterceptor`) ; `SafMultipart.kt` (multipart SAF paresseux partagé upload OCR + V2)
|
||||
- `data/repository/` — `FolderRepository`, `FileRepository`, `AuthRepository`, `OutboxRepository` (enqueue `create_resource` (+`scanSessionId` S5)/`move_resource`/`update_metadata` (+`baseUpdatedAt` S6)/`trash_resource`/`restore_resource`/`purge_resource` (V3, `delete_resource` legacy mappé serveur) + ops partage `share`/`update_share`/`revoke_share`/`create_link` (expiration 7j défaut)/`revoke_link` + `report_bytes` V2 (`{ hasBytes }`, nouvel `operation_id` par état) + `correct_facts` S3 (full-replace jeu `user`), écrivains dans la même transaction Room que la mutation parente ; pour un fichier SAF, la mutation parente du `create_resource` est le **garder** de la review (`markProcessed`), et `hasCreateOperation` en garde l'unicité), `ShareRepository` (hydratation + delta + `canAccess` depuis `GET /sync/permissions`), `OcrRepository` (upload ciblé + jobs + `syncFromServer`), `FactsRepository` (sync remplace-en-bloc + corrections optimistes), `LinksRepository` (graphe + verdicts REST), `TrashRepository` (restore/purge/convergence), `CollectionsRepository` (local), `DevicesRepository` (appareils + usage), `RemoteContentRepository` (streaming `GET /files/:id/content`, jamais persisté ; **upload V2** des octets après metadata synced, `bytes_uploaded` ; **download V2** vers SAF + rattachement, `wanted_offline`)
|
||||
- `features/` — logique descendue côté client :
|
||||
- `sync/DeviceSync.kt` + `SafScanner.kt` — sync device↔SAF : **two-pass walk** (listing hors transaction puis upserts Room dans une seule transaction), réconciliation `exists = 0` (jamais de suppression), single-flight via `Mutex` ; les nouveaux **dossiers** sont journalisés dans l'outbox à la découverte, les **fichiers** uniquement au « garder » de la review (gate `processed`)
|
||||
- `saf/` — `SafUris`, `SafFolderCreator`, `FileMover` (relocalisation SAF `DocumentsContract.moveDocument`, repli métadonnée seule ; le déplacement est poussé en `move_resource`), `SafFileDeleter` (suppression SAF + `delete_resource` atomique)
|
||||
- `sync/OutboxSyncWorker.kt` — worker WorkManager (`enqueueUniqueWork` KEEP, single-flight, `NetworkType.CONNECTED`, backoff 30s) qui draine `pending_operations` vers `POST /sync/ops` (batch 20, dead-letter immédiat `failed` sur erreur 4xx non-idempotente — `attempts` diagnostic, purge synced > 7 jours) ; **schedulé** au login/restauration de session (`AuthViewModel`) et après chaque `syncAll()` (`SyncViewModel`)
|
||||
- `saf/` — `SafUris`, `SafFolderCreator`, `FileMover` (relocalisation SAF `DocumentsContract.moveDocument`, repli métadonnée seule ; le déplacement est poussé en `move_resource`), `SafFileDeleter` (suppression SAF + `delete_resource` atomique), `FileDeleter.releaseLocalCopies` (purge sûre V2-3 : gate `isReleasable`, détachement cloud-only, zéro outbox)
|
||||
- `sync/OutboxSyncWorker.kt` — worker WorkManager (`enqueueUniqueWork` KEEP, single-flight, `NetworkType.CONNECTED`, backoff 30s) qui draine `pending_operations` vers `POST /sync/ops` (batch 20, dead-letter immédiat `failed` sur erreur 4xx non-idempotente — `attempts` diagnostic, purge synced > 7 jours, **sauf `report_bytes` : dead-letter + suite du batch, jamais bloquant**) ; **schedulé** au login/restauration de session (`AuthViewModel`) et après chaque `syncAll()` (`SyncViewModel`) ; enchaîne `OcrAutoWorker` + `BytesUploadWorker` quand un fichier est promu (`DownloadWorker` sur demande hors-ligne) ; `RetentionWorker` (passe quotidienne, purge sûre des copies anciennes selon `RetentionStore`, sans réseau)
|
||||
- `sync/SyncViewModel.kt` — état du sync exposé à l'UI (`SyncStatus` : marche en cours, ops en attente, dernières opérations) via le badge header `ui/components/SyncStatusBadge.kt` + `SyncOperationsDialog.kt` (« liste des sync »)
|
||||
- `auth/` — session (login user + token paseto) : `SessionManager`, `SecureTokenStore`, `TokenProvider`
|
||||
- `ui/` — écrans Compose : `folderlist`, `folderdetail`, `document` (contenu PDF/image/texte + placeholder cloud-only), `search` (**recherche locale** via Room, sans endpoint serveur), `settings` (URL serveur + thème), `pdfbuilder` (multi-select → génération PDF), `auth` (login, mode local), `components`, `theme`
|
||||
- `domain/` — stores de préférences: `DeviceIdentity`, `ActiveUserStore`, `LocalModeStore`, `ServerConfigStore`, `ThemePreferenceStore`, `GenerateId` (identifiants 32-hex)
|
||||
- `auth/` — session (login user + token paseto + **re-login silencieux** sur 401 via identifiants chiffrés persistés, single-flight, événements `Reauthenticated`/`SessionExpired`) : `SessionManager`, `SecureTokenStore`, `TokenProvider`, `TokenStore` (+ `Credentials`)
|
||||
- `ui/` — écrans Compose : `folderlist`, `folderdetail`, `document` (contenu PDF/image/texte local + streaming distant image/texte/PDF multi-pages via `RemoteDocumentViewer`, autres types non supportés en streaming + fiche faits/liens + partage ACTION_SEND), `search` (**recherche locale** via Room FTS + facettes + collections locales), `settings` (URL serveur + thème + stockage/retention + export chiffré + appareils + corbeille + doublons), `trash`, `duplicates`, `devices`, `facts` (bottom-sheet), `pdfbuilder` (multi-select → génération PDF), `auth` (login, mode local, avis d'expiration), `share` (`ShareBottomSheet` : grants + liens + ouverture de lien + copies V2 + groupes V2-6 ; `OpenLinkScreen` + deep-link `kazier://share/<token>`), `components`, `theme`
|
||||
- `domain/` — stores de préférences: `DeviceIdentity`, `ActiveUserStore`, `LocalModeStore`, `ServerConfigStore`, `ThemePreferenceStore`, `RetentionStore` (`all` | `last_months:N`), `GenerateId` (identifiants 32-hex)
|
||||
- `di/` — modules Hilt (`AppModule`, `DatabaseModule`, `NetworkModule`)
|
||||
- Base URL serveur **configurée à l'exécution** dans les Réglages (`ServerConfigStore`, persistée en `user_preferences`), défaut `http://10.0.2.2:8080/api/v1` (pas de variable d'env)
|
||||
- Client **local-first** : lecture via `GET /files/folders`, `GET /files` ; mutations locales poussées via l'outbox `POST /sync/ops` (`create_resource`/`move_resource`/`delete_resource`) ; `POST /devices`, `POST /auth/login` ; download/upload physique hors scope
|
||||
- Client **local-first** : lecture via `GET /files/folders`, `GET /files` (+ streaming distant `GET /files/:id/content` pour les fichiers cloud-only, jamais persisté) ; mutations locales poussées via l'outbox `POST /sync/ops` (`create_resource`/`move_resource`/`delete_resource` + ops partage) ; `POST /devices`, `POST /auth/login` ; upload physique limité au pipeline OCR (`POST /files/upload` + `resourceId` ciblé)
|
||||
|
||||
## Data Conventions
|
||||
|
||||
- Canonical identity for folders/files is `resource_id`: opaque 32-hex, generated locally (`GenerateId`), never reused. The physical `uri` (SAF) is nullable (NULL = cloud-only) and is the reconciliation key for the SAF walk (unique index, NULLs distincts).
|
||||
- Similarly, `pending_operations.operation_id` is a client-generated 32-hex (`GenerateId`), the idempotency key of the outbox `UNIQUE(device_id, operation_id)` lettre contre les rejeux du worker.
|
||||
- `pending_operations.operation_id` est un **TEXT 32-hex** généré par le client (`^[0-9a-f]{32}$`, CHECK-enforced côté serveur depuis la migration `000008`), distinct de `resource_id`. L'idempotence outbox reste **par device** : `UNIQUE(device_id, operation_id)` (colonne `device_id` côté mobile depuis Room v12).
|
||||
- `owner_id` is **nullable** in the Room schema; it is set at runtime from `DeviceIdentity.getOrCreate()` during the SAF walk.
|
||||
- Folder/file `sync_status` is a **placement** state: `local` | `cloud` | `local-cloud`. It is not a push progress marker.
|
||||
- Client storage: `user_preferences` (clé/valeur) pour l'identité device, le compte actif, l'URL serveur, le thème, le mode local.
|
||||
- Outbox atomique : `create_resource`/`move_resource`/`delete_resource` sont écrits dans la **même transaction Room** que la mutation parente ; l'écrivain (repository) et le worker partagent ce contrat. En mode local (pas de token), le worker est no-op. **Gate `processed`** : un fichier SAF nouvellement découvert est importé localement mais poussé seulement au « garder » (`markProcessed`, qui enqueue le `create_resource` dans la même transaction, une seule fois via `hasCreateOperation`) — d'où le filtrage des documents par l'utilisateur avant toute synchro. Un `delete_resource` n'est journalisé que si un `create_resource` a existé (jamais pour un fichier non poussé).
|
||||
- Pas de cache de permissions côté client en V1 : le serveur reste la source de vérité pour l'accès (`GET /sync/permissions` non consommé par le client Kotlin).
|
||||
- Cache de permissions côté client : `ShareRepository` hydrate `resource_permissions` depuis `GET /sync/permissions` (delta + pull complet, TTL 24h → downgrade lecture seule) et expose `canAccess` ; le serveur reste la source de vérité.
|
||||
|
||||
## API Contract (V1)
|
||||
|
||||
- **Le client mobile est le contrat**: les formes d'endpoints dans `mobile-kotlin/.../data/remote/ApiService.kt` + `dto/Dtos.kt` sont autoritatives et doivent matcher exactement ; le serveur ne renégocie pas. Spec consolidée: `docs/api-v1.md`.
|
||||
- **Identity (user-first, V1 finale)**: le device s'enregistre (`POST /devices`, `{ "deviceId" }` seul, sans token), puis `POST /auth/login` (`username` + `password` + `device_id`) émet le seul token paseto **v4-local** — subject = `user_id` (autorise, scoping de toutes les ressources), claim `device_id` (porté, non autorisant seul), **TTL 7j sans refresh**. À expiration, le client re-logine.
|
||||
- **Identity (user-first, V1 finale)**: le device s'enregistre (`POST /devices`, `{ "deviceId" }` seul, sans token), puis `POST /auth/login` (`username` + `password` + `device_id`) émet le seul token paseto **v4-local** — subject = `user_id` (autorise, scoping de toutes les ressources), claim `device_id` (porté, non autorisant seul), **TTL 7j sans refresh**. À expiration, le client re-logine **silencieusement** via les identifiants chiffrés persistés (`SecureTokenStore`, single-flight, événements `Reauthenticated`/`SessionExpired`), sans rejouer la requête en vol.
|
||||
- **Bootstrap**: au premier démarrage, si `users` est vide, `ADMIN_USERNAME`/`ADMIN_PASSWORD` (env) créent le premier admin ; absents → le serveur refuse de démarrer. L'env n'écrase jamais un compte existant. Usernames résolus sur `username_normalized` (lowercase + trim), `GET /users/resolve` exact uniquement (pas d'énumération).
|
||||
- **Identifiers**: `resource_id` / `device_user_id` / `user_id` / share-link `token` / `operation_id` sont des **lowercase 32-hex** TEXT (`^[0-9a-f]{32}$`, CHECK-enforced), stockés tels quels côté serveur (pas de conversion UUID). Le mobile génère toujours 32-hex.
|
||||
- **Outbox idempotence + ordering**: le serveur applique les batchs de `pending_operations` **séquentiellement**, s'arrête à la première erreur non-idempotente et retourne l'index atteint. Consommé par le client Kotlin (`OutboxSyncWorker` → `POST /sync/ops`, batch 20, dead-letter immédiat sur 4xx non-idempotente, purge > 7 jours). `create_resource` porte le `parentResourceId` optionnel (racine si absent) — le walk SAF garantit parent avant enfant dans l'ordre d'`id`.
|
||||
- **Permissions snapshot**: le serveur pousse des snapshots `resource_permissions` (`effective_access` ranking viewer < commenter < editor < owner, `inherit`, `expires_at`, TTL 24h → read-only downgrade). (Endpoint existant côté serveur ; pas encore consommé par le client Kotlin.)
|
||||
- **Outbox idempotence + ordering**: le serveur applique les batchs de `pending_operations` **séquentiellement**, s'arrête à la première erreur non-idempotente et retourne l'index atteint. Consommé par le client Kotlin (`OutboxSyncWorker` → `POST /sync/ops`, batch 20, dead-letter immédiat sur 4xx non-idempotente, purge > 7 jours). `create_resource` porte le `parentResourceId` optionnel (racine si absent) ; `report_bytes` (`{ hasBytes }`, V2 placements, nouvel `operation_id` par état) ; `share`/`update_share` acceptent `granteeUserId` XOR `granteeGroupId` (V2-6) — le walk SAF garantit parent avant enfant dans l'ordre d'`id`.
|
||||
- **Permissions snapshot**: le serveur pousse des snapshots `resource_permissions` (`effective_access` ranking viewer < commenter < editor < owner, `inherit`, `expires_at`, TTL 24h → read-only downgrade). Consommé par le client Kotlin (`ShareRepository` : hydratation + delta + `canAccess`).
|
||||
|
||||
- **Lecture partagée** : `GET /files/:id/shares` + `GET /files/:id/links` (owner uniquement, 404 sinon) exposent grants et liens au propriétaire ; `GET /files/:id/content` streame les octets (viewer+, `Range` supporté).
|
||||
|
||||
## Non-Goals (V1)
|
||||
|
||||
@@ -92,7 +94,7 @@ Tests mobiles : JVM unit tests dans `app/src/test` (JUnit + Robolectric + Room i
|
||||
- On-device OCR
|
||||
- Full multi-tenant federation / public discovery
|
||||
- Multi-writer sync conflicts (single-owner device identity)
|
||||
- Upload physique client→cloud (métadonnées seules via l'outbox, download, OCR jobs, permissions) côté mobile Kotlin
|
||||
- Upload physique généralisé client→cloud et download persistant (seuls l'upload ciblé OCR et le streaming lecture existent, cf. `docs/api-v1.md`)
|
||||
|
||||
## References
|
||||
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
# Changelog
|
||||
|
||||
Toutes les modifications notables de Kazier sont documentées ici.
|
||||
Le format suit [Keep a Changelog](https://keepachangelog.com/fr/1.1.0/) et le versioning est [SemVer](https://semver.org/lang/fr/).
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [1.2.0] - 2026-09-26
|
||||
|
||||
V3 « Le classeur qui comprend » : filet anti-perte, extraction générique,
|
||||
recherche plein-texte + facettes, graphe documentaire, confiance & partage
|
||||
sobre, preview cloud, export chiffré. Contrat consolidé (`docs/api-v1.md`),
|
||||
vision et plan (`V3.md`).
|
||||
|
||||
### Ajouté
|
||||
- Corbeille V3 (S1 backend) : `DELETE /files/:id` = trash par défaut (`?mode=purge` = définitif), `POST /files/:id/restore`, `GET /trash` (paginé), trash/restore récursifs (subtree + chaîne d'ancêtres), purge auto serveur après 30j (`TrashRetention`, passe au boot + horaire).
|
||||
- Versions V3 (S1 backend) : table `resource_versions` (N=3, élagage auto), historique figé à chaque overwrite (`Upload` ciblé), `GET /files/:id/versions` + `POST /files/:id/versions/:n/restore` (l'actuel est à son tour versionné), upload ciblé vers trashé → `NOT_FOUND`.
|
||||
- Outbox : ops `trash_resource` / `restore_resource` / `purge_resource` (owner-only, `delete_resource` legacy mappé vers trash, même idempotence) ; purge d'octets best-effort côté serveur.
|
||||
- Migrations `000017` (index corbeille) + `000018` (versions), contrat `docs/api-v1.md` §3/§6.1 étendu, test de contrat `router_test.go`.
|
||||
- Extract générique V3 (S2 backend) : package `extract` (règles FR stdlib — dates/échéances/périodes/montants/émetteurs/références masquées/`doc_type` suggéré, confiances, corpus testé), table `resource_facts` (migration `000019`, `origin` auto/user), hook worker OCR (`OnText` + backfill boot borné, best-effort, anti-panic), `GET /files/:id/facts` (viewer+, `[]` si vide, 404 si trashé/étranger).
|
||||
- Corrections V3 (S3) : `PATCH /files/:id/facts` + op `correct_facts` (full-replace du jeu `user`, editor+, validation Go → 400 jamais 500), Room v20 (`resource_facts` + `FactsDao`, migration 19→20), fiche « Ce qu'on a compris » sur le viewer (chips, confiance grisée + suggéré, édition par type, reset auto), `FactsRepository` (sync remplace-en-bloc, correction optimiste + outbox atomique), `GET /files/:id/facts` consommé côté client.
|
||||
- Recherche V3 (S4) : colonne `search_tsv` (migration `000020`, `french`, index GIN) + `SearchDocumentsVisible` (nom LIKE + TS + faits, rang nom > TS > fait sûr > fait douteux, facettes émetteur/type/montant/période), `GET /files/search` étendu (400 si facette malformée), Room v21 (FTS4 `files_fts` + triggers partagés fresh-install/migration, entité déclarée pour KSP), `searchDocuments` offline (même rang, accents préservés) + UI filtres (émetteur, types chips, montants, période ISO, compteur, effacer).
|
||||
- Relations V3 (S5) : table `resource_links` + `scan_session_id` (migration `000021`), réconciliation déterministe (same_scan via outbox, doublons via SHA-256 posé à l'upload + backfill boot, same_issuer/référence via faits effectifs, verdicts préservés, élagage auto), `GET /files/:id/relations` + verdict (owner-only), `GET /duplicates` (groupes + gain récupérable), Room v22 (links + collections), « Voir aussi » dans la fiche (verdicts en ligne), écran Doublons (garder le plus récent), collections locales (sauver/rejouer/supprimer depuis la recherche), `scanSessionId` dans les creates de scan.
|
||||
- Confiance V3 (S6) : `token_version` (migration `000022`, claim `tv`, 401 si révoqué, bump au changement de mot de passe — fin de la limite V1), quota (colonne héritée de `000001`, `000023` idempotente, 413 `QUOTA_EXCEEDED` sur créations, `GET /users/me/usage`), appareils (`GET /devices`, `DELETE /devices/:id`, `EnforceDeviceExists` en prod, écran Appareils + quota), renames concurrents transparents (`baseUpdatedAt` + `conflicts[]` non-fataux, gracieuseté 2 s, pipeline mobile persistance + dialogue), liens à expiration 7j par défaut, import « classeur » unifié (scan/note/dossier), onboarding cercle Famille.
|
||||
- Corbeille V3 (S1 mobile, Room v19) : colonne `trashed_at` (files + folders, migration 18→19), tombstone `exists = 0` (requêtes `exists = 1` inchangées), `FileDeleter`/`FolderDeleter`/`SafFileDeleter` → ops `trash_resource` (plus de `delete_resource` émis), garde walk anti-résurrection + reconcile, `TrashRepository` (restore/purge/convergence `GET /trash`/expiration locale 31j), écran Corbeille (Réglages → restore/purge avec confirmation), expiration tombstones dans `RetentionWorker`, dialogue de suppression rewordé (corbeille 30j, plus « irréversible »).
|
||||
- Preview cloud V3 (S7) : table `resource_previews` (migration `000024`), `GET /files/:id/preview` (JPEG 512, images pur Go, PDF page 1 via `pdftoppm` + poppler au Dockerfile, cache régénéré sur changement, 404 `NO_PREVIEW` sinon), vignettes distantes côté mobile (une requête/session puis cache WebP versionné, garde `tried`, icône sinon).
|
||||
- Export chiffré V3 (S7 C3) : `POST /export { password 8+ }` → zip AES-256 streamé (fichiers vifs possédés + `manifest.json` avec faits, tout chiffré, metadata-only sans octets), mobile (mot de passe jamais persisté, téléchargement cache, partage système via FileProvider), bouton Partager (ACTION_SEND local) sur le viewer — impression papier reportée (V4, pas de dep print).
|
||||
|
||||
## [1.1.0] - 2026-09-25
|
||||
|
||||
### Ajouté
|
||||
- Upload V2 des octets : worker `BytesUploadWorker` (fichier par fichier, overwrite ciblée idempotente, cap 3 échecs, backoff), marqueurs `bytes_uploaded`/`bytes_upload_attempts` (Room v17, migration 16→17), multipart SAF mutualisé (`SafMultipart.kt`).
|
||||
- Flag serveur `has_content` (migration `000014`, lazy healing : TRUE à l'upload / au `OpenContent` réussi, FALSE si octets disparus), exposé en `FileDto.hasContent` + miroir Room `has_content_server` (v18).
|
||||
- Download V2 hors-ligne : `DownloadWorker` (fichier par fichier, écriture SAF dossier propre sinon racine, rattachement même `resource_id`, walk convergent par URI), marqueurs `has_content_server`/`wanted_offline` (Room v18, migration 17→18), bouton « Rendre disponible hors-ligne » dans le viewer distant.
|
||||
- Purge sûre V2-3 « libérer de l'espace » : gate `isReleasable` (copie serveur connue), détachement cloud-only sans op outbox, bouton viewer + action multi-sélection.
|
||||
- Placements multi-device : tables `storage_locations` + `resource_placements` (migration `000015`), op outbox `report_bytes` (viewer+, nouvel `operation_id` par état, dead-letter sans bloquer la file), `GET /files/:id/placements` (owner-only), reporting auto (upload/download/release/delete locale), section copies dans le sheet de partage.
|
||||
- Rétention auto : règle `all` | `last_months:N` (`RetentionStore`), passe quotidienne `RetentionWorker` (purge sûre, sans réseau), réglages stockage (stats récupérables + libération manuelle).
|
||||
- Groupes V2-6 (backend) : tables `groups` + `group_members` (migration `000016`, un niveau), CRUD + membres (owner-only), partage vers groupe via outbox (`granteeGroupId`, snapshot + enforcement étendus), `GROUP_NOT_FOUND`, lecture grants avec `granteeKind`.
|
||||
- Groupes V2-6 (mobile) : section groupes du sheet (création, membres, suppression, partage au groupe), badge groupe sur les grants, endpoints CRUD + membres.
|
||||
|
||||
### Corrigé
|
||||
- 401 sans session (mode local / jamais connecté) : ignoré sans purge ni événement — un appel égaré n'éjecte plus vers le login.
|
||||
|
||||
## [1.0.0] - 2026-09-25
|
||||
|
||||
### Ajouté
|
||||
- Streaming lecture des fichiers distants : `GET /files/:id/content` (binaire brut, `Range`, viewer+), viewer mobile à la demande sans persistance (image/texte/PDF multi-pages, autres types non supportés).
|
||||
- Gestion du partage côté owner : `GET /files/:id/shares` + `GET /files/:id/links`, sheet mobile (grants modifiables/révocables, liens création/copie/révocation, ouverture de lien, badge pending outbox), deep-link `kazier://share/<token>` + écran de résolution.
|
||||
- Re-login silencieux : identifiants chiffrés persistés, renouvellement auto sur 401 (single-flight, file conservée si même compte), écran login avec avis d'expiration + username pré-rempli.
|
||||
- Pipeline de release CI (GitLab) : lint, tests backend/mobile, build Docker, signature APK release, publication GitLab Release + Docker Hub.
|
||||
- `backend/Dockerfile` multi-stage : image de production avec Tesseract (fra+eng) embarqué, user non-root, labels OCI.
|
||||
- Service `backend` actif dans `docker-compose.yml` (déploiement conteneurisé).
|
||||
- Signature release Android (`signing.properties` non commité, `assembleRelease`).
|
||||
- Scripts `mise build:release`, `mise docker:build`, `mise release`.
|
||||
- Licence AGPLv3, badge README, `CHANGELOG.md`.
|
||||
- Versioning Android aligné sur le `VERSION` racine (versionCode dérivé).
|
||||
|
||||
### Changé
|
||||
- **Renommage : VaultDrop → Kazier.** Marque, package Android (`com.kazier.mobile`), module Go (`github.com/kazier/backend`), binaire/image `kazier-server`, creds Postgres par défaut. Le code interne `dot.db` est conservé.
|
||||
|
||||
### Corrigé
|
||||
- Docs : en-tête `docs/api-v1.md` (référence à l'ancien client Expo remplacée par le client Kotlin), quickstart et docker de la doc Starlight.
|
||||
- `.gitignore` racine (uploads, binaires, keystore, artifacts).
|
||||
|
||||
## [0.1.0] - 2026-09-15
|
||||
|
||||
Première version publique.
|
||||
|
||||
### Ajouté
|
||||
- Backend Go/Gin : auth user-first (paseto v4-local), devices, users (resolve, change password), CRUD fichiers/dossiers, upload multipart, recherche, sharing, sync outbox idempotente + snapshot permissions, OCR jobs (Tesseract), migrations Postgres testées.
|
||||
- Client Android Kotlin : local-first Room (v7, migrations, DAO), SAF scanner deux passes, outbox transactionnel + worker WorkManager, recherche locale, génération PDF, scanner de documents, thumbnails, mode sombre, i18n FR/EN.
|
||||
- Contrat API V1 consolidé (`docs/api-v1.md`), test de contrat de routes.
|
||||
- Site de promotion public + documentation Starlight/Astro (FR/EN, versionnée).
|
||||
+158
@@ -0,0 +1,158 @@
|
||||
# FEEDBACK — Audit bugs mobile-kotlin (local-first)
|
||||
|
||||
> Généré le 2026-09-22 par 5 agents d'exploration parallèles (scopes : Room/DB, sync/outbox/SAF, réseau/auth/repos, UI/navigation/VM, scan/pdf/review/thumbnails).
|
||||
> Mise à jour le 2026-09-24 : les items corrigés par `d89c0d0 (fix 1 to 6)` et `22286aa (fix mobile audit review)` ont été re-vérifiés code en main et retirés. Ce document ne liste que le **reliquat confirmé**.
|
||||
> Màj 2026-09-24 (soir) : tout le reliquat ci-dessous est **corrigé et testé** (`:app:testDebugUnitTest` : 162/162 verts, DB v16, schéma `app/schemas/16.json` exporté). Les sections 🟠/🟡/🛠️ sont conservées comme traçabilité.
|
||||
> Chaque finding restant est vérifié avec preuve `file:line`.
|
||||
> Scope : `mobile-kotlin/` uniquement.
|
||||
> Légende : 🟠 Majeur (divergence silencieuse, contrat violé) · 🟡 Mineur (UX, perf, dette).
|
||||
|
||||
Base paths : `mobile-kotlin/app/src/main/java/com/kazier/mobile/…`
|
||||
|
||||
---
|
||||
|
||||
## ✅ Corrigés et retirés (vérifiés le 2026-09-24)
|
||||
|
||||
### 🔴 Critiques : 9/9 corrigés
|
||||
- **C1** `domain/GenerateId.kt:19` — masque `and 0xFF` contre la sign-extension, 32-hex conforme.
|
||||
- **C2** `features/sync/OutboxSyncWorker.kt:139-145` — erreurs non-attribuables (`INVALID_RESPONSE`, 429/408, 4xx batch-level) → `Result.retry()` sans toucher aux ops.
|
||||
- **C3** `data/local/dao/PendingOperationDao.kt:66-72` — `purgeSynced` exclut les `create_resource` (tombstones), gates `countCreateOperations` préservés.
|
||||
- **C4** `features/saf/FileDeleter.kt:122-128` + `FolderDeleter.kt:122-124` — journal-first (`withTransaction` avant suppression SAF) + `sweepPendingPhysicalDeletes`.
|
||||
- **C5** `auth/SessionManager.kt:70-77` — `clear()` purge les ops non poussées (`pending`/`failed`), appelé par `AuthViewModel.signOut/signOutToLocal`.
|
||||
- **C6** `OutboxSyncWorker.kt:185-188` — stop au 1er échec (`return success()`, jamais `continue`) + cascade transitive `cascadeCreateFailure` via `parent_resource_id`/payload.
|
||||
- **C7** `OutboxSyncWorker.kt:152,170-174,193-196` — `applied.coerceIn`, `failedIndex < 0 → stop`, `applied == 0 → stop` (anti busy-loop/crash).
|
||||
- **C8** Scan export — `ScanExport.pendingForExport` idempotent, `shouldFinishSession` gate `DONE`, `uniquePageFile()` UUID, `deletePage` DB-d'abord, `exportPdf` ne marque que les succès.
|
||||
- **C9** `features/scan/ScanImageProcessor.kt:91-92` + `ScanViewModel.kt:213` — jamais de `recycle()` sur l'alias affiché (`!==` check).
|
||||
|
||||
### 🟠 Majeurs corrigés (31)
|
||||
M1 (garde pending-move dossiers), M2 (gate `create` avant `move`), M3 (`FileMover` sans repli mensonger), M4 (exclusion `exists=0` pending seul), M6 (parent inconnu → `continue`), M7 (`importRoot` schedule le drain), M8 (fichiers 0-octet acceptés), M10 (`DeviceIdentity` mutex + `insertIgnore`), M11 (décision assumée : pas de backfill `create_resource`), M12 (`upsertTolerant` sur `resource_id`/`uri`), M14 (colonne `device_id`, `UNIQUE(device_id, operation_id)` + migration 11→12), M15 (`failDescendants` élargi, cf. C6), M21 (boucle `listFilesPaged` + `meta.total`), M22 (`ApiClient` distingue `CancellationException`/`JsonDataException`/`IOException`), M23 (`activeUserStore` vs `ownerId`, plus de confusion device/user), M24 (`normalizePayloadNumbers` Double→Long), M25 (`SessionManager` possède `onUnauthorized`), M26 (`ServerUrlInterceptor` `runBlocking load()` anti-race), M28 (validation URL `toHttpUrl()`), M29 (`pendingDefaultPick` saveable), M30 (pager `rememberSaveable`), M31 (debounce sur résultats seuls), M32 (gate `canShare` FolderDetail), M33 (`refreshShareGate` au login/logout), M34 (`selection.clear()` + chips figés en sélection), M35 (`resolveViewerPage` clamp), M36 (sélecteur thème 3 états), M37 (flag `dirty` anti-écrasement saisie), M38 (`MAX(sortOrder)+1`), M42 (`processed=true` PDF builder), M43 (réinsertion optimiste review), M44 (`remember(existingIds)` stable).
|
||||
|
||||
### 🟡 Mineurs corrigés dans `d89c0d0` (voir `git show --stat d89c0d0`)
|
||||
m4 (gardes cursor SAF), m6 (`deleteLocally` transactionnel), m8 (`FileOrdering` aligné SQL), m9 (commentaire `attempts` diagnostic), m10-m14 (`updateUri`, `LocalModeStore`, `load` réarmable, `StateFlow` après succès DB, clé `SYNC_MODE` retirée), m16-m18 (index, `defaultValue processed`, `swapOrder` contraint), m22 (`ApiService` endpoints contrat ajoutés), m23 (`registerDevice` erreurs visibles), m24 (garde mode local), m25 (logging sans PII), m26 (`Bearer` hors routes publiques), m27 (`expiresAt` persisté), m28 (`SecureTokenStore` lazy + Direct Boot), m29-m31 (nav `pdfbuilder/empty`, `launchSingleTop`, controller hoisté), m33-m39 (side-effects `buildPhase`, dialogs `rememberSaveable`, badge collectes, saisie serveur debouncée, flash thème, bandeau import, `FileStatus.LOCAL`), m40-m56 (scan/camera/thumbnails : résolution bornée, `toBitmap` throttlé, listener retiré, `else` rotation, garde `sourceRect`, arrondi crop, `combine` nettoyé, purge `ABANDONED`, unicité session active, `FileCategory` CSV cohérent, purge map `Mutex`, `.tmp` nettoyés, `isUpToDate` taille+mtime, `runCatching` close, commentaire `max`, `release()` sur exception, deck conservé sur erreur).
|
||||
|
||||
---
|
||||
|
||||
## 🟠 Majeurs restants (4) — ✅ tous corrigés le 2026-09-24 (162/162 tests verts)
|
||||
|
||||
- **M5 — `features/saf/FileDeleter.kt:88-104` + `features/saf/FolderDeleter.kt:91-103`** : `deleteInCloud` sans tombstone locale (`exists` reste 1) → ressource toujours affichée, walk la revoit, `refreshFromServer` ne supprime jamais de lignes → fantôme local vs serveur supprimé.
|
||||
- **M9 — `features/saf/FolderMerger.kt:59,65-101`** : merge toujours multi-transactions (create, moves, deletes séparés) ; seule idempotence `findExistingMerged:154-161` ajoutée, pas d'atomicité → crash à mi-fusion = état partiel poussé.
|
||||
- **M13 — entités `exists: Int? = null` + filtres `= 1`** (`FolderEntity.kt:36`, `FileEntity.kt:46`, `FolderDao.kt:43,50`, `FileDao.kt:27,44`) : toute ligne sans `exists` explicite disparaît UI/recherche/review — perte fonctionnelle silencieuse.
|
||||
- **M27 — `data/repository/ShareRepository.kt:47` + `data/remote/dto/Dtos.kt:150-151`** : snapshot permissions sans delta (`after` jamais passé), `sharedById`/`expiresAt` en `Any?` jamais lus, `cachedAt` jamais persisté, aucun downgrade 24h ni `canAccess` → grant révoqué/expiré reste visible et ouvrable (`404`) jusqu'au prochain login.
|
||||
|
||||
---
|
||||
|
||||
## 🟡 Mineurs restants (8) — ✅ tous corrigés le 2026-09-24 (162/162 tests verts)
|
||||
|
||||
### Sync / Worker / SAF
|
||||
- **m1 — `features/sync/SyncViewModel.kt:108-133`** : `_walkInProgress` sans `finally` dans la boucle (reste `true` si scope annulé ; seul `importRoot:173` a un `finally`) → badge « syncing » collé ; la fenêtre inclut l'appel réseau partage.
|
||||
- **m2 — `features/sync/SyncViewModel.kt:110-116`** : `OutboxSyncWorker.enqueue` + `OcrAutoWorker.enqueue` à chaque tick même sans changement → réveils WorkManager / 30s pour rien.
|
||||
- **m5 — `features/sync/DeviceSync.kt:76-162`** : « transaction courte » en réalité géante (N upserts + `getAll()` dossiers **et** fichiers par racine sous verrou écriture) → lecteurs Room bloqués, jank/ANR sur grosses arborescences.
|
||||
|
||||
### Room / Domain / DI
|
||||
- **m7 — `data/local/dao/FileDao.kt:45`** : `searchWithFilters` `LIKE` brut sans `ESCAPE`, casse ASCII-only → `%`/`_` = jokers (match-all), « ete » ne trouve pas « ÉTÉ ».
|
||||
- **m15 — `di/DatabaseModule.kt:25-28`** : aucune stratégie de downgrade → APK plus ancien sur DB v14 = crash `IllegalStateException` au lieu de dégradation contrôlée.
|
||||
- **m19 — `data/local/AppDatabase.kt:36-37`** : `version=14, exportSchema=false`, migrations non snapshotées → dérives indétectables en CI.
|
||||
|
||||
### Sync contrat / OCR
|
||||
- **m20 — `data/repository/OcrRepository.kt:66-73`** : `poll()` infini (`while(true)` + 3s, sans timeout) alors qu'un job peut rester `processing` longtemps (stale claim serveur) → coroutine tenue indéfiniment.
|
||||
- **m21 — `data/repository/OutboxRepository.kt:72-147`** : ops contrat non émissibles (`update_metadata`, `update_share`, `create_link`, `revoke_link` — §6.1 = 9 ops, helpers partiels) → rename partagé, update share, liens publics inopérants.
|
||||
|
||||
---
|
||||
|
||||
## 🛠️ Plans de correction (reliquat) — ✅ implémentés le 2026-09-24
|
||||
|
||||
Ordre suivi : M13 → M5 → M27+m21 → M9 → mineurs par lot (m1+m2, m5, m7, m20, m15+m19).
|
||||
Écarts d'implémentation vs plan : M13 sans rewrite (colonne restée nullable, défaut Kotlin 1 + backfill — pas de bump de contrainte) ; m7 `COLLATE NOCASE` sans colonne normalisée (repli Unicode « ete » → « ÉTÉ » toujours ouvert, voir suivi) ; M27 `cachedAt` persisté = heure locale du pull (pas l'horloge serveur, skew).
|
||||
|
||||
### M5 — `deleteInCloud` sans tombstone locale
|
||||
Fichiers : `features/saf/FileDeleter.kt:88-104`, `features/saf/FolderDeleter.kt:91-103` (+ descendants via `collectDescendants`).
|
||||
Constat : `deleteInCloud` enqueue `delete_resource` mais laisse `exists = 1` ; la ressource reste affichée et le walk la revoit.
|
||||
Plan :
|
||||
1. Dans `FileDeleter.deleteInCloud`, après `enqueueDeleteResource`, appeler `fileRepository.markMissing(resourceId, now)` dans la même transaction Room (ou `appDatabase.withTransaction` comme `deleteFull`).
|
||||
2. Dans `FolderDeleter.deleteInCloud`, idem pour le dossier ET sa descendance Room (`treeMarkMissing`-léger : `markMissing` sur fichiers + sous-dossiers, sans `delete_resource` supplémentaire sauf si `hasSyncedCreateOperation`).
|
||||
3. Vérifier que le walk ne ressuscite pas : les lignes `exists = 0` + `delete_resource` pending doivent être ignorées par la réconciliation (garde `hasPendingDeleteOperation`, cf. `OutboxRepository.kt:204`).
|
||||
4. UI : vérifier que les listes filtrent `exists = 1` (déjà le cas) → la ligne disparaît aussitôt.
|
||||
Tests : étendre `ProcessedGateTest` ou nouveau `DeleteInCloudTest` — `deleteInCloud` puis assert `exists == 0` + op `delete_resource` présente ; walk simulé ne ressuscite pas.
|
||||
|
||||
### M9 — `FolderMerger` multi-transactions non-atomique
|
||||
Fichier : `features/saf/FolderMerger.kt:53-106`.
|
||||
Constat : `createMergedFolder` (transaction 1), moves (N transactions via `FileMover`/`reparentSubFolders`), deletes (N transactions). Un crash à mi-fusion laisse un état partiel ; la relance est idempotente (dossier réutilisé) mais ne reprend pas les moves manquants proprement.
|
||||
Plan :
|
||||
1. Envelopper `mergeSelectedFolders` dans `appDatabase.withTransaction` pour la partie Room (create + reparent + `treeMarkMissing`) — nécessite d'injecter `AppDatabase` dans `FolderMerger` et de rendre `FileMover.moveFiles` / `reparentSubFolders` appelables dans une transaction existante (ou journaliser les `move_resource`/`delete_resource` dans la transaction et n'exécuter le SAF physique qu'avant/après selon le sens crash-safe : journal-first comme C4).
|
||||
2. Ordre crash-safe : (a) transaction Room = create + moves logiques + deletes logiques (`exists = 0` + outbox), (b) exécution SAF physique (moves + deletes), (c) sweep au prochain tick pour les physiques manqués (réutiliser `sweepPendingPhysicalDeletes`).
|
||||
3. Garder l'idempotence `findExistingMerged` pour les relances.
|
||||
4. À défaut d'une transaction unique (SAF non transactionnel), a minima : rendre chaque étape reprise-safe (skip les sources déjà vides, skip les moves déjà journalisés via `hasPendingMoveOperation`).
|
||||
Tests : nouveau `FolderMergerTest` — fusion 2 dossiers avec fichiers + sous-dossier, assert `movedFiles/deletedFolders`, relance idempotente (pas de doublon), crash simulé entre étape 2 et 3 → reprise sans perte.
|
||||
|
||||
### M13 — `exists: Int? = null` invisible
|
||||
Fichiers : `data/local/entity/FolderEntity.kt:36`, `data/local/entity/FileEntity.kt:46`, DAO (`FolderDao`, `FileDao` filtres `= 1`), `Migrations.kt` (créations tables sans `DEFAULT 1` / `NOT NULL`).
|
||||
Constat : toute ligne insérée sans `exists` explicite (défaut Kotlin `null`) est cachée par les requêtes `WHERE exists = 1`.
|
||||
Plan :
|
||||
1. Durcir les entités : `exists: Int = 1` (non-null, défaut 1) dans `FolderEntity` et `FileEntity`.
|
||||
2. Migration v14→v15 : `UPDATE folders SET exists = 1 WHERE exists IS NULL`, idem `files`, puis recréation ou `NOT NULL DEFAULT 1` (Room : via `autoMigrations` ou SQL `ALTER` + table rewrite selon la version SQLite ; le plus simple : `UPDATE` + contrainte applicative + défaut entité, sans rewrite si Room accepte le changement nullable→non-null via migration manuelle avec recréation de table).
|
||||
3. Auditer tous les `saveFolder`/`saveLocalFile`/`upsertShared*` pour passer `exists = 1` explicitement.
|
||||
4. Option : ajouter un test `ExistsDefaultTest` — insert sans `exists`, assert visible dans `observeByFolder`/`searchWithFilters`.
|
||||
Note : migration à tester via `Migration13To14Test`-style (nouveau `Migration14To15Test`) + `:app:testDebugUnitTest`.
|
||||
|
||||
### M27 — snapshot permissions sans delta ni `canAccess`
|
||||
Fichiers : `data/repository/ShareRepository.kt:44-47`, `data/remote/dto/Dtos.kt:144-156` (`sharedById`/`expiresAt: Any?`), `data/remote/ApiService.kt:74-78`, `ApiClient.kt:106-108`.
|
||||
Constat : `syncPermissions()` sans `after`, champs `sharedById`/`expiresAt` jamais lus, `cachedAt` jamais persisté, aucun downgrade TTL 24h ni `canAccess` → grants révoqués/expirés visibles jusqu'au prochain login.
|
||||
Plan :
|
||||
1. DTO : typer `sharedById: String?`, `expiresAt: Long?` (le serveur renvoie ms epoch ou null — vérifier `docs/api-v1.md §6.2` et adapter avec un adapter Moshi tolérant `Any?`→Long).
|
||||
2. Persistance : stocker `cachedAt`/`expiresAt`/`sharedById`/`effectiveAccess` — soit colonnes sur `files`/`folders`, soit nouvelle table `resource_permissions` (préférable : ne pas polluer les entités). A minima `user_preferences` : `permissions_cached_at`.
|
||||
3. Delta : `syncSnapshot(after: Long? = cachedAt)` → `apiClient.syncPermissions(after)` ; pull complet (`after = null`) au login/restauration + quand `now - cachedAt > 24h` (TTL).
|
||||
4. Expiration locale : au tick, marquer `exists = 0` les lignes dont `expiresAt < now` ; downgrade lecture seule après TTL sans reseed (gate `canAccess` : à exposer via `ShareRepository.canAccess(resourceId, required)` utilisé par les VM avant open/share/rename).
|
||||
5. `canAccess` : ranking viewer < commenter < editor < owner, `ownerId == currentUser` → owner.
|
||||
Tests : étendre `ShareRepositoryTest` — delta `after`, révocation (absent du snapshot → `markMissing`), expiration (`expiresAt` passé → masqué), `canAccess` ranking.
|
||||
|
||||
### m1+m2 — badge sync et réveils WorkManager
|
||||
Fichier : `features/sync/SyncViewModel.kt:100-137`.
|
||||
Plan :
|
||||
1. m1 : envelopper le corps de boucle dans `try { … } finally { _walkInProgress.value = false }` (comme `importRoot:173-176`), et sortir `syncSnapshot` de la fenêtre `_walkInProgress` (flag séparé ou `syncing` = walk || drain déjà calculé via WorkManager).
|
||||
2. m2 : n'enqueuer `OutboxSyncWorker`/`OcrAutoWorker` que si le tick a produit quelque chose (`results.any { folders+files+missing > 0 }` ou `pendingOperationDao.countPending() > 0` / fichier promu). Éviter les réveils /30s à vide.
|
||||
Tests : Robolectric `SyncViewModelTest` — annulation du scope → flag retombe ; tick sans changement → `WorkManager` non sollicité (mock/static ou extraction d'un `SyncScheduler` testable).
|
||||
|
||||
### m5 — walk : transaction géante + `getAll()` par racine
|
||||
Fichier : `features/sync/DeviceSync.kt:67-163`.
|
||||
Plan :
|
||||
1. Découper la phase 2 : upserts dossiers/fichiers par batch (ex. 200) en transactions courtes successives au lieu d'une transaction unique ; la réconciliation `missing` en passe séparée.
|
||||
2. Remplacer `folderRepository.getAll()` + `fileRepository.getAll()` (full-table scan par racine) par des requêtes scopées à la racine (`getByRootUri`/`getDescendants`) — à ajouter aux DAO avec index sur `uri` (déjà unique) ; filtrer en SQL plutôt qu'en mémoire (`isChildOf` en `LIKE rootUri || '/%'`).
|
||||
3. Conserver le `Mutex` single-flight ; vérifier `ensureActive()` dans les boucles longues pour annulation propre.
|
||||
Tests : `SafScannerTest`-style perf — 5k fichiers simulés, assert temps/absence d'ANR ; test de convergence identique avant/après.
|
||||
|
||||
### m7 — recherche `LIKE` sans `ESCAPE`
|
||||
Fichier : `data/local/dao/FileDao.kt:42-49`.
|
||||
Plan : échapper `%`, `_`, `\` dans `:query` côté repository (`searchFiles`) + `LIKE … ESCAPE '\'` dans la requête ; ajouter `COLLATE NOCASE` (ou normalisation) pour « ete » → « ÉTÉ ». Tester : `%`, `_`, `\`, accents, casse.
|
||||
Tests : `SearchFieldTest` ou nouveau `SearchEscapeTest` — `%` littéral ne matche pas tout, `_` littéral, insensible à la casse.
|
||||
|
||||
### m20 — `OcrRepository.poll()` infini
|
||||
Fichier : `data/repository/OcrRepository.kt:66-73`.
|
||||
Plan : ajouter timeout (ex. 10 min) + nombre max de polls + backoff ; lever `OcrTimeoutException` ou retourner le dernier état ; coopérer avec l'annulation (`ensureActive`). Mettre à jour les appelants (viewer auto) pour afficher « OCR indisponible, réessayer ».
|
||||
Tests : `OcrRepositoryTest` — job restant `processing` → timeout au bout de N polls (fake `ApiClient` + virtual time/delays courts).
|
||||
|
||||
### m15+m19 — robustesse DB (downgrade + `exportSchema`)
|
||||
Fichiers : `di/DatabaseModule.kt:25-28`, `data/local/AppDatabase.kt:31-38`.
|
||||
Plan :
|
||||
1. m19 : `exportSchema = true` + `schemas/` commité ; ajouter un test `Migration13To14Test`-style qui échoue en cas de dérive (CI).
|
||||
2. m15 : stratégie downgrade explicite et documentée — soit `fallbackToDestructiveMigrationOnDowngrade()` (avec avertissement perte de données locales, acceptable car miroir SAF + outbox non poussée à préserver → plutôt refuser le démarrage avec message), soit crash contrôlé avec écran « mettez à jour l'app ». Choisir et documenter ; ne jamais laisser `IllegalStateException` brut.
|
||||
Tests : build avec `exportSchema=true` + `room.schemaLocation` ; test downgrade (ouverture v15 avec APK v14 → comportement attendu).
|
||||
|
||||
### m21 — ops contrat non émissibles
|
||||
Fichiers : `data/repository/OutboxRepository.kt:72-147`, `data/local/entity/PendingOperationEntity.kt:76-86` (constantes déjà présentes : `UPDATE_METADATA`, `UPDATE_SHARE`, `CREATE_LINK`, `REVOKE_LINK`).
|
||||
Plan : ajouter `enqueueUpdateMetadata(resourceId, resourceType, name?)`, `enqueueUpdateShare(...)`, `enqueueCreateLink(resourceId, resourceType, token, access, expiresAt?)`, `enqueueRevokeLink(...)` avec payloads §6.1 (`granteeUserId/access/inherit/expiresAt`, `token/access/expiresAt`) ; brancher les appelants (rename partagé, update share, liens publics) ; `GenerateId` pour `token` 32-hex côté client.
|
||||
Tests : `OutboxSyncWorkerTest`-style — sérialisation payload, `normalizePayloadNumbers` (`expiresAt` Long exact, non `1.7E12`), round-trip `POST /sync/ops` mocké.
|
||||
|
||||
---
|
||||
|
||||
## ✅ Vérifié infondé (suspects écartés)
|
||||
- Single-flight `Mutex` présent (`DeviceSync.kt:49`), `enqueueUniqueWork(KEEP)` + batch 20 + `ORDER BY id` conformes — non-bugs.
|
||||
- `checkHealth` construit bien `{base}/health` avec base incluant `/api/v1` = route backend `GET /api/v1/health` — OK.
|
||||
- Token **non** stocké en clair (`EncryptedSharedPreferences` AES256-SIV/GCM + Keystore) — seul `TokenProvider.current` en mémoire, normal.
|
||||
- Ordre interceptors `serverUrl → auth → logging`, `BASIC` ne log ni headers ni bodies — OK.
|
||||
- Contrat HTTP conforme sur endpoints déclarés (query `folderId/page/pageSize`, `deviceId` camel vs `device_id` login, multipart `file`/`resourceId`/`folderId`, enveloppes `{data,meta}`/`{error:{code,message}}`, codes) — OK.
|
||||
- `device_id` claim jamais utilisé comme preuve, seul le `Bearer` parle — conforme middleware.
|
||||
- Worker no-op en mode local (`OutboxSyncWorker.kt:111`), batch 20 ≤ 100 serveur, statuts OCR, `resolveUser` trim compatible, password jamais trimé — OK.
|
||||
- Deep-links : aucun défini (absence, pas de bug) ; args nav `folderResourceId`/`documentResourceId`/`ids` cohérents, 32-hex sans `/`.
|
||||
- Hilt : `MainActivity @AndroidEntryPoint`, tous `hiltViewModel()` ↔ `@HiltViewModel`, scopes corrects — aucune injection manquante.
|
||||
- Pas de recomposition infinie trouvée ; `collectAsStateWithLifecycle` partout ; placeholder cloud-only correct (`DocumentContentViewer.kt:39-47`) ; recherche 100 % locale conforme V1 ; back stack onglets correct (`saveState`/`restoreState`) ; `SelectionState` volatile documenté volontaire.
|
||||
- Scan : pas de double-`recycle` draft, pas de double rotation EXIF (comportement correct, commentaire trompeur), `swapOrder` args corrects, FK `scan_pages→sessions CASCADE` actif.
|
||||
@@ -0,0 +1,655 @@
|
||||
GNU AFFERO GENERAL PUBLIC LICENSE
|
||||
Version 3, 19 November 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU Affero General Public License is a free, copyleft license for
|
||||
software and other kinds of works, specifically designed to ensure
|
||||
cooperation with the community in the case of network server software.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
our General Public Licenses are intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
Developers that use our General Public Licenses protect your rights
|
||||
with two steps: (1) assert copyright on the software, and (2) offer
|
||||
you this License which gives you legal permission to copy, distribute
|
||||
and/or modify the software.
|
||||
|
||||
A secondary benefit of defending all users' freedom is that
|
||||
improvements made in alternate versions of the program, if they receive
|
||||
widespread use, become available for other developers to incorporate.
|
||||
Many developers of free software are heartened and encouraged by the
|
||||
resulting cooperation. However, in the case of software used on
|
||||
network servers, this result may fail to come about. The GNU General
|
||||
Public License permits making a modified version and letting the public
|
||||
access it on a server without ever releasing its source code to the
|
||||
public.
|
||||
|
||||
The GNU Affero General Public License is designed specifically to
|
||||
ensure that, in such cases, the modified source code becomes available
|
||||
to the community. It requires the operator of a network server to
|
||||
provide the source code of the modified version running there to the
|
||||
users of that server. Therefore, public use of a modified version, on
|
||||
a publicly accessible server, carries the obligation to make the source
|
||||
code freely available.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU Affero General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms of
|
||||
sections 4 and 5, provided that you also convey the machine-readable
|
||||
Corresponding Source under the terms of this License, in one of these
|
||||
ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions; the
|
||||
above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that transaction
|
||||
who receives a copy of the work also receives whatever licenses to the
|
||||
work the party's predecessors in interest had or could give under the
|
||||
previous paragraph, plus a right to possession of the Corresponding
|
||||
Source of the work from the predecessor in interest, if the predecessor
|
||||
has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Remote Network Interaction; Use with the GNU General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, if you modify the
|
||||
Program, your modified version must prominently offer all users
|
||||
interacting with it remotely through a computer network (if your version
|
||||
supports such interaction) an opportunity to receive the Corresponding
|
||||
Source of your version by providing access to the Corresponding Source
|
||||
from a network server at no charge, through some standard or customary
|
||||
means of facilitating copying of software. This Corresponding Source
|
||||
shall include the Corresponding Source for any work covered by version 3
|
||||
of the GNU General Public License that is incorporated pursuant to the
|
||||
following paragraph.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the work with which it is combined will remain governed by version
|
||||
3 of the GNU General Public License.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions
|
||||
of the GNU Affero General Public License from time to time. Such new
|
||||
versions will be similar in spirit to the present version, but may
|
||||
differ in detail to address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU Affero
|
||||
General Public License "or any later version" applies to it, you have
|
||||
the option of following the terms and conditions either of that
|
||||
numbered version or of any later version published by the Free
|
||||
Software Foundation. If the Program does not specify a version number
|
||||
of the GNU Affero General Public License, you may choose any version
|
||||
ever published by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU Affero General Public License can be used, that
|
||||
proxy's public statement of acceptance of a version permanently
|
||||
authorizes you to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
|
||||
PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
|
||||
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR
|
||||
CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
|
||||
ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT
|
||||
LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES
|
||||
SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE
|
||||
WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN
|
||||
ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
Kazier — personal document vault, local-first.
|
||||
Copyright (C) 2026 Thirdshop
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If your software can interact with users remotely through a computer
|
||||
network, you should also make sure that it provides a way for users to
|
||||
get its source. For example, if your program is a web application, its
|
||||
interface could display a "Source" link that leads users to an archive
|
||||
of the code. There are many ways you could offer source, and different
|
||||
solutions will be better for different programs; see section 13 for the
|
||||
specific requirements.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU AGPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
||||
@@ -1,8 +1,10 @@
|
||||
# VaultDrop
|
||||
# Kazier
|
||||
|
||||
VaultDrop est un coffre-fort de documents personnel, local-first. Le client Android镜映 le SAF dans une base Room et pousse les métadonnées de ses mutations via un outbox transactionnel vers le serveur Go/PostgreSQL — pas d'upload physique, pas de cloud-dépendance.
|
||||
Kazier est un coffre-fort de documents personnel, local-first. Le client Android reflète le SAF dans une base Room et pousse les métadonnées de ses mutations via un outbox transactionnel vers le serveur Go/PostgreSQL — pas d'upload physique, pas de cloud-dépendance.
|
||||
|
||||
Disponible sur [thirdshop.fr](https://thirdshop.fr/en/applications/vault/)
|
||||
[](LICENSE)
|
||||
|
||||
Disponible sur [thirdshop.fr](https://thirdshop.fr/en/applications/kazier/)
|
||||
|
||||
## Setup
|
||||
|
||||
@@ -27,5 +29,5 @@ cd mobile-kotlin && ./gradlew :app:testDebugUnitTest
|
||||
| Backend entry | `backend/cmd/server/main.go` — wiring gin + routes |
|
||||
| Sync outbox | `mobile-kotlin/.../features/sync/OutboxSyncWorker.kt` — worker WorkManager → `POST /sync/ops` |
|
||||
| SAF scan | `mobile-kotlin/.../features/sync/DeviceSync.kt` — two-pass walk SAF ↔ Room |
|
||||
| Room DB | `mobile-kotlin/.../data/local/AppDatabase.kt` — version 7, migrations, DAO |
|
||||
| Room DB | `mobile-kotlin/.../data/local/AppDatabase.kt` — version 16, migrations, DAO |
|
||||
| API Retrofit | `mobile-kotlin/.../data/remote/ApiService.kt` — interface Retrofit (contrat client) |
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
# Kazier V3 — « Le classeur qui comprend »
|
||||
|
||||
Statut : **livrée en 1.2.0** (2026-09-26). V1 (1.0.0, socle local-first + partage) et V2 (1.1.0, multi-device : upload/download, placements, rétention, groupes) sont livrées. Ce document reste le contrat produit + plan d'exécution de la V3 (toutes slices S1–S7 livrées).
|
||||
|
||||
## 1. Positionnement (acté)
|
||||
|
||||
- **Tout fichier est un document potentiel, y compris une photo.** Une image (scan, photo de papier, photo tout court) suit le même pipeline : OCR quand il y a du texte, faits génériques, FTS, relations. En revanche **aucune analyse biométrique ou sémantique d'image** : pas de visages, pas de lieux, pas d'événements, pas de clustering de personnes. Si une photo contient du texte (affiche, facture photographiée), on l'exploite ; sinon elle reste un fichier comme un autre (nom, dossier, favori, partage).
|
||||
- **Tous les documents comptent pareil.** Pas de top-5 types prioritaires. Extraction **générique d'abord** (dates, montants, émetteurs, références), typage (`facture`, `contrat`…) en **bonus humble** : suggestion corrigeable, jamais gate, jamais pénalisant.
|
||||
- **Humble par design.** L'extraction se trompe (~20% au début). Donc : chaque fait porte une `confidence`, l'UI affiche le douteux en « suggéré », la recherche **déclasse mais n'exclut jamais** sur un fait douteux, et tout est corrigeable en un tap.
|
||||
- **Échéance = facette, pas alerte.** Trop bruitée pour notifier (faux « expiré » dangereux). Filtre + tri uniquement. Alertes = V4 éventuelle, sur preuve de fiabilité via feedback.
|
||||
- **100% self-hosté / local-first.** Aucun appel IA externe. Règles FR + petits modèles CPU côté serveur, cache Room offline, workers async jamais bloquants (même pattern que `Ocr.Run`).
|
||||
- **Pas de LLM génératif en V3.** On extrait des faits vérifiables, on ne rédige rien. Moins d'hallucinations, moins de GPU, plus de confiance famille.
|
||||
|
||||
## 2. Les 5 piliers (cœur, scope 6–8 semaines)
|
||||
|
||||
| # | Pilier | Promesse | Réutilise |
|
||||
|---|---|---|---|
|
||||
| P1 | Filet anti-perte | Corbeille 30j + versions N=3 | `deleted_at` soft-delete, `UPLOAD_DIR/<user>/<resource>.<ext>`, overwrite ciblée V2 |
|
||||
| P2 | Extract générique | Faits universels + type suggéré, corrigés depuis la fiche document | Pipeline `upload ciblé → Tesseract → resources.ocr_text → GET /files/:id/ocr`, gate `processed` |
|
||||
| P3 | Recherche | FTS nom+ocr+faits, facettes, offline | `FileDao.searchWithFilters` (LIKE+accents), `GET /files/search` serveur |
|
||||
| P4 | Relations doc | `same_scan` (sûr) + doublons/versions/même-émetteur (invalidables), collections virtuelles | `scan_sessions`+`scan_pages`, `content_hash` (V2.md), versions P1 |
|
||||
| P5 | Partage sobre | Cercles (groupes V2-6), liens à expiration par défaut, révocation tokens, quota/3-2-1 visible | Snapshot permissions + TTL 24h, `share`/`create_link`, `PATCH /users/me/password` |
|
||||
|
||||
## 2bis. Plan A — cohérence obligatoire (inclus dans la V3, pas du scope en plus)
|
||||
|
||||
Sans ces 4 items, les piliers grincent. Ils sont petits et intégrés aux slices S1–S6 :
|
||||
|
||||
- **A1 Appareils connectés** : exposer `storage_locations` (existe depuis V2, migration `000015`) en lecture `GET /devices` + `DELETE /devices/:id` (révocation = bump `token_version` ciblé ou suppression ligne ; le device révoqué retombe en 401 → re-login refusé `INVALID_DEVICE_ID`). Écran Réglages « Appareils » : voir / révoquer un téléphone perdu. Grosse valeur famille, ~1 endpoint + 1 écran.
|
||||
- **A2 Quota enforcement** : `/usage` (P5) n'est que de l'affichage. Ajouter `users.storage_quota_bytes` (défaut 10 Go, cf. V2.md §3.1) + garde-fou à l'upload (`413 QUOTA_EXCEEDED`) + à l'overwrite versionnée. Migration `000023_quota`. Sans ça, un foyer remplit le disque.
|
||||
- **A3 Corbeille cohérente des deux côtés** : règle purge serveur (cron au boot + passe quotidienne : `trashed_at < NOW()-30j` → purge dure) **+** miroir mobile (`trashed_at` Room, filtre global `exists=1 AND trashed_at IS NULL`, écran Corbeille). Le walk SAF doit ignorer les trashés (garde `hasPendingTrash`, même pattern que FEEDBACK M5) sinon ils ressuscitent. Fait partie de S1, pas une option.
|
||||
- **A4 Conflits rename transparents** : `update_metadata` concurrent (2 devices renomment) = last-writer-wins silencieux aujourd'hui. Minimum V3 : comparer `updated_at` (perdant = op arrivée sur `updated_at` plus récent que son `base_updated_at` → appliquée quand même mais flag `conflict` remonté dans la réponse `POST /sync/ops` + badge UI « renommé ailleurs » via snapshot). Pas de CRDT, juste de la transparence. Payload `update_metadata` étendu : `{ name?, baseUpdatedAt? }`.
|
||||
|
||||
## 2ter. Plan B — quick wins produit (inclus : B1, B2, B4 ; B3/B5 en stretch)
|
||||
|
||||
- **B1 Import « classeur » unifié** : un seul point d'entrée « Ajouter un papier » (scan / photo / PDF / fichier) + file d'attente d'import visible (compteur `unprocessed`, déjà en Room). Aujourd'hui scan + upload + PDF builder sont 3 chemins séparés — les unifier en UX, zéro backend nouveau. Inclus (S6).
|
||||
- **B2 Doublons visibles + action** : `duplicate_of` (P4, hash) exposé en écran « Doublons (240 Mo récupérables) » + action « garder un seul » (= trash les autres, même op `trash_resource`). Les familles adorent. Inclus (S5).
|
||||
- **B3 Impression / export PDF** : ouvrir le PDF builder existant aux docs déjà stockés (pas qu'au multi-select) + `ACTION_SEND` / impression système. Stretch S7 (pur mobile, pas de backend).
|
||||
- **B4 Accessibilité + i18n** : labels TalkBack sur review/chips/facettes, contrastes, chaînes FR/EN des nouveaux écrans dès la slice (pas de rattrapage). Inclus, transverse à chaque slice.
|
||||
- **B5 Onboarding foyer** : écran « Crée ton cercle Famille » au premier login + import guidé des 3 premiers papiers (deep-link vers B1 + création groupe V2-6 pré-rempli). Sans ça, les cercles P5 restent vides. Stretch S6 si le temps manque (1 écran + 1 CTA, pas de backend).
|
||||
|
||||
## 2quater. Plan C — dette V2.md : solder ou enterrer (décisions actées)
|
||||
|
||||
- **C1 `retention_policies` serveur (V2.md §3.6) : ENTERRÉ.** La rétention V2 mobile (`RetentionStore` + `RetentionWorker`) fait le job ; une politique serveur ajouterait un 2e cerveau en conflit avec le walk local-first. Documenter dans `docs/api-v1.md` : « la rétention est une affaire locale ».
|
||||
- **C2 `sync_queue` serveur (V2.md §3.7) : ENTERRÉ.** L'outbox client (`pending_operations` + batch 20 + dead-letter) + workers WM couvrent le besoin. Ne pas rouvrir.
|
||||
- **C3 Backup 3-2-1 réel : EXPORT, pas second serveur.** `role='backup'` reste un modèle, pas une infra. Minimum V3 : bouton « Exporter tout » (zip + manifest `resource_id, name, hash, facts`) depuis Réglages/Stockage — testé, restaurable, chiffré par le mot de passe (zip AES). Stretch S7, à arbitrer contre les embeddings : **l'export gagne** (vrai 3-2-1 perçu, zéro GPU).
|
||||
|
||||
## 3. Backend — plan fichier par fichier
|
||||
|
||||
### 3.1 Migrations (`backend/db/migrations/`, une par palier, up+down + `migrations_test.go`)
|
||||
|
||||
- `000017_trash` : rien à créer — ajouter index partiel `idx_resources_deleted_at ON resources(deleted_at) WHERE deleted_at IS NOT NULL` + colonne `trashed_at TIMESTAMPTZ` (NULL = vivant ; purge = `trashed_at < NOW()-30j`). Le `DELETE` actuel devient trash (soft), pas de breaking change API.
|
||||
- `000018_versions` : table `resource_versions (id 32-hex PK, resource_id FK CASCADE, version_no INT, size_bytes BIGINT, content_hash TEXT, stored_path TEXT, created_at)` + `UNIQUE(resource_id, version_no)`. À chaque overwrite d'octets (upload ciblé), la copie précédente est poussée en version (garder N=3, purger la plus vieille).
|
||||
- `000019_facts` : table `resource_facts (id 32-hex PK, resource_id FK CASCADE, fact_type TEXT CHECK IN ('date','due_date','period','amount','issuer','reference','doc_type'), value_text TEXT, value_num NUMERIC NULL, value_date DATE NULL, confidence REAL CHECK 0..1, origin TEXT CHECK IN ('auto','user'), created_at, updated_at)` + index `(resource_id)`, `(fact_type, value_text)`. `doc_type` n'est qu'un fait comme un autre (suggestion, pas statut).
|
||||
- `000020_search_fts` (livré S4) : colonne `search_tsv TSVECTOR` générée (`name + ocr_text`, `french`) + index GIN ; faits joints à la requête (rang, jamais d'exclusion).
|
||||
- `000021_links` (S5, livré) : table `resource_links (id 32-hex PK, from_id/to_id canoniques, link_type IN (same_scan, duplicate_of, same_issuer, same_reference) — pas de `version_of`, CHECK from != to, UNIQUE trio)` + `resources.scan_session_id` (session opaque du create outbox). `version_of` non matérialisé (versions DANS la ressource).
|
||||
- `000022_token_version` (S6) : `ALTER TABLE users ADD COLUMN token_version INT NOT NULL DEFAULT 0` — `PATCH /users/me/password` bump ; claim paseto `tv`, middleware `RequireAuth` rejette `tv != users.token_version` (lève la limite V1 « tokens non révoqués »).
|
||||
- `000023_quota` (A2, S6) : `ALTER TABLE users ADD COLUMN storage_quota_bytes BIGINT NOT NULL DEFAULT 10737418240` (10 Go, V2.md §3.1) + vue/fonction `user_used_bytes(user_id)` (somme `size_bytes`hors trash). Check à l'upload et à l'overwrite versionnée → `413 QUOTA_EXCEEDED` (nouveau code §7).
|
||||
|
||||
> Ordre imposé par golang-migrate (montée stricte, pas de trous) : FTS a pris `000020` en S4, décalant links→`000021` et token→`000022`.
|
||||
|
||||
### 3.2 Extract — nouveau worker (miroir `service/ocr.go`, `cmd/server/main.go`)
|
||||
|
||||
- Nouveau `backend/extract/rules.go` : pur package, **testable sans PG** — entrée `text string`, sortie `[]Fact{Type, ValueText/Num/Date, Confidence}`. Règles FR : dates (`12/03/2026`, `mars 2026`, `échéance ..`), montants (`1 234,56 €`, `EUR`), raisons sociales (ligne en capitales + SIRET/SIREN voisin), références (`n°`, `facture n°`, `contrat`, IBAN masqué → on ne stocke que les 4 derniers chiffres).
|
||||
- Nouveau `backend/service/extract.go` : struct `Extract{Repository}` + `Run(ctx)` — même squelette que `Ocr.Run` (poll file, `FOR UPDATE SKIP LOCKED`, `QueuePollInterval`, `ResetStale` au boot). Déclencheur : après chaque OCR `done` avec texte non vide (appel direct `Extract.Enqueue/Process` depuis `Ocr.process`, pas de nouvelle file PG — une colonne `extract_status` sur `ocr_jobs` ou simple appel synchrone borné suffit en V3).
|
||||
- `backend/repository/facts.go` : `UpsertAuto(resourceID, facts)` (remplace les `origin='auto'`, ne touche jamais aux `origin='user'`), `Correct(resourceID, facts)` (écrit `origin='user'`, confidence 1.0), `ListForResource`, `SearchFacets(...)`.
|
||||
- `backend/repository/links.go` : `EnsureAuto` (idempotent, upsert par `(from,to,type)`), `SetUserVerdict` (confirme/invalide → `created_by='user'`, confidence 0/1), `ListForResource`. Règles auto V3 : `same_scan` (déterministe depuis `scan_sessions` — en fait côté mobile, voir §4, serveur : `duplicate_of` via `content_hash` égal, `version_of` via `resource_versions`, `same_issuer` via facts `issuer` normalisé identique + confidence = min des deux facts).
|
||||
- `backend/service/resources.go` : trash/restore/purge (`Trash` = set `trashed_at`, `Restore` = NULL, `Purge` = hard delete + versions + faits + liens + fichier physique) ; overwrite d'octets → pousser l'ancienne copie en `resource_versions` avant écriture.
|
||||
- `backend/service/sync.go` : nouvelles ops outbox **`trash_resource` / `restore_resource` / `purge_resource` / `correct_facts`** (même enveloppe §6.1, `operation_id` 32-hex, idempotence `(device_id, operation_id)`, arrêt au premier échec non-idempotent). `delete_resource` legacy → mappé vers `trash_resource` (pas de break client existant). `correct_facts` porte `[{fact_type, value, confidence}]`, applique `Correct` (writer = user, écrase l'auto).
|
||||
- `backend/handlers/` : `GET /trash` (liste `trashed`, paginé, même enveloppe `{data,meta}`), `POST /files/:id/restore`, `DELETE /files/:id?mode=purge` (défaut trash ; `purge` = définitif), `GET /files/:id/versions` + `POST /files/:id/versions/:n/restore`, `GET /files/:id/facts` + `PATCH /files/:id/facts` (correction directe, même sémantique que `correct_facts`), `GET /files/:id/relations` + `POST /files/:id/relations/verdict` (livrés S5 — `/links` étant pris par les liens de partage), `GET /duplicates` (livré S5 B2), `GET /users/me/usage` (quota + copies 3-2-1 : `used_bytes`, `quota_bytes`, `copies`, `offsite_ok`), `GET /devices` + `DELETE /devices/:id` (A1, lecture `storage_locations` + révocation), `GET /export` (C3, zip + manifest, streamé). Tous scopés `viewer+` lecture / `owner` mutation sauf `correct_facts` (`editor+`, comme `update_metadata`).
|
||||
- `backend/handlers/router.go` + `cmd/server/router_test.go` : enregistrer les routes (le test de contrat miroir du client doit rester vert).
|
||||
- `docs/api-v1.md` §6.1/§7 : documenter les 4 ops + codes (`TRASHED` vs `NOT_FOUND` — une ressource trashée reste `NOT_FOUND` en lecture normale, visible seulement via `/trash`).
|
||||
|
||||
### 3.3 Recherche serveur
|
||||
|
||||
- `repository/resources.go` `SearchFilesVisible` : brancher `search_tsv @@ plainto_tsquery('french', q)` en OR avec le `LIKE` existant (compat), rank `ts_rank` + bonus si match facts (`issuer`, `reference`). Jamais d'exclusion sur fait basse-confiance : `confidence < 0.5` → pondération /2, pas filtre.
|
||||
- Facettes : `GET /files/search?q=&issuer=&from=&to=&min_amount=&max_amount=&doc_type=` — filtres SQL sur `resource_facts` (jointure, `origin` indifférent, `doc_type` = suggestion acceptée comme filtre mais affichée « suggéré » si `origin='auto'`).
|
||||
|
||||
## 4. Mobile — plan fichier par fichier
|
||||
|
||||
### 4.1 Room (v19 → v20 faits, v21 FTS — voir S3/S4, `app/schemas/` exporté, tests `MigrationXToYTest`)
|
||||
|
||||
- v19 (S1, livré) : `trashed_at` sur files + folders.
|
||||
- v20 (S3, livré) : `resource_facts (id, resource_id, fact_type, value_text, value_num, value_date, confidence, origin, updated_at)` + `FactsDao` (remplacement en bloc au sync, corrections `user` poussées en outbox).
|
||||
- v22 (S5, livré) : `resource_links` (miroir `GET /relations`, remplacement en bloc, verdicts REST directs) + `LinksDao` + `collections` (requêtes sauvegardées locales, jamais syncées) + `CollectionDao`. `is_favorite` reporté (non bloquant, S6+ si besoin).
|
||||
- v21 (S4) : FTS4 `files_fts (name, ocr_text, facts_text)` + triggers sync + requête `searchDocuments(query, facets)` remplaçant `searchWithFilters` pour les docs (garder l'ancien pour dossiers). `facts_text` = concat des `value_text` — reconstruit à chaque sync facts.
|
||||
- `OutboxRepository.kt` : `enqueueTrash/restore/purge`, `enqueueCorrectFacts` (payload `[{fact_type, value}]`, `GenerateId` 32-hex, même transaction Room que la mutation parente — contrat outbox atomique inchangé). `delete_resource` existant → renommé en trash côté appelants (`FileDeleter`, `FolderDeleter`).
|
||||
|
||||
### 4.2 Review = moment de vérité (zéro écran en plus)
|
||||
|
||||
- `OcrRepository.syncFromServer` : après `ocr_text`, rapatrier `GET /files/:id/facts` → Room (même pattern `syncFromServer`, poll existant réutilisé).
|
||||
- Fiche document (livré S3 — pas le review : le deck ne contient que des fichiers jamais OCR-és, sans faits) : fiche « Ce qu'on a compris » (`FactsBottomSheet`, bouton viewer si `processed`) — chips `Émetteur: EDF • 84,20 € • Échéance: 12/04 (suggéré)`, confiance < 0.5 grisée, user gagne par type à l'affichage, édition par type (texte/nombre/date ISO) → `enqueueCorrectFacts` dans la **même transaction** que l'écriture Room optimiste, reset « rétablir auto » (jeu vide poussé).
|
||||
- Fiche document : section « Voir aussi » (`LinksDao` → `same_scan` d'abord avec badge « sûr », puis autres avec bouton invalider) + section « Faits » (grisé si `confidence < 0.5`).
|
||||
|
||||
### 4.3 Relations `same_scan` (déterministe, local)
|
||||
|
||||
- `ScanExport.kt` : à l'export multi-pages, écrire les liens `same_scan` entre les `resource_id` de la session (même transaction que les `create_resource`). Confiance 1.0, `created_by='user'` (acte volontaire). Poussés via `correct_facts`-like ? Non — nouvelle op `link_pages` ou piggyback sur `create_resource` payload (préféré : champ `scanSessionId` dans le payload `create_resource`, le serveur en déduit `same_scan` — **zéro nouvelle op**).
|
||||
- Collections virtuelles : table locale `collections + collection_members` (requêtes sauvées sur facts, ex. « Impôts 2026 » = `doc_type suggéré:impôt + period:2026`) — pur local en V3, pas de sync (un device, pas de conflit). Sync serveur = V4.
|
||||
|
||||
### 4.4 Recherche + facettes UI
|
||||
|
||||
- Barre unique : texte + chips facettes (`émetteur`, `période`, `montant`, `type suggéré`). Requête Room FTS offline ; si réseau, `GET /files/search` enrichit (même ranking affiché, source badgée « serveur »).
|
||||
- OutboxSyncWorker : après drain, enchaîner `FactsSync` (comme `OcrAutoWorker` aujourd'hui) — pas de nouveau worker WM, simple étape dans le worker existant.
|
||||
|
||||
### 4.5 Partage sobre + confiance
|
||||
|
||||
- Sheet existant : expiration lien **7j par défaut** (aujourd'hui `null`), badge « suggéré » sur `doc_type`, section copies déjà là (placements V2) + ligne quota/3-2-1 (`GET /users/me/usage` → Réglages/Stockage existant).
|
||||
- `SessionManager` : `tv` claim (voir §3.1 `token_version`) — changement de password → autres devices 401 → re-login silencieux existant (aucun code nouveau, juste le claim).
|
||||
|
||||
## 5. Ordre d'exécution (7–9 semaines, chaque ligne = shippable)
|
||||
|
||||
1. **S1 — Filet + A3** (livré : BE `000017/000018` + trash/restore/purge + cron purge 30j + mobile v19 + `FileDeleter` → trash + gardes walk + écran Corbeille) — done quand : supprimer → restaure depuis un 2e device, le walk ne ressuscite pas.
|
||||
2. **S2 — Extract générique** (livré : `extract/rules.go` + `service/extract.go` + `000019` + `UpsertAuto` + `GET /files/:id/facts`) — done quand : un scan EDF propose émetteur/montant/échéance.
|
||||
3. **S3 — Fiche + correction** (livré : `PATCH /files/:id/facts` + op `correct_facts` editor+ + Room v20 facts + fiche viewer + `FactsRepository` + B4 a11y fiche) — done quand : corriger en un tap converge serveur ↔ 2e device.
|
||||
4. **S4 — Recherche** (livré : `000020` FTS + facettes serveur + Room FTS v21 + UI filtres + B4 a11y) — done quand : « facture edf mars » trouve `scan_042.pdf` offline ET online.
|
||||
5. **S5 — Relations + B2** (livré : `000021` links + doublons SHA-256 + same_scan outbox + `GET /relations` + verdict + `GET /duplicates` + Room v22 + « Voir aussi » + Doublons + collections locales) — done quand : fiche CNI → verso + doublon listés, invalidables ; doublons chiffrés en Mo récupérables.
|
||||
6. **S6 — Partage sobre + A1/A2/A4 + B1 (+B5 si temps)** (livré : expiration liens 7j défaut + `000022` token_version + `000023` quota idempotente + `413 QUOTA_EXCEEDED` + `GET /devices` + `DELETE /devices/:id` + `EnforceDeviceExists` prod + écran Appareils + quota/usage + `update_metadata{baseUpdatedAt}` + `conflicts[]` + pipeline mobile + import « classeur » unifié + onboarding cercle Famille) — done quand : lien RIB expiré → `NOT_FOUND`, password changé → autres devices re-logués, device perdu révoqué, quota plein → 413 propre, rename concurrent → badge « renommé ailleurs ».
|
||||
7. **S7 — Preview + export** (livré : `000024` previews + `GET /files/:id/preview` JPEG 512 — images pur Go, PDF via `pdftoppm` + poppler, `NO_PREVIEW` sinon — + vignettes distantes mobile + `POST /export` zip AES + manifest + partage ACTION_SEND + release 1.2.0). Impression papier reportée V4 (pas de dep print).
|
||||
|
||||
## 6. Tests (mêmes harnais, pas de nouveau framework)
|
||||
|
||||
- BE : `dbtest` + `go test ./...` — `extract/rules_test.go` (corpus FR : factures, bulletins, courriers, cas limites montants/dates), tests repo `facts/links` (upsert auto n'écrase pas user, recherche ne filtre pas sur low-confidence), tests handlers (trash/restore/purge, versions, facts, links, usage, devices, export, quota 413, conflit rename), `router_test.go` vert.
|
||||
- Mobile : `:app:testDebugUnitTest` (Robolectric + Room in-memory) — `Migration18To19/19To20Test`, `FactsSyncTest` (round-trip `correct_facts`), `SearchFtsTest` (accents, `%`/`_` échappés, facettes), `SameScanTest` (export multi-pages → liens), `TrashWalkTest` (walk n ressuscite pas les trashés, miroir FEEDBACK M5), `DuplicatesTest`, `QuotaBadgeTest`.
|
||||
|
||||
## 7. Non-goals V3 (actés, basculables V4)
|
||||
|
||||
Alertes d'échéance, résumés/chat LLM, embeddings sémantiques (déscopés au profit de C3 export — tables `facts`/`links` déjà prêtes à les accueillir en V4), analyse d'image biométrique/sémantique (visages, lieux, événements, clustering personnes — **hors vision par principe**, les photos restent des documents image), OCR on-device, transcodage vidéo, E2EE dédiée (les facts/liens héritent des perms `viewer+` comme `ocr_text` — pas de nouveau modèle de droits), sync des collections, client desktop/web, fédération, `retention_policies` serveur (C1 enterré : rétention = affaire locale), `sync_queue` serveur (C2 enterré : l'outbox client fait le job).
|
||||
@@ -0,0 +1,8 @@
|
||||
bin/
|
||||
uploads/*
|
||||
!uploads/.gitkeep
|
||||
.env
|
||||
.git
|
||||
*.md
|
||||
.vscode/
|
||||
.idea/
|
||||
@@ -2,13 +2,14 @@
|
||||
|
||||
# Serveur
|
||||
PORT=8080
|
||||
DATABASE_URL=postgres://vaultdrop:vaultdrop@localhost:5432/vaultdrop_dev?sslmode=disable
|
||||
DATABASE_URL=postgres://kazier:kazier@localhost:5432/kazier_dev?sslmode=disable
|
||||
UPLOAD_DIR=./uploads
|
||||
MAX_FILE_SIZE_MB=50
|
||||
OCR_LANG=fra+eng
|
||||
|
||||
# Sécurité — à changer en production (jamais commité)
|
||||
AUTH_SECRET=dev-secret-change-me
|
||||
# ≥ 32 caractères, ex. `openssl rand -hex 32`. Doit matcher config.go (min 32).
|
||||
AUTH_SECRET=dev-secret-change-me-please-set-in-prod
|
||||
|
||||
# Premier admin (créé UNIQUEMENT si la table users est vide). Obligatoires au
|
||||
# premier démarrage ; ignorés ensuite (l'env n'écrase jamais un compte).
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
|
||||
ARG VERSION=dev
|
||||
ARG BUILD_DATE=unknown
|
||||
|
||||
# --- builder: compile statique du binaire Go (CGO off, musl) ---
|
||||
FROM golang:1.26-alpine AS builder
|
||||
ENV GOTOOLCHAIN=auto
|
||||
WORKDIR /src
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
COPY . .
|
||||
ARG VERSION
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build \
|
||||
-trimpath \
|
||||
-ldflags "-s -w -X main.version=${VERSION}" \
|
||||
-o /out/kazier-server \
|
||||
./cmd/server/main.go
|
||||
|
||||
# --- runtime: mince, Tesseract (fra+eng) embarqué pour l'OCR + poppler
|
||||
# (pdftoppm) pour les vignettes PDF page 1 (V3 S7, best-effort) ---
|
||||
FROM alpine:3.20
|
||||
RUN apk add --no-cache \
|
||||
ca-certificates \
|
||||
tzdata \
|
||||
tesseract-ocr \
|
||||
tesseract-ocr-data-eng \
|
||||
tesseract-ocr-data-fra \
|
||||
poppler-utils
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
ARG VERSION
|
||||
ARG BUILD_DATE
|
||||
LABEL org.opencontainers.image.title="kazier-server" \
|
||||
org.opencontainers.image.description="Kazier backend API (Go/Gin + PostgreSQL) — local-first document vault" \
|
||||
org.opencontainers.image.version="${VERSION}" \
|
||||
org.opencontainers.image.licenses="AGPL-3.0-only" \
|
||||
org.opencontainers.image.source="https://gitea.thirdshop.fr/thirdshop/kazier" \
|
||||
org.opencontainers.image.url="https://thirdshop.fr/en/applications/kazier/" \
|
||||
org.opencontainers.image.created="${BUILD_DATE}" \
|
||||
org.opencontainers.image.revision=""
|
||||
|
||||
COPY --from=builder /out/kazier-server /usr/local/bin/kazier-server
|
||||
|
||||
# UPLOAD_DIR persistant, user non-root
|
||||
RUN mkdir -p /app/uploads \
|
||||
&& adduser -D -H -u 10001 kazier \
|
||||
&& chown kazier:kazier /app/uploads
|
||||
VOLUME /app/uploads
|
||||
|
||||
ENV PORT=8080 \
|
||||
UPLOAD_DIR=/app/uploads
|
||||
|
||||
USER kazier
|
||||
EXPOSE 8080
|
||||
|
||||
HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
|
||||
CMD wget -q -O - http://127.0.0.1:8080/api/v1/health || exit 1
|
||||
|
||||
ENTRYPOINT ["kazier-server"]
|
||||
+154
-14
@@ -1,19 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/vaultdrop/backend/config"
|
||||
"github.com/vaultdrop/backend/db"
|
||||
"github.com/vaultdrop/backend/handlers"
|
||||
"github.com/vaultdrop/backend/ocr"
|
||||
"github.com/vaultdrop/backend/pkg/auth"
|
||||
"github.com/vaultdrop/backend/repository"
|
||||
"github.com/vaultdrop/backend/service"
|
||||
"github.com/kazier/backend/config"
|
||||
"github.com/kazier/backend/db"
|
||||
"github.com/kazier/backend/handlers"
|
||||
"github.com/kazier/backend/ocr"
|
||||
"github.com/kazier/backend/pkg/auth"
|
||||
"github.com/kazier/backend/repository"
|
||||
"github.com/kazier/backend/service"
|
||||
)
|
||||
|
||||
var version = "dev"
|
||||
|
||||
func newRouter() *gin.Engine {
|
||||
r := gin.Default()
|
||||
handlers.RegisterRoutes(r)
|
||||
@@ -21,34 +31,57 @@ func newRouter() *gin.Engine {
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) > 1 && os.Args[1] == "--version" {
|
||||
fmt.Printf("kazier-server %s\n", version)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
err, cfg := config.LoadApplicationConfig()
|
||||
cfg, err := config.LoadApplicationConfig()
|
||||
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// Migrations AVANT l'ouverture du pool applicatif : un seul *sql.DB vit à
|
||||
// la fois (MigrateDatabase ouvre/ferme sa propre connexion éphémère).
|
||||
// Inter-lots : pour partager strictement le même pool, db doit exposer
|
||||
// Migrate(conn *sql.DB) — fichier LOT 4 (db/db.go), non édité ici.
|
||||
if err := db.MigrateDatabase(cfg.DatabaseURL); err != nil {
|
||||
log.Fatalf("migrations postgres: %v", err)
|
||||
}
|
||||
|
||||
conn, err := db.Open(cfg.DatabaseURL)
|
||||
if err != nil {
|
||||
log.Fatalf("connexion postgres: %v", err)
|
||||
}
|
||||
|
||||
if err := db.MigrateDatabase(cfg.DatabaseURL); err != nil {
|
||||
log.Fatalf("migrations postgres: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
authManager, err := auth.NewManager(cfg.AuthSecret)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
handlers.Auth = authManager
|
||||
// V3 S6 A1 : les tokens d'un device révoqué (ligne `devices` supprimée)
|
||||
// sont rejetés en 401 — révocation réelle, pas décorative.
|
||||
handlers.EnforceDeviceExists = true
|
||||
|
||||
repo := repository.NewRepository(conn)
|
||||
|
||||
// Premier admin (users vide) — refus de démarrer si identifiants absents.
|
||||
// Concurrence au boot (2 instances, users vide) : le perdant de l'INSERT
|
||||
// reçoit ErrNameConflict (UNIQUE username_normalized) → recompte avant de
|
||||
// refuser de démarrer (retry logique, sans toucher bootstrap.go — LOT 4).
|
||||
if err := service.EnsureAdmin(repo, cfg.AdminUsername, cfg.AdminPassword); err != nil {
|
||||
if errors.Is(err, repository.ErrNameConflict) {
|
||||
if n, cerr := repo.Users.Count(); cerr == nil && n > 0 {
|
||||
log.Println("bootstrap admin: compte créé par une instance concurrente, démarrage poursuivi")
|
||||
} else {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
} else {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}
|
||||
|
||||
handlers.Store = service.NewResources(
|
||||
repo,
|
||||
@@ -57,8 +90,115 @@ func main() {
|
||||
)
|
||||
handlers.Ocr = service.NewOcr(repo, cfg.UploadDir, cfg.OcrLang, ocr.NewTesseract())
|
||||
|
||||
if err := newRouter().Run(fmt.Sprintf(":%d", cfg.Port)); err != nil {
|
||||
log.Fatalln(err)
|
||||
// V3 S2 : faits génériques — chaque texte OCR extrait alimente
|
||||
// `resource_facts` (origin='auto'). Enrichissement best-effort : un
|
||||
// échec d'extract ne fait jamais échouer le job OCR.
|
||||
extractSvc := service.NewExtract(repo)
|
||||
handlers.Ocr.OnText = func(userID, resourceID, text string) {
|
||||
if err := extractSvc.ProcessText(userID, resourceID, text); err != nil {
|
||||
log.Printf("extract: %s: %v", resourceID, err)
|
||||
}
|
||||
}
|
||||
|
||||
// V3 S5 : backfill des hashes (uploads d'avant la V3) — doublons.
|
||||
go func() {
|
||||
n, err := handlers.Store.BackfillHashes(200)
|
||||
if err != nil {
|
||||
log.Printf("content backfill hashes: %v", err)
|
||||
return
|
||||
}
|
||||
if n > 0 {
|
||||
log.Printf("content backfill hashes: %d fichiers", n)
|
||||
}
|
||||
}()
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
// Worker OCR borné (OCR_WORKERS) : file `ocr_jobs`, hors request handlers.
|
||||
// WaitGroup + budget d'arrêt partagé (5s, voir shutdown plus bas) : le
|
||||
// worker finit le job en cours ; passé le délai, le process sort et le job
|
||||
// resté `processing` sera requeue au prochain boot (ResetStaleProcessing).
|
||||
var ocrWg sync.WaitGroup
|
||||
ocrWg.Add(1)
|
||||
go func() {
|
||||
defer ocrWg.Done()
|
||||
handlers.Ocr.Run(ctx, cfg.OcrWorkers)
|
||||
}()
|
||||
log.Printf("ocr worker démarré (%d workers)", cfg.OcrWorkers)
|
||||
|
||||
// V3 S2 : backfill extract (une passe bornée au boot — les ressources
|
||||
// avec ocr_text mais sans faits auto, ex. OCR d'avant la V3).
|
||||
go func() {
|
||||
n, err := extractSvc.Backfill(200)
|
||||
if err != nil {
|
||||
log.Printf("extract backfill: %v", err)
|
||||
return
|
||||
}
|
||||
if n > 0 {
|
||||
log.Printf("extract backfill: %d ressources enrichies", n)
|
||||
}
|
||||
}()
|
||||
|
||||
// V3 corbeille : passe de purge (trash > TrashRetention) au boot puis
|
||||
// toutes les heures. Best-effort : une passe ratée sera rejouée à la
|
||||
// suivante, jamais de crash serveur sur une erreur de purge.
|
||||
go func() {
|
||||
purge := func() {
|
||||
n, err := handlers.Store.PurgeExpiredTrash(service.TrashRetention)
|
||||
if err != nil {
|
||||
log.Printf("purge corbeille: %v", err)
|
||||
return
|
||||
}
|
||||
if n > 0 {
|
||||
log.Printf("purge corbeille: %d ressources détruites", n)
|
||||
}
|
||||
}
|
||||
purge()
|
||||
ticker := time.NewTicker(time.Hour)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
purge()
|
||||
}
|
||||
}
|
||||
}()
|
||||
log.Printf("purge corbeille démarrée (rétention %s)", service.TrashRetention)
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", cfg.Port),
|
||||
Handler: newRouter(),
|
||||
}
|
||||
go func() {
|
||||
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Fatalf("server: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
<-ctx.Done()
|
||||
stop()
|
||||
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := srv.Shutdown(shutdownCtx); err != nil {
|
||||
log.Printf("shutdown: %v", err)
|
||||
}
|
||||
// Le worker OCR dispose du même budget de 5s : on attend la fin du job en
|
||||
// cours, passé ce délai le process sort (job resté `processing` → requeue
|
||||
// au prochain boot via ResetStaleProcessing, jamais de job perdu).
|
||||
ocrDone := make(chan struct{})
|
||||
go func() {
|
||||
ocrWg.Wait()
|
||||
close(ocrDone)
|
||||
}()
|
||||
select {
|
||||
case <-ocrDone:
|
||||
log.Println("worker OCR arrêté")
|
||||
case <-shutdownCtx.Done():
|
||||
log.Println("worker OCR: budget 5s écoulé, arrêt forcé (jobs `processing` requeues au prochain boot)")
|
||||
}
|
||||
log.Println("arrêt propre")
|
||||
}
|
||||
|
||||
@@ -4,19 +4,19 @@ import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/vaultdrop/backend/models"
|
||||
"github.com/kazier/backend/models"
|
||||
)
|
||||
|
||||
func TestUserCreation(t *testing.T) {
|
||||
|
||||
err, userAntoine := models.NewUser("antoine")
|
||||
userAntoine, err := models.NewUser("antoine")
|
||||
if err != nil {
|
||||
t.Errorf(`Error creating antoine user %v`, err)
|
||||
t.Fatalf("creating antoine user: %v", err)
|
||||
}
|
||||
|
||||
err, userBob := models.NewUser("bob")
|
||||
userBob, err := models.NewUser("bob")
|
||||
if err != nil {
|
||||
t.Errorf(`Error creating bob user %v`, err)
|
||||
t.Fatalf("creating bob user: %v", err)
|
||||
}
|
||||
|
||||
fmt.Println(userAntoine, userBob)
|
||||
@@ -25,14 +25,14 @@ func TestUserCreation(t *testing.T) {
|
||||
|
||||
func TestCreateDocument(t *testing.T) {
|
||||
|
||||
err, document := models.NewDocument("paper.pdf", models.FILE, 1)
|
||||
document, err := models.NewDocument("paper.pdf", models.FILE, 1)
|
||||
if err != nil {
|
||||
t.Errorf(`Error creating document %v`, err)
|
||||
t.Fatalf("creating document: %v", err)
|
||||
}
|
||||
|
||||
err, directory := models.NewDocument("bob", models.DIRECTORY, 1)
|
||||
directory, err := models.NewDocument("bob", models.DIRECTORY, 1)
|
||||
if err != nil {
|
||||
t.Errorf(`Error creating bob directory %v`, err)
|
||||
t.Fatalf("creating bob directory: %v", err)
|
||||
}
|
||||
|
||||
fmt.Println(document, directory)
|
||||
|
||||
@@ -13,14 +13,41 @@ var expectedRoutes = []string{
|
||||
"GET /api/v1/shares/links/:token",
|
||||
|
||||
"GET /api/v1/files",
|
||||
"GET /api/v1/files/:id",
|
||||
"DELETE /api/v1/files/:id",
|
||||
"GET /api/v1/files/search",
|
||||
"GET /api/v1/files/folders",
|
||||
"GET /api/v1/files/:id",
|
||||
"GET /api/v1/files/:id/ocr",
|
||||
"GET /api/v1/files/:id/facts",
|
||||
"PATCH /api/v1/files/:id/facts",
|
||||
"GET /api/v1/files/:id/content",
|
||||
"GET /api/v1/files/:id/preview",
|
||||
"GET /api/v1/files/:id/shares",
|
||||
"GET /api/v1/files/:id/links",
|
||||
"GET /api/v1/files/:id/placements",
|
||||
"GET /api/v1/files/:id/versions",
|
||||
"GET /api/v1/files/:id/relations",
|
||||
"POST /api/v1/files/:id/relations/verdict",
|
||||
"GET /api/v1/duplicates",
|
||||
"POST /api/v1/export",
|
||||
"POST /api/v1/files/:id/restore",
|
||||
"POST /api/v1/files/:id/versions/:n/restore",
|
||||
"GET /api/v1/trash",
|
||||
"DELETE /api/v1/files/:id",
|
||||
"POST /api/v1/files/upload",
|
||||
|
||||
"GET /api/v1/users/resolve",
|
||||
"PATCH /api/v1/users/me/password",
|
||||
"GET /api/v1/users/me/usage",
|
||||
|
||||
"GET /api/v1/devices",
|
||||
"DELETE /api/v1/devices/:id",
|
||||
|
||||
"POST /api/v1/groups",
|
||||
"GET /api/v1/groups",
|
||||
"POST /api/v1/groups/:id/members",
|
||||
"GET /api/v1/groups/:id/members",
|
||||
"DELETE /api/v1/groups/:id/members/:userId",
|
||||
"DELETE /api/v1/groups/:id",
|
||||
|
||||
"POST /api/v1/ocr/jobs",
|
||||
"GET /api/v1/ocr/jobs/:id",
|
||||
@@ -43,3 +70,21 @@ func TestRoutesMatchClientContract(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// TestStaticFileRoutesListedBeforeParam (LOT 6 §1) : verrouille que les routes
|
||||
// statiques /files/search et /files/folders restent déclarées avant
|
||||
// /files/:id dans le contrat. Gin priorise déjà le statique sur le paramétré
|
||||
// à l'exécution, mais l'ordre source documente l'intention ("/files/folders
|
||||
// ne doit jamais être avalé par FilesGet avec id="folders"). Le comportement
|
||||
// réel est couvert côté handlers (folders→200, :id="folders"→404).
|
||||
func TestStaticFileRoutesListedBeforeParam(t *testing.T) {
|
||||
index := map[string]int{}
|
||||
for i, r := range expectedRoutes {
|
||||
index[r] = i
|
||||
}
|
||||
for _, static := range []string{"GET /api/v1/files/search", "GET /api/v1/files/folders"} {
|
||||
if index[static] > index["GET /api/v1/files/:id"] {
|
||||
t.Errorf("contrat: %q doit rester listé avant %q (ordre §1)", static, "GET /api/v1/files/:id")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
@@ -13,39 +15,86 @@ type ApplicationConfig struct {
|
||||
UploadDir string
|
||||
MaxFileSizeMB int64
|
||||
OcrLang string
|
||||
OcrWorkers int
|
||||
AuthSecret string
|
||||
AdminUsername string
|
||||
AdminPassword string
|
||||
}
|
||||
|
||||
func LoadApplicationConfig() (error, *ApplicationConfig) {
|
||||
// Sécurité AUTH_SECRET (FEEDBACK #4) :
|
||||
// - valeur par défaut DEV UNIQUEMENT (jamais en production : refusée au boot
|
||||
// si APP_ENV/ENV vaut "prod"/"production") ;
|
||||
// - longueur minimale 32 caractères partout (dev comme prod) : le secret ne
|
||||
// sert qu'à dériver la clé paseto v4-local (SHA256), un secret court ou
|
||||
// deviné = tokens forgeables.
|
||||
//
|
||||
// NOTE inter-lots (LOT 6 « secret-doc ») : aligner backend/.env.example —
|
||||
// documenter « ≥ 32 caractères, ex. `openssl rand -hex 32` » et ne plus
|
||||
// suggérer une valeur copiable telle quelle.
|
||||
const (
|
||||
defaultAuthSecret = "dev-secret-change-me-please-set-in-prod"
|
||||
minAuthSecretLen = 32
|
||||
)
|
||||
|
||||
func LoadApplicationConfig() (*ApplicationConfig, error) {
|
||||
|
||||
// .env optionnel — les défauts suffisent pour le dev local.
|
||||
_ = godotenv.Load()
|
||||
|
||||
port, err := getInt("PORT", 8080)
|
||||
if err != nil {
|
||||
return err, nil
|
||||
return nil, err
|
||||
}
|
||||
if port < 1 || port > 65535 {
|
||||
return nil, fmt.Errorf("PORT hors plage 1-65535: %d", port)
|
||||
}
|
||||
|
||||
maxSize, err := getInt("MAX_FILE_SIZE_MB", 50)
|
||||
if err != nil {
|
||||
return err, nil
|
||||
return nil, err
|
||||
}
|
||||
if maxSize < 1 || maxSize > 1024 {
|
||||
return nil, fmt.Errorf("MAX_FILE_SIZE_MB hors plage 1-1024: %d", maxSize)
|
||||
}
|
||||
|
||||
return nil, &ApplicationConfig{
|
||||
ocrWorkers, err := getInt("OCR_WORKERS", 2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ocrWorkers < 1 || ocrWorkers > 16 {
|
||||
return nil, fmt.Errorf("OCR_WORKERS hors plage 1-16: %d", ocrWorkers)
|
||||
}
|
||||
|
||||
authSecret := get("AUTH_SECRET", defaultAuthSecret)
|
||||
if isProduction() && (authSecret == "" || authSecret == defaultAuthSecret) {
|
||||
return nil, fmt.Errorf("AUTH_SECRET doit être défini en production (pas de défaut)")
|
||||
}
|
||||
if len(authSecret) < minAuthSecretLen {
|
||||
return nil, fmt.Errorf("AUTH_SECRET trop court (min %d caractères)", minAuthSecretLen)
|
||||
}
|
||||
|
||||
return &ApplicationConfig{
|
||||
Port: port,
|
||||
DatabaseURL: get("DATABASE_URL", "postgres://vaultdrop:vaultdrop@localhost:5432/vaultdrop_dev?sslmode=disable"),
|
||||
DatabaseURL: get("DATABASE_URL", "postgres://kazier:kazier@localhost:5432/kazier_dev?sslmode=disable"),
|
||||
UploadDir: get("UPLOAD_DIR", "./uploads"),
|
||||
MaxFileSizeMB: int64(maxSize),
|
||||
OcrLang: get("OCR_LANG", "fra+eng"),
|
||||
AuthSecret: get("AUTH_SECRET", "dev-secret-change-me"),
|
||||
OcrWorkers: ocrWorkers,
|
||||
AuthSecret: authSecret,
|
||||
AdminUsername: get("ADMIN_USERNAME", ""),
|
||||
AdminPassword: get("ADMIN_PASSWORD", ""),
|
||||
}
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
func isProduction() bool {
|
||||
env := strings.ToLower(strings.TrimSpace(os.Getenv("APP_ENV")))
|
||||
if env == "" {
|
||||
env = strings.ToLower(strings.TrimSpace(os.Getenv("ENV")))
|
||||
}
|
||||
return env == "production" || env == "prod"
|
||||
}
|
||||
|
||||
func get(name string, defaultValue string) string {
|
||||
value := os.Getenv(name)
|
||||
if value == "" {
|
||||
|
||||
@@ -9,7 +9,8 @@ func clearEnv(t *testing.T) {
|
||||
t.Helper()
|
||||
for _, name := range []string{
|
||||
"PORT", "DATABASE_URL", "UPLOAD_DIR", "MAX_FILE_SIZE_MB",
|
||||
"OCR_LANG", "AUTH_SECRET", "ADMIN_USERNAME", "ADMIN_PASSWORD",
|
||||
"OCR_LANG", "OCR_WORKERS", "AUTH_SECRET", "ADMIN_USERNAME", "ADMIN_PASSWORD",
|
||||
"APP_ENV", "ENV",
|
||||
} {
|
||||
t.Setenv(name, "")
|
||||
}
|
||||
@@ -18,7 +19,7 @@ func clearEnv(t *testing.T) {
|
||||
func TestLoadApplicationDefaults(t *testing.T) {
|
||||
clearEnv(t)
|
||||
|
||||
err, cfg := LoadApplicationConfig()
|
||||
cfg, err := LoadApplicationConfig()
|
||||
if err != nil {
|
||||
t.Fatalf("LoadApplicationConfig: %v", err)
|
||||
}
|
||||
@@ -28,7 +29,7 @@ func TestLoadApplicationDefaults(t *testing.T) {
|
||||
if cfg.MaxFileSizeMB != 50 {
|
||||
t.Errorf("MaxFileSizeMB = %d, attendu 50", cfg.MaxFileSizeMB)
|
||||
}
|
||||
if cfg.DatabaseURL != "postgres://vaultdrop:vaultdrop@localhost:5432/vaultdrop_dev?sslmode=disable" {
|
||||
if cfg.DatabaseURL != "postgres://kazier:kazier@localhost:5432/kazier_dev?sslmode=disable" {
|
||||
t.Errorf("DatabaseURL défaut inattendu: %q", cfg.DatabaseURL)
|
||||
}
|
||||
if cfg.UploadDir != "./uploads" {
|
||||
@@ -37,9 +38,15 @@ func TestLoadApplicationDefaults(t *testing.T) {
|
||||
if cfg.OcrLang != "fra+eng" {
|
||||
t.Errorf("OcrLang défaut inattendu: %q", cfg.OcrLang)
|
||||
}
|
||||
if cfg.AuthSecret != "dev-secret-change-me" {
|
||||
if cfg.OcrWorkers != 2 {
|
||||
t.Errorf("OcrWorkers = %d, attendu 2", cfg.OcrWorkers)
|
||||
}
|
||||
if cfg.AuthSecret != defaultAuthSecret {
|
||||
t.Errorf("AuthSecret défaut inattendu: %q", cfg.AuthSecret)
|
||||
}
|
||||
if len(cfg.AuthSecret) < minAuthSecretLen {
|
||||
t.Errorf("AuthSecret défaut trop court pour le minimum %d: %q", minAuthSecretLen, cfg.AuthSecret)
|
||||
}
|
||||
if cfg.AdminUsername != "" || cfg.AdminPassword != "" {
|
||||
t.Errorf("identifiants admin doivent être vides par défaut: %+v", cfg)
|
||||
}
|
||||
@@ -52,32 +59,67 @@ func TestLoadApplicationEnvOverrides(t *testing.T) {
|
||||
t.Setenv("DATABASE_URL", "postgres://u:p@host:5433/db?sslmode=disable")
|
||||
t.Setenv("UPLOAD_DIR", "/tmp/up")
|
||||
t.Setenv("OCR_LANG", "eng")
|
||||
t.Setenv("AUTH_SECRET", "super-secret")
|
||||
t.Setenv("OCR_WORKERS", "4")
|
||||
t.Setenv("AUTH_SECRET", "super-secret-0123456789abcdef01234567")
|
||||
t.Setenv("ADMIN_USERNAME", "root")
|
||||
t.Setenv("ADMIN_PASSWORD", "toor")
|
||||
|
||||
err, cfg := LoadApplicationConfig()
|
||||
cfg, err := LoadApplicationConfig()
|
||||
if err != nil {
|
||||
t.Fatalf("LoadApplicationConfig: %v", err)
|
||||
}
|
||||
if cfg.Port != 9090 || cfg.MaxFileSizeMB != 120 || cfg.DatabaseURL != "postgres://u:p@host:5433/db?sslmode=disable" ||
|
||||
cfg.UploadDir != "/tmp/up" || cfg.OcrLang != "eng" || cfg.AuthSecret != "super-secret" ||
|
||||
cfg.UploadDir != "/tmp/up" || cfg.OcrLang != "eng" || cfg.OcrWorkers != 4 || cfg.AuthSecret != "super-secret-0123456789abcdef01234567" ||
|
||||
cfg.AdminUsername != "root" || cfg.AdminPassword != "toor" {
|
||||
t.Errorf("overrides non appliqués: %+v", cfg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadApplicationRejectsShortSecret(t *testing.T) {
|
||||
clearEnv(t)
|
||||
t.Setenv("AUTH_SECRET", "too-short")
|
||||
if _, err := LoadApplicationConfig(); err == nil {
|
||||
t.Error("AUTH_SECRET court : attendu une erreur (min 32)")
|
||||
}
|
||||
|
||||
// Le défaut dev passe en dev mais est refusé en production.
|
||||
clearEnv(t)
|
||||
if _, err := LoadApplicationConfig(); err != nil {
|
||||
t.Errorf("défaut dev en dev : inattendu %v", err)
|
||||
}
|
||||
for _, env := range []string{"prod", "production", "PROD"} {
|
||||
clearEnv(t)
|
||||
t.Setenv("APP_ENV", env)
|
||||
if _, err := LoadApplicationConfig(); err == nil {
|
||||
t.Errorf("défaut dev avec APP_ENV=%q : attendu une erreur", env)
|
||||
}
|
||||
clearEnv(t)
|
||||
t.Setenv("ENV", env)
|
||||
if _, err := LoadApplicationConfig(); err == nil {
|
||||
t.Errorf("défaut dev avec ENV=%q : attendu une erreur", env)
|
||||
}
|
||||
}
|
||||
|
||||
// Un secret long et explicite passe en production.
|
||||
clearEnv(t)
|
||||
t.Setenv("APP_ENV", "prod")
|
||||
t.Setenv("AUTH_SECRET", "prod-secret-0123456789abcdef01234567")
|
||||
if _, err := LoadApplicationConfig(); err != nil {
|
||||
t.Errorf("secret prod valide : inattendu %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadApplicationRejectsInvalidInt(t *testing.T) {
|
||||
clearEnv(t)
|
||||
|
||||
t.Setenv("PORT", "not-a-number")
|
||||
if err, _ := LoadApplicationConfig(); err == nil {
|
||||
if _, err := LoadApplicationConfig(); err == nil {
|
||||
t.Error("PORT invalide : attendu une erreur")
|
||||
}
|
||||
|
||||
clearEnv(t)
|
||||
t.Setenv("MAX_FILE_SIZE_MB", "99999999999999999999999")
|
||||
if err, _ := LoadApplicationConfig(); err == nil {
|
||||
if _, err := LoadApplicationConfig(); err == nil {
|
||||
t.Error("MAX_FILE_SIZE_MB invalide : attendu une erreur")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,21 @@
|
||||
-- Reverse de 000007. Réversible SEULEMENT sur base vide (les ressources sont
|
||||
-- détruites dans les deux sens : sans device associé, il n'y a rien à
|
||||
-- préserver).
|
||||
DELETE FROM resources;
|
||||
-- Reverse de 000007. Chemin TESTS uniquement : ne jamais down en production
|
||||
-- avec des données.
|
||||
-- Les ressources retrouvent leur device d'origine via devices.user_id
|
||||
-- (premier device trouvé en cas de multi-device — mapping non bijectif
|
||||
-- assumé) ; les orphelines (user_id NULL, ou user sans device, ou valeur ne
|
||||
-- correspondant à aucun device) sont détruites car owner_id est NOT NULL
|
||||
-- avec FK vers devices.
|
||||
UPDATE resources r
|
||||
SET user_id = d.device_id
|
||||
FROM devices d
|
||||
WHERE d.user_id = r.user_id;
|
||||
|
||||
DELETE FROM resources
|
||||
WHERE user_id IS NULL
|
||||
OR user_id NOT IN (SELECT device_id FROM devices);
|
||||
|
||||
ALTER TABLE resources DROP CONSTRAINT resources_user_id_fkey;
|
||||
ALTER TABLE resources RENAME COLUMN user_id TO owner_id;
|
||||
ALTER TABLE resources ALTER COLUMN owner_id SET NOT NULL;
|
||||
ALTER TABLE resources ADD CONSTRAINT resources_owner_id_fkey
|
||||
FOREIGN KEY (owner_id) REFERENCES devices(device_id) ON DELETE CASCADE;
|
||||
@@ -1,15 +1,35 @@
|
||||
-- Bascule ownership device → user (étapes 6-10 de la tranche identité).
|
||||
-- Destructif ASSUMÉ : à ce stade aucune release V1, la base de dev est RESET
|
||||
-- (DELETE trivial sur une table vide ; PAS de user marqueur). Le modele
|
||||
-- resources reste parfaitement identique par ailleurs.
|
||||
DELETE FROM resources;
|
||||
-- Migration DOUCE et non destructive (cf. #10) : AUCUNE suppression de
|
||||
-- ressources (l'ancien DELETE FROM resources est supprimé).
|
||||
-- Le device d'origine est résolu vers son dernier utilisateur connu
|
||||
-- (devices.user_id) ; les ressources orphelines (device sans user connu ou
|
||||
-- device inconnu) gardent user_id NULL — visibles par personne mais
|
||||
-- conservées — et user_id reste donc NULLABLE après cette migration.
|
||||
|
||||
ALTER TABLE resources DROP CONSTRAINT resources_owner_id_fkey;
|
||||
ALTER TABLE resources RENAME COLUMN owner_id TO user_id;
|
||||
|
||||
-- La colonne hérite du NOT NULL de owner_id : on l'assouplit AVANT le
|
||||
-- backfill pour accueillir les orphelines.
|
||||
ALTER TABLE resources ALTER COLUMN user_id DROP NOT NULL;
|
||||
|
||||
-- Backfill : device → dernier utilisateur connu.
|
||||
UPDATE resources r
|
||||
SET user_id = d.user_id
|
||||
FROM devices d
|
||||
WHERE r.user_id = d.device_id
|
||||
AND d.user_id IS NOT NULL;
|
||||
|
||||
-- Orphelines : tout ce qui ne référence toujours pas un user existant → NULL.
|
||||
UPDATE resources
|
||||
SET user_id = NULL
|
||||
WHERE user_id IS NOT NULL
|
||||
AND user_id NOT IN (SELECT id FROM users);
|
||||
|
||||
ALTER TABLE resources ADD CONSTRAINT resources_user_id_fkey
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
|
||||
|
||||
-- Les index créés en 000003 (idx_resources_owner, idx_resources_root_name
|
||||
-- sur owner_id) suivent le renommage Intrinsèquement : ils portent désormais
|
||||
-- sur owner_id) suivent le renommage intrinsèquement : ils portent désormais
|
||||
-- sur user_id. L'unicité racine devient donc « 1 nom par USER » (multi-device
|
||||
-- possible pour le même compte).
|
||||
@@ -1,4 +1,23 @@
|
||||
-- Down de 000008 : retour operation_id TEXT 32-hex → BIGINT.
|
||||
-- PERTES ASSUMÉES, chemin TESTS uniquement (migrate down complet) : seules
|
||||
-- les valeurs exclusivement numériques sur ≤ 18 chiffres significatifs
|
||||
-- (traces héritées paddées par le up, p.ex. '0000…0042' → 42) survivent ;
|
||||
-- les vrais 32-hex client-generated (contenant a-f, ou > 18 chiffres pour
|
||||
-- éviter tout overflow bigint) ne sont pas convertibles et leurs lignes
|
||||
-- sont supprimées. L'ancien `USING operation_id::bigint` échouait sur ces
|
||||
-- valeurs. Ne jamais down en production avec des données V1 : recréer la
|
||||
-- base plutôt que `migrate down`.
|
||||
ALTER TABLE operations DROP CONSTRAINT operations_operation_id_hex_check;
|
||||
|
||||
ALTER TABLE operations
|
||||
ALTER COLUMN operation_id TYPE BIGINT USING operation_id::bigint;
|
||||
ALTER TABLE operations ADD COLUMN operation_id_bigint BIGINT;
|
||||
|
||||
UPDATE operations
|
||||
SET operation_id_bigint = operation_id::bigint
|
||||
WHERE operation_id ~ '^0*[0-9]{1,18}$';
|
||||
|
||||
DELETE FROM operations WHERE operation_id_bigint IS NULL;
|
||||
|
||||
ALTER TABLE operations DROP COLUMN operation_id;
|
||||
ALTER TABLE operations RENAME COLUMN operation_id_bigint TO operation_id;
|
||||
ALTER TABLE operations ALTER COLUMN operation_id SET NOT NULL;
|
||||
ALTER TABLE operations ADD CONSTRAINT operations_unique_per_device UNIQUE (device_id, operation_id);
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE resources DROP COLUMN ocr_text;
|
||||
@@ -0,0 +1,5 @@
|
||||
-- Texte OCR porté sur la ressource (docs/api-v1.md §5) : le worker OCR écrit
|
||||
-- l'extrait (`resources.ocr_text`, updated_at bumpé) pour qu'il soit
|
||||
-- consultable par tous les devices du user ET par les grantees
|
||||
-- (GET /files/:id/ocr, visibilité viewer+).
|
||||
ALTER TABLE resources ADD COLUMN ocr_text TEXT;
|
||||
@@ -0,0 +1 @@
|
||||
DROP INDEX IF EXISTS idx_resources_owner_updated_active;
|
||||
@@ -0,0 +1,7 @@
|
||||
-- LOT 4 (DATA ownership) : index snapshot delta — ListOwned filtre
|
||||
-- (user_id, updated_at) WHERE deleted_at IS NULL. Numéro réservé au LOT 4
|
||||
-- par le plan TASKS_PARALLELES.md (LOT 5 = 000012, LOT 3 = 000013) : cette
|
||||
-- migration bouche aussi le trou de version (golang-migrate refuse les gaps
|
||||
-- au `Up` une fois 000012/000013 présentes).
|
||||
CREATE INDEX IF NOT EXISTS idx_resources_owner_updated_active
|
||||
ON resources(user_id, updated_at) WHERE deleted_at IS NULL;
|
||||
@@ -0,0 +1 @@
|
||||
DROP INDEX IF EXISTS idx_ocr_jobs_status_created;
|
||||
@@ -0,0 +1,4 @@
|
||||
-- Index composite pour la file OCR : ClaimNext filtre `status = 'queued'` et
|
||||
-- trie par `created_at, job_id` (FIFO). L'index simple (status) de 000005
|
||||
-- est conservé (LOT 4 reste propriétaire du schéma historique).
|
||||
CREATE INDEX IF NOT EXISTS idx_ocr_jobs_status_created ON ocr_jobs(status, created_at);
|
||||
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE operations DROP CONSTRAINT IF EXISTS operations_payload_hash_hex_check;
|
||||
ALTER TABLE operations DROP COLUMN IF EXISTS payload_hash;
|
||||
@@ -0,0 +1,10 @@
|
||||
-- Trace outbox : hash du payload (A9, LOT 3).
|
||||
-- Au rejeu du même (device_id, operation_id) avec un payload différent, le
|
||||
-- serveur rejette en INVALID_REQUEST au lieu d'un skip silencieux.
|
||||
-- Colonne NULLABLE : les traces héritées (pré-000013) restent valides ; pour
|
||||
-- elles, le hash est recalculé en Go depuis le payload stocké.
|
||||
ALTER TABLE operations ADD COLUMN payload_hash TEXT;
|
||||
|
||||
ALTER TABLE operations
|
||||
ADD CONSTRAINT operations_payload_hash_hex_check
|
||||
CHECK (payload_hash IS NULL OR payload_hash ~ '^[0-9a-f]{64}$');
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE resources DROP COLUMN IF EXISTS has_content;
|
||||
@@ -0,0 +1,6 @@
|
||||
-- V2 placements : `has_content` = « octets vus présents » (lazy healing).
|
||||
-- TRUE à chaque upload (nouveau ou ciblé), healen FALSE→TRUE au premier
|
||||
-- `OpenContent` réussi, TRUE→FALSE quand les octets ont disparu sous
|
||||
-- UPLOAD_DIR. Les lignes existantes partent à FALSE (inconnu, pas absent) :
|
||||
-- le premier accès les heal, aucun scan au boot.
|
||||
ALTER TABLE resources ADD COLUMN has_content BOOLEAN NOT NULL DEFAULT FALSE;
|
||||
@@ -0,0 +1,2 @@
|
||||
DROP TABLE IF EXISTS resource_placements;
|
||||
DROP TABLE IF EXISTS storage_locations;
|
||||
@@ -0,0 +1,25 @@
|
||||
-- V2 placements multi-device : où existent les octets d'une ressource.
|
||||
-- `storage_locations` modélise chaque device (le serveur reste implicite via
|
||||
-- `resources.has_content`, pas de ligne). `resource_placements` matérialise
|
||||
-- les reports clients (`report_bytes` via l'outbox) : hint de présence,
|
||||
-- jamais une preuve — le serveur ne vérifie pas les octets du device.
|
||||
CREATE TABLE storage_locations (
|
||||
id TEXT PRIMARY KEY CHECK (id ~ '^[0-9a-f]{32}$'),
|
||||
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
device_id TEXT NOT NULL UNIQUE CHECK (device_id ~ '^[0-9a-f]{32}$'),
|
||||
type TEXT NOT NULL DEFAULT 'mobile' CHECK (type IN ('mobile', 'desktop', 'server')),
|
||||
last_seen_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
created_at TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_storage_locations_user ON storage_locations(user_id);
|
||||
|
||||
CREATE TABLE resource_placements (
|
||||
resource_id TEXT NOT NULL REFERENCES resources(resource_id) ON DELETE CASCADE,
|
||||
location_id TEXT NOT NULL REFERENCES storage_locations(id) ON DELETE CASCADE,
|
||||
has_bytes BOOLEAN NOT NULL,
|
||||
updated_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
PRIMARY KEY (resource_id, location_id)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_placements_location ON resource_placements(location_id);
|
||||
@@ -0,0 +1,6 @@
|
||||
DROP INDEX IF EXISTS idx_shares_active_group;
|
||||
ALTER TABLE shares DROP CONSTRAINT IF EXISTS shares_grantee_xor_check;
|
||||
ALTER TABLE shares ALTER COLUMN grantee_user_id SET NOT NULL;
|
||||
ALTER TABLE shares DROP COLUMN IF EXISTS grantee_group_id;
|
||||
DROP TABLE IF EXISTS group_members;
|
||||
DROP TABLE IF EXISTS groups;
|
||||
@@ -0,0 +1,32 @@
|
||||
-- V2-6 groupes : partage vers un ensemble d'utilisateurs (famille, équipe).
|
||||
-- `groups` : identité nommée possédée (created_by) ; `group_members` :
|
||||
-- appartenances (pas de groupes imbriqués — un niveau, documenté).
|
||||
-- `shares` accepte une cible groupe : exactement une des deux cibles est
|
||||
-- renseignée (CHECK), avec un index d'unicité active par cible.
|
||||
CREATE TABLE groups (
|
||||
id TEXT PRIMARY KEY CHECK (id ~ '^[0-9a-f]{32}$'),
|
||||
name TEXT NOT NULL CHECK (name <> '' AND char_length(name) <= 255),
|
||||
created_by TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE TABLE group_members (
|
||||
group_id TEXT NOT NULL REFERENCES groups(id) ON DELETE CASCADE,
|
||||
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
PRIMARY KEY (group_id, user_id)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_group_members_user ON group_members(user_id);
|
||||
|
||||
ALTER TABLE shares ADD COLUMN grantee_group_id TEXT REFERENCES groups(id) ON DELETE CASCADE;
|
||||
ALTER TABLE shares ALTER COLUMN grantee_user_id DROP NOT NULL;
|
||||
ALTER TABLE shares
|
||||
ADD CONSTRAINT shares_grantee_xor_check
|
||||
CHECK (
|
||||
(grantee_user_id IS NOT NULL AND grantee_group_id IS NULL)
|
||||
OR (grantee_user_id IS NULL AND grantee_group_id IS NOT NULL)
|
||||
);
|
||||
CREATE UNIQUE INDEX idx_shares_active_group
|
||||
ON shares(resource_id, grantee_group_id) WHERE revoked_at IS NULL AND grantee_group_id IS NOT NULL;
|
||||
@@ -0,0 +1 @@
|
||||
DROP INDEX IF EXISTS idx_resources_trashed_active;
|
||||
@@ -0,0 +1,7 @@
|
||||
-- V3 corbeille : `deleted_at` est le timestamp de trash (restauration =
|
||||
-- `deleted_at = NULL`, purge = hard delete après 30j via PurgeExpired).
|
||||
-- Toutes les lectures filtrent déjà `deleted_at IS NULL` : les trashés sont
|
||||
-- invisibles partout sauf via les requêtes dédiées (ListTrashed). Cet index
|
||||
-- partiel accélère la corbeille (`GET /trash`) et la passe de purge.
|
||||
CREATE INDEX IF NOT EXISTS idx_resources_trashed_active
|
||||
ON resources(user_id, deleted_at) WHERE deleted_at IS NOT NULL;
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS resource_versions;
|
||||
@@ -0,0 +1,19 @@
|
||||
-- V3 versions : historique léger des octets écrasés (N=3, cf. V3.md P1).
|
||||
-- À chaque overwrite (upload ciblé), l'ancienne copie physique est déplacée
|
||||
-- sous `stored_path` (relatif à UPLOAD_DIR) et sa métadonnée figée ici.
|
||||
-- Restaurer = recopier vers l'emplacement live (l'actuel est à son tour
|
||||
-- versionné). `ON DELETE CASCADE` : purger la ressource purge ses versions.
|
||||
CREATE TABLE resource_versions (
|
||||
id TEXT PRIMARY KEY CHECK (id ~ '^[0-9a-f]{32}$'),
|
||||
resource_id TEXT NOT NULL REFERENCES resources(resource_id) ON DELETE CASCADE,
|
||||
version_no INT NOT NULL CHECK (version_no >= 1),
|
||||
size_bytes BIGINT NOT NULL DEFAULT 0,
|
||||
mime_type VARCHAR(255),
|
||||
extension VARCHAR(64),
|
||||
content_hash VARCHAR(128),
|
||||
stored_path TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
UNIQUE (resource_id, version_no)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_versions_resource ON resource_versions(resource_id);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS resource_facts;
|
||||
@@ -0,0 +1,23 @@
|
||||
-- V3 extract générique (S2) : faits universels extraits du texte OCR
|
||||
-- (dates, montants, émetteurs, références, type suggéré) — cf. V3.md §2.
|
||||
-- `doc_type` n'est qu'un fait comme un autre (suggestion, jamais gate) ;
|
||||
-- `origin` distingue l'auto (remplaçable à chaque passe) du `user` (S3,
|
||||
-- correction en un tap — l'auto ne l'écrase jamais). Cascade : purger la
|
||||
-- ressource purge ses faits.
|
||||
CREATE TABLE resource_facts (
|
||||
id TEXT PRIMARY KEY CHECK (id ~ '^[0-9a-f]{32}$'),
|
||||
resource_id TEXT NOT NULL REFERENCES resources(resource_id) ON DELETE CASCADE,
|
||||
fact_type TEXT NOT NULL CHECK (fact_type IN (
|
||||
'date', 'due_date', 'period', 'amount', 'issuer', 'reference', 'doc_type'
|
||||
)),
|
||||
value_text TEXT,
|
||||
value_num NUMERIC,
|
||||
value_date DATE,
|
||||
confidence REAL NOT NULL CHECK (confidence >= 0 AND confidence <= 1),
|
||||
origin TEXT NOT NULL DEFAULT 'auto' CHECK (origin IN ('auto', 'user')),
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_facts_resource ON resource_facts(resource_id);
|
||||
CREATE INDEX idx_facts_type_value ON resource_facts(fact_type, value_text);
|
||||
@@ -0,0 +1,2 @@
|
||||
DROP INDEX IF EXISTS idx_resources_search_tsv;
|
||||
ALTER TABLE resources DROP COLUMN IF EXISTS search_tsv;
|
||||
@@ -0,0 +1,13 @@
|
||||
-- V3 recherche (S4) : plein-texte français sur (nom + OCR).
|
||||
-- Colonne générée stockée (`to_tsvector(regconfig, text)` est IMMUTABLE,
|
||||
-- donc autorisée en GENERATED) : pas de trigger, backfill automatique des
|
||||
-- lignes existantes. Index GIN. Les faits (`resource_facts`) sont joints à
|
||||
-- la requête (jamais dupliqués) : ils pondèrent le rang sans jamais exclure
|
||||
-- (humble par design — une confiance < 0.5 déclasse, cf. SearchDocuments).
|
||||
ALTER TABLE resources
|
||||
ADD COLUMN search_tsv TSVECTOR
|
||||
GENERATED ALWAYS AS (
|
||||
to_tsvector('french', COALESCE(name, '') || ' ' || COALESCE(ocr_text, ''))
|
||||
) STORED;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_resources_search_tsv ON resources USING GIN (search_tsv);
|
||||
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE resources DROP COLUMN IF EXISTS scan_session_id;
|
||||
DROP TABLE IF EXISTS resource_links;
|
||||
@@ -0,0 +1,32 @@
|
||||
-- V3 relations documentaires (S5) : graphe auto + verdicts utilisateur.
|
||||
-- `resource_links` : arêtes orientées (from_id → to_id) typées, avec
|
||||
-- confiance et origine. `duplicate_of` (même content_hash), `same_scan`
|
||||
-- (même session de scan, déterministe), `same_issuer` / `same_reference`
|
||||
-- (faits normalisés égaux). `version_of` non matérialisé : les versions
|
||||
-- vivent DANS la ressource (resource_versions, 000018), pas entre ressources.
|
||||
-- `created_by` : 'auto' (dérivé serveur, écrasable) vs 'user' (verdict
|
||||
-- explicite — la réconciliation ne touche jamais aux verdicts). Les
|
||||
-- invalidations (confidence 0) sont conservées (pas de re-création auto).
|
||||
-- `resources.scan_session_id` : session de scan opaque fournie par le client
|
||||
-- dans le payload `create_resource` (zéro nouvelle op) — le serveur en
|
||||
-- déduit les arêtes `same_scan`.
|
||||
CREATE TABLE resource_links (
|
||||
id TEXT PRIMARY KEY CHECK (id ~ '^[0-9a-f]{32}$'),
|
||||
from_id TEXT NOT NULL REFERENCES resources(resource_id) ON DELETE CASCADE,
|
||||
to_id TEXT NOT NULL REFERENCES resources(resource_id) ON DELETE CASCADE,
|
||||
link_type TEXT NOT NULL CHECK (link_type IN (
|
||||
'same_scan', 'duplicate_of', 'same_issuer', 'same_reference'
|
||||
)),
|
||||
confidence REAL NOT NULL CHECK (confidence >= 0 AND confidence <= 1),
|
||||
created_by TEXT NOT NULL DEFAULT 'auto' CHECK (created_by IN ('auto', 'user')),
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
CHECK (from_id != to_id),
|
||||
UNIQUE (from_id, to_id, link_type)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_links_from ON resource_links(from_id);
|
||||
CREATE INDEX idx_links_to ON resource_links(to_id);
|
||||
|
||||
ALTER TABLE resources ADD COLUMN scan_session_id TEXT;
|
||||
CREATE INDEX IF NOT EXISTS idx_resources_scan_session ON resources(user_id, scan_session_id)
|
||||
WHERE scan_session_id IS NOT NULL;
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE users DROP COLUMN IF EXISTS token_version;
|
||||
@@ -0,0 +1,6 @@
|
||||
-- V3 S6 : révocation des tokens au changement de mot de passe.
|
||||
-- `token_version` est bumpé par PATCH /users/me/password ; le middleware
|
||||
-- rejette les tokens dont le claim `tv` diffère (401). Défaut 0 : les tokens
|
||||
-- pré-V3 (sans claim `tv`, lus comme 0) restent valides jusqu'au premier
|
||||
-- changement de mot de passe — montée en version sans coupure.
|
||||
ALTER TABLE users ADD COLUMN token_version INT NOT NULL DEFAULT 0;
|
||||
@@ -0,0 +1,2 @@
|
||||
-- No-op : la colonne prédate cette migration (000001), on ne la supprime pas.
|
||||
SELECT 1;
|
||||
@@ -0,0 +1,6 @@
|
||||
-- V3 S6 (A2) : quota de stockage par compte (10 Go par défaut, V2.md §3.1).
|
||||
-- Garde-fou à l'upload (413 QUOTA_EXCEEDED) + affichage `GET /users/me/usage`.
|
||||
-- L'usage est calculé (somme `size_bytes` des fichiers vifs), jamais stocké.
|
||||
-- Note : la colonne existe depuis 000001 (modèle V2) — IF NOT EXISTS pour
|
||||
-- les bases créées avant/après, sans double ajout.
|
||||
ALTER TABLE users ADD COLUMN IF NOT EXISTS storage_quota_bytes BIGINT NOT NULL DEFAULT 10737418240;
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS resource_previews;
|
||||
@@ -0,0 +1,13 @@
|
||||
-- V3 S7 : vignettes serveur pour les fichiers cloud-only (la galerie reste
|
||||
-- < 1 s sans rapatrier les octets). `resource_previews` mémorise la
|
||||
-- génération servie (taille + updated_at sources) pour ne régénérer que sur
|
||||
-- changement ; le fichier vit sous UPLOAD_DIR/.previews/<resource_id>.jpg
|
||||
-- (jamais versionné, reconstruit à la demande). Images : pur Go (stdlib +
|
||||
-- x/image). PDF : `pdftoppm` (poppler, cf. Dockerfile) page 1 ; sans l'outil
|
||||
-- ou autre type → pas de preview (404 NO_PREVIEW, l'UI montre l'icône).
|
||||
CREATE TABLE resource_previews (
|
||||
resource_id TEXT PRIMARY KEY REFERENCES resources(resource_id) ON DELETE CASCADE,
|
||||
src_size_bytes BIGINT NOT NULL,
|
||||
src_updated_at TIMESTAMPTZ NOT NULL,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
@@ -2,20 +2,21 @@ package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"os"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
const defaultTestDatabaseURL = "postgres://vaultdrop:vaultdrop@localhost:5432/vaultdrop_migrations_test?sslmode=disable"
|
||||
const defaultTestDatabaseURL = "postgres://kazier:kazier@localhost:5432/kazier_migrations_test?sslmode=disable"
|
||||
|
||||
func testDatabaseURL(t *testing.T) string {
|
||||
t.Helper()
|
||||
url := os.Getenv("TEST_DATABASE_URL")
|
||||
if url == "" {
|
||||
url = defaultTestDatabaseURL
|
||||
url := TestDatabaseURL(defaultTestDatabaseURL)
|
||||
if err := ensureTestDatabaseExists(url); err != nil {
|
||||
t.Fatalf("ensure database: %v", err)
|
||||
}
|
||||
conn, err := sql.Open("postgres", url)
|
||||
if err != nil {
|
||||
@@ -28,6 +29,41 @@ func testDatabaseURL(t *testing.T) string {
|
||||
return url
|
||||
}
|
||||
|
||||
// ensureTestDatabaseExists crée la base de tests du package quand Postgres est
|
||||
// joignable mais que la base n'est pas encore provisionnée (service CI frais,
|
||||
// volume local vierge). Le probing de disponibilité et le skip restent le rôle
|
||||
// du Ping de testDatabaseURL.
|
||||
func ensureTestDatabaseExists(databaseURL string) error {
|
||||
parsed, err := url.Parse(databaseURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dbName := strings.TrimPrefix(parsed.Path, "/")
|
||||
if dbName == "" {
|
||||
return nil
|
||||
}
|
||||
maintenance := *parsed
|
||||
maintenance.Path = "/postgres"
|
||||
conn, err := sql.Open("postgres", maintenance.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer conn.Close()
|
||||
if err := conn.Ping(); err != nil {
|
||||
return nil // Postgres down : le Ping du test skippera
|
||||
}
|
||||
var exists bool
|
||||
if err := conn.QueryRow(`SELECT EXISTS(SELECT 1 FROM pg_database WHERE datname = $1)`, dbName).Scan(&exists); err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
if _, err := conn.Exec(fmt.Sprintf(`CREATE DATABASE %s`, pq.QuoteIdentifier(dbName))); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func resetSchema(t *testing.T, url string) {
|
||||
t.Helper()
|
||||
conn, err := sql.Open("postgres", url)
|
||||
@@ -179,6 +215,33 @@ func TestMigrationsUpDown(t *testing.T) {
|
||||
t.Error("FK resources.user_id → users(id) manquante après 000007")
|
||||
}
|
||||
|
||||
// --- 000007 : migration douce (#10) ----------------------------------
|
||||
// user_id NULLABLE (orphelines conservées, pas de DELETE FROM resources).
|
||||
var userIDNullable string
|
||||
err = conn.QueryRow(`
|
||||
SELECT is_nullable FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'resources' AND column_name = 'user_id'`).Scan(&userIDNullable)
|
||||
if err != nil {
|
||||
t.Fatalf("resources.user_id nullabilité: %v", err)
|
||||
}
|
||||
if userIDNullable != "YES" {
|
||||
t.Error("resources.user_id doit être NULLABLE après 000007 (migration douce, orphelines)")
|
||||
}
|
||||
|
||||
// --- 000011 : index snapshot delta (LOT 4) -----------------------------
|
||||
var ownerUpdatedIdx int
|
||||
err = conn.QueryRow(`
|
||||
SELECT COUNT(*) FROM pg_index i
|
||||
JOIN pg_class t ON t.oid = i.indrelid
|
||||
WHERE t.relname = 'resources' AND i.indisvalid
|
||||
AND pg_get_indexdef(i.indexrelid) LIKE '%idx_resources_owner_updated_active%'`).Scan(&ownerUpdatedIdx)
|
||||
if err != nil {
|
||||
t.Fatalf("000011 index: %v", err)
|
||||
}
|
||||
if ownerUpdatedIdx == 0 {
|
||||
t.Error("index idx_resources_owner_updated_active manquant après 000011")
|
||||
}
|
||||
|
||||
// operation_id outbox = UUID 32-hex client-generated — cf. docs/api-v1.md §6.1
|
||||
var opType string
|
||||
err = conn.QueryRow(`
|
||||
@@ -270,6 +333,191 @@ func TestMigrationsUpDown(t *testing.T) {
|
||||
t.Error("index unique partiel (id) WHERE revoked_at IS NULL manquant sur share_links")
|
||||
}
|
||||
|
||||
// --- 000014 : has_content + 000015 : placements -------------------------
|
||||
var hasContentCol int
|
||||
err = conn.QueryRow(`
|
||||
SELECT COUNT(*) FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'resources' AND column_name = 'has_content'`).Scan(&hasContentCol)
|
||||
if err != nil {
|
||||
t.Fatalf("has_content column: %v", err)
|
||||
}
|
||||
if hasContentCol == 0 {
|
||||
t.Error("colonne resources.has_content manquante après 000014")
|
||||
}
|
||||
|
||||
assertTables(t, conn, "users", "devices", "resources", "operations", "ocr_jobs", "shares", "share_links", "storage_locations", "resource_placements", "schema_migrations")
|
||||
|
||||
assertHexCheck(t, conn, "storage_locations", "id")
|
||||
assertHexCheck(t, conn, "storage_locations", "device_id")
|
||||
assertHexCheck(t, conn, "groups", "id")
|
||||
|
||||
// shares XOR : exactement une cible (user ou groupe).
|
||||
var xorCheck int
|
||||
err = conn.QueryRow(`
|
||||
SELECT COUNT(*) FROM pg_constraint c
|
||||
JOIN pg_class t ON t.oid = c.conrelid
|
||||
WHERE t.relname = 'shares'
|
||||
AND pg_get_constraintdef(c.oid) LIKE '%grantee_user_id%'
|
||||
AND pg_get_constraintdef(c.oid) LIKE '%grantee_group_id%'`).Scan(&xorCheck)
|
||||
if err != nil {
|
||||
t.Fatalf("shares xor check: %v", err)
|
||||
}
|
||||
if xorCheck == 0 {
|
||||
t.Error("CHECK XOR (grantee_user_id / grantee_group_id) manquant sur shares après 000016")
|
||||
}
|
||||
|
||||
// --- 000016 : groupes + cible groupe sur shares -------------------------
|
||||
assertTables(t, conn, "users", "devices", "resources", "operations", "ocr_jobs", "shares", "share_links", "storage_locations", "resource_placements", "groups", "group_members", "schema_migrations")
|
||||
|
||||
// --- 000017 : corbeille (index partiel deleted_at) ----------------------
|
||||
var trashedIdx int
|
||||
err = conn.QueryRow(`
|
||||
SELECT COUNT(*) FROM pg_index i
|
||||
JOIN pg_class t ON t.oid = i.indrelid
|
||||
WHERE t.relname = 'resources' AND i.indisvalid
|
||||
AND pg_get_indexdef(i.indexrelid) LIKE '%idx_resources_trashed_active%'`).Scan(&trashedIdx)
|
||||
if err != nil {
|
||||
t.Fatalf("000017 index: %v", err)
|
||||
}
|
||||
if trashedIdx == 0 {
|
||||
t.Error("index idx_resources_trashed_active manquant après 000017")
|
||||
}
|
||||
|
||||
// --- 000018 : versions --------------------------------------------------
|
||||
assertTables(t, conn, "users", "devices", "resources", "operations", "ocr_jobs", "shares", "share_links", "storage_locations", "resource_placements", "groups", "group_members", "resource_versions", "schema_migrations")
|
||||
|
||||
assertHexCheck(t, conn, "resource_versions", "id")
|
||||
|
||||
var versionUniq int
|
||||
err = conn.QueryRow(`
|
||||
SELECT COUNT(*) FROM pg_index i
|
||||
JOIN pg_class t ON t.oid = i.indrelid
|
||||
WHERE t.relname = 'resource_versions' AND i.indisunique
|
||||
AND ARRAY(SELECT a.attname FROM unnest(i.indkey) WITH ORDINALITY k(attnum, ord)
|
||||
JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = k.attnum
|
||||
ORDER BY k.ord)::text[] = ARRAY['resource_id','version_no']`).Scan(&versionUniq)
|
||||
if err != nil {
|
||||
t.Fatalf("versions unique: %v", err)
|
||||
}
|
||||
if versionUniq == 0 {
|
||||
t.Error("contrainte UNIQUE(resource_id, version_no) manquante sur resource_versions après 000018")
|
||||
}
|
||||
|
||||
// --- 000019 : faits génériques -----------------------------------------
|
||||
assertTables(t, conn, "users", "devices", "resources", "operations", "ocr_jobs", "shares", "share_links", "storage_locations", "resource_placements", "groups", "group_members", "resource_versions", "resource_facts", "schema_migrations")
|
||||
|
||||
assertHexCheck(t, conn, "resource_facts", "id")
|
||||
|
||||
var factTypeCheck int
|
||||
err = conn.QueryRow(`
|
||||
SELECT COUNT(*) FROM pg_constraint c
|
||||
JOIN pg_class t ON t.oid = c.conrelid
|
||||
WHERE t.relname = 'resource_facts'
|
||||
AND pg_get_constraintdef(c.oid) LIKE '%doc_type%'
|
||||
AND pg_get_constraintdef(c.oid) LIKE '%due_date%'`).Scan(&factTypeCheck)
|
||||
if err != nil {
|
||||
t.Fatalf("check resource_facts.fact_type: %v", err)
|
||||
}
|
||||
if factTypeCheck == 0 {
|
||||
t.Error("CHECK fact_type manquant sur resource_facts après 000019")
|
||||
}
|
||||
|
||||
// --- 000020 : plein-texte ----------------------------------------------
|
||||
var tsvCol int
|
||||
err = conn.QueryRow(`
|
||||
SELECT COUNT(*) FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'resources' AND column_name = 'search_tsv'`).Scan(&tsvCol)
|
||||
if err != nil {
|
||||
t.Fatalf("search_tsv column: %v", err)
|
||||
}
|
||||
if tsvCol == 0 {
|
||||
t.Error("colonne resources.search_tsv manquante après 000020")
|
||||
}
|
||||
|
||||
var tsvIdx int
|
||||
err = conn.QueryRow(`
|
||||
SELECT COUNT(*) FROM pg_index i
|
||||
JOIN pg_class t ON t.oid = i.indrelid
|
||||
WHERE t.relname = 'resources' AND i.indisvalid
|
||||
AND pg_get_indexdef(i.indexrelid) LIKE '%idx_resources_search_tsv%'`).Scan(&tsvIdx)
|
||||
if err != nil {
|
||||
t.Fatalf("000020 index: %v", err)
|
||||
}
|
||||
if tsvIdx == 0 {
|
||||
t.Error("index idx_resources_search_tsv manquant après 000020")
|
||||
}
|
||||
|
||||
// --- 000021 : graphe documentaire ----------------------------------------
|
||||
assertTables(t, conn, "users", "devices", "resources", "operations", "ocr_jobs", "shares", "share_links", "storage_locations", "resource_placements", "groups", "group_members", "resource_versions", "resource_facts", "resource_links", "schema_migrations")
|
||||
|
||||
// resource_links.id : CHECK 32-hex (requête ciblée — le helper
|
||||
// générique matcherait `from_id <> to_id` qui contient aussi "id").
|
||||
var linksIDCheck string
|
||||
err = conn.QueryRow(`
|
||||
SELECT pg_get_constraintdef(c.oid)
|
||||
FROM pg_constraint c
|
||||
JOIN pg_class t ON t.oid = c.conrelid
|
||||
WHERE t.relname = 'resource_links' AND c.conname = 'resource_links_id_check'`).Scan(&linksIDCheck)
|
||||
if err != nil {
|
||||
t.Fatalf("contrainte CHECK(resource_links.id) manquante: %v", err)
|
||||
}
|
||||
if !strings.Contains(linksIDCheck, "^[0-9a-f]{32}$") {
|
||||
t.Errorf("CHECK resource_links.id attendu 32-hex, obtenu : %s", linksIDCheck)
|
||||
}
|
||||
|
||||
var linksUniq int
|
||||
err = conn.QueryRow(`
|
||||
SELECT COUNT(*) FROM pg_index i
|
||||
JOIN pg_class t ON t.oid = i.indrelid
|
||||
WHERE t.relname = 'resource_links' AND i.indisunique
|
||||
AND ARRAY(SELECT a.attname FROM unnest(i.indkey) WITH ORDINALITY k(attnum, ord)
|
||||
JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = k.attnum
|
||||
ORDER BY k.ord)::text[] = ARRAY['from_id','to_id','link_type']`).Scan(&linksUniq)
|
||||
if err != nil {
|
||||
t.Fatalf("links unique: %v", err)
|
||||
}
|
||||
if linksUniq == 0 {
|
||||
t.Error("contrainte UNIQUE(from_id, to_id, link_type) manquante sur resource_links après 000021")
|
||||
}
|
||||
|
||||
var scanSessionCol int
|
||||
err = conn.QueryRow(`
|
||||
SELECT COUNT(*) FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'resources' AND column_name = 'scan_session_id'`).Scan(&scanSessionCol)
|
||||
if err != nil {
|
||||
t.Fatalf("scan_session_id column: %v", err)
|
||||
}
|
||||
if scanSessionCol == 0 {
|
||||
t.Error("colonne resources.scan_session_id manquante après 000021")
|
||||
}
|
||||
|
||||
// --- 000022 : révocation tokens ------------------------------------------
|
||||
var tvCol int
|
||||
err = conn.QueryRow(`
|
||||
SELECT COUNT(*) FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'users' AND column_name = 'token_version'`).Scan(&tvCol)
|
||||
if err != nil {
|
||||
t.Fatalf("token_version column: %v", err)
|
||||
}
|
||||
if tvCol == 0 {
|
||||
t.Error("colonne users.token_version manquante après 000022")
|
||||
}
|
||||
|
||||
// --- 000023 : quota (colonne héritée de 000001, IF NOT EXISTS) ------------
|
||||
var quotaCol int
|
||||
err = conn.QueryRow(`
|
||||
SELECT COUNT(*) FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'users' AND column_name = 'storage_quota_bytes'`).Scan("aCol)
|
||||
if err != nil {
|
||||
t.Fatalf("storage_quota_bytes column: %v", err)
|
||||
}
|
||||
if quotaCol == 0 {
|
||||
t.Error("colonne users.storage_quota_bytes manquante après 000023")
|
||||
}
|
||||
|
||||
// --- 000024 : vignettes serveur ------------------------------------------
|
||||
assertTables(t, conn, "users", "devices", "resources", "operations", "ocr_jobs", "shares", "share_links", "storage_locations", "resource_placements", "groups", "group_members", "resource_versions", "resource_facts", "resource_links", "resource_previews", "schema_migrations")
|
||||
|
||||
if err := MigrateDownDatabase(url); err != nil {
|
||||
t.Fatalf("migrate down: %v", err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"os"
|
||||
)
|
||||
|
||||
// TestDatabaseURL resolve l'URL que chaque package de test doit utiliser pour
|
||||
// joindre SA base dédiée.
|
||||
//
|
||||
// Chaque package possède une URL par défaut portant un nom de base distinct
|
||||
// (kazier_repository_test, kazier_handlers_test, kazier_migrations_test…), de
|
||||
// sorte que `go test ./...` puisse lancer les binaires de test en parallèle
|
||||
// sans qu'ils ne s'écrasent le schéma. Quand TEST_DATABASE_URL est posée (CI),
|
||||
// elle joue le rôle d'override d'endpoint : scheme, hôte, identifiants et
|
||||
// paramètres de connexion gagnent, mais le nom de base du package est
|
||||
// conservé. Sans variable d'env, l'URL par défaut du package est retournée
|
||||
// telle quelle.
|
||||
func TestDatabaseURL(packageURL string) string {
|
||||
env := os.Getenv("TEST_DATABASE_URL")
|
||||
if env == "" {
|
||||
return packageURL
|
||||
}
|
||||
base, err := url.Parse(env)
|
||||
if err != nil || base.Scheme == "" || base.Host == "" {
|
||||
return packageURL
|
||||
}
|
||||
pkg, err := url.Parse(packageURL)
|
||||
if err != nil || pkg.Path == "" || pkg.Path == "/" {
|
||||
return env
|
||||
}
|
||||
merged := *base
|
||||
merged.Path = pkg.Path
|
||||
return merged.String()
|
||||
}
|
||||
@@ -4,31 +4,27 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/lib/pq"
|
||||
|
||||
"github.com/vaultdrop/backend/db"
|
||||
"github.com/kazier/backend/db"
|
||||
)
|
||||
|
||||
// OpenTestDatabase ensures the test database exists, resets its schema, runs
|
||||
// all migrations, and returns a live connection (closed via t.Cleanup).
|
||||
// Tests are skipped only when Postgres itself is unreachable. `databaseURL`
|
||||
// empty falls back to TEST_DATABASE_URL, then to the global default.
|
||||
// empty falls back to the global default. TEST_DATABASE_URL (CI) n'override
|
||||
// que l'endpoint (hôte/creds) : le nom de base propre au package est
|
||||
// préservé, pour que les packages parallèles ne partagent jamais un schéma.
|
||||
func OpenTestDatabase(t *testing.T, databaseURL string) *sql.DB {
|
||||
t.Helper()
|
||||
|
||||
switch {
|
||||
case databaseURL == "":
|
||||
databaseURL = os.Getenv("TEST_DATABASE_URL")
|
||||
case os.Getenv("TEST_DATABASE_URL") != "":
|
||||
databaseURL = os.Getenv("TEST_DATABASE_URL")
|
||||
}
|
||||
if databaseURL == "" {
|
||||
databaseURL = "postgres://vaultdrop:vaultdrop@localhost:5432/vaultdrop_test?sslmode=disable"
|
||||
databaseURL = "postgres://kazier:kazier@localhost:5432/kazier_test?sslmode=disable"
|
||||
}
|
||||
databaseURL = db.TestDatabaseURL(databaseURL)
|
||||
|
||||
// Postgres joignable ? (sinon on skippe, sans être gênés par l'existence
|
||||
// ou non de la base cible)
|
||||
|
||||
@@ -0,0 +1,408 @@
|
||||
// Package extract derives generic, verifiable facts from OCR text (V3 S2).
|
||||
//
|
||||
// Rules-based French heuristics, zero ML : every fact carries a confidence,
|
||||
// low-confidence facts are suggested (greyed in UI) and user-correctable (S3),
|
||||
// and search demotes but never excludes on them. No LLM, no network, pure
|
||||
// stdlib — it runs inside the OCR worker right after Tesseract.
|
||||
package extract
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// Fact types — mirror of the resource_facts CHECK (migration 000019).
|
||||
type FactType string
|
||||
|
||||
const (
|
||||
FactDate FactType = "date"
|
||||
FactDueDate FactType = "due_date"
|
||||
FactPeriod FactType = "period"
|
||||
FactAmount FactType = "amount"
|
||||
FactIssuer FactType = "issuer"
|
||||
FactReference FactType = "reference"
|
||||
FactDocType FactType = "doc_type"
|
||||
)
|
||||
|
||||
// Fact is one extracted statement. ValueText holds issuer names, reference
|
||||
// codes ("facture:FA-2026-0031"), doc types and periods ("2026-03") ;
|
||||
// ValueNum holds amounts (HasNum) ; ValueDate holds ISO dates ("2026-03-12").
|
||||
type Fact struct {
|
||||
Type FactType
|
||||
ValueText string
|
||||
ValueNum float64
|
||||
HasNum bool
|
||||
ValueDate string
|
||||
Confidence float64
|
||||
}
|
||||
|
||||
// MaxFacts caps the output per resource (OCR garbage must stay bounded).
|
||||
const MaxFacts = 50
|
||||
|
||||
// FromText extracts generic facts from raw OCR text. Deterministic output
|
||||
// order (document order within each type, types in fixed order), duplicates
|
||||
// removed.
|
||||
func FromText(text string) []Fact {
|
||||
e := &extractor{seen: map[string]bool{}}
|
||||
e.extract(collectLines(text))
|
||||
out := e.facts
|
||||
if len(out) > MaxFacts {
|
||||
out = out[:MaxFacts]
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
type extractor struct {
|
||||
facts []Fact
|
||||
seen map[string]bool
|
||||
}
|
||||
|
||||
func (e *extractor) add(f Fact) {
|
||||
if f.Confidence < 0 {
|
||||
f.Confidence = 0
|
||||
}
|
||||
if f.Confidence > 1 {
|
||||
f.Confidence = 1
|
||||
}
|
||||
key := string(f.Type) + "\x00" + f.ValueText + "\x00" + f.ValueDate + "\x00" + fmt.Sprintf("%.2f", f.ValueNum)
|
||||
if e.seen[key] {
|
||||
return
|
||||
}
|
||||
e.seen[key] = true
|
||||
e.facts = append(e.facts, f)
|
||||
}
|
||||
|
||||
type line struct {
|
||||
raw string
|
||||
folded string
|
||||
}
|
||||
|
||||
func collectLines(text string) []line {
|
||||
raw := strings.Split(text, "\n")
|
||||
out := make([]line, 0, len(raw))
|
||||
for _, r := range raw {
|
||||
r = strings.TrimSpace(strings.ReplaceAll(r, "\t", " "))
|
||||
if r == "" {
|
||||
continue
|
||||
}
|
||||
out = append(out, line{raw: r, folded: fold(r)})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (e *extractor) extract(lines []line) {
|
||||
for _, l := range lines {
|
||||
e.extractAmounts(l)
|
||||
e.extractDates(l)
|
||||
e.extractReferences(l)
|
||||
}
|
||||
e.extractIssuers(lines)
|
||||
e.extractDocType(lines)
|
||||
}
|
||||
|
||||
// --- amounts ---------------------------------------------------------------
|
||||
|
||||
// Note : pas de `\b` après `€` — c'est un symbole (non-mot), donc il n'y a
|
||||
// jamais de frontière avec un espace ou une fin de ligne. `eur` garde son
|
||||
// `\b` (évite "europe"), `euros?` n'en a pas besoin.
|
||||
var amountPattern = regexp.MustCompile(`(\d[\d\s\x{a0}\x{202f}]*[,\.]\d{2}|\d[\d\s\x{a0}\x{202f}]*)\s*(€|euros?|eur\b)`)
|
||||
|
||||
func (e *extractor) extractAmounts(l line) {
|
||||
// Percentages are not amounts.
|
||||
if strings.Contains(l.raw, "%") && !strings.ContainsAny(l.raw, "€") {
|
||||
return
|
||||
}
|
||||
for _, m := range amountPattern.FindAllStringSubmatch(l.raw, -1) {
|
||||
num, ok := parseFrenchNumber(m[1])
|
||||
if !ok || num <= 0 || num >= 1e13 {
|
||||
continue
|
||||
}
|
||||
conf := 0.7
|
||||
if amountKeywordPattern.MatchString(l.folded) {
|
||||
conf = 0.9
|
||||
}
|
||||
e.add(Fact{Type: FactAmount, ValueNum: num, HasNum: true, ValueText: formatAmount(num), Confidence: conf})
|
||||
}
|
||||
}
|
||||
|
||||
var amountKeywordPattern = regexp.MustCompile(`\b(total|montant|a payer|net|ttc|solde|du)\b`)
|
||||
|
||||
func formatAmount(num float64) string {
|
||||
return strconv.FormatFloat(num, 'f', 2, 64) + " €"
|
||||
}
|
||||
|
||||
// parseFrenchNumber reads "1 234,56" / "1234.56" / "84 €"-style magnitudes.
|
||||
// Comma = decimal separator (French), dots/spaces (incl. NBSP U+00A0 and
|
||||
// narrow NBSP U+202F, common in "1 234 €") = thousands.
|
||||
func parseFrenchNumber(s string) (float64, bool) {
|
||||
s = strings.ReplaceAll(s, "\u00a0", "")
|
||||
s = strings.ReplaceAll(s, "\u202f", "")
|
||||
s = strings.ReplaceAll(s, " ", "")
|
||||
if strings.Contains(s, ",") {
|
||||
s = strings.ReplaceAll(s, ".", "")
|
||||
s = strings.ReplaceAll(s, ",", ".")
|
||||
}
|
||||
num, err := strconv.ParseFloat(s, 64)
|
||||
return num, err == nil
|
||||
}
|
||||
|
||||
// --- dates -----------------------------------------------------------------
|
||||
|
||||
var numericDatePattern = regexp.MustCompile(`\b(\d{1,2})\s*[/\-.]\s*(\d{1,2})\s*[/\-.]\s*(\d{2}|\d{4})\b`)
|
||||
|
||||
var writtenDatePattern = regexp.MustCompile(`\b(\d{1,2})\s+(janv(?:ier)?|fev(?:rier)?|mars|avr(?:il)?|mai|juin|juil(?:let)?|aout|sept?(?:embre)?|oct(?:obre)?|nov(?:embre)?|dec(?:embre)?)\s+(\d{4})\b`)
|
||||
|
||||
var periodPattern = regexp.MustCompile(`\b(janv(?:ier)?|fev(?:rier)?|mars|avr(?:il)?|mai|juin|juil(?:let)?|aout|sept?(?:embre)?|oct(?:obre)?|nov(?:embre)?|dec(?:embre)?)\s+(\d{4})\b`)
|
||||
|
||||
var monthNumbers = map[string]int{
|
||||
"janv": 1, "janvier": 1, "fev": 2, "fevrier": 2, "mars": 3,
|
||||
"avr": 4, "avril": 4, "mai": 5, "juin": 6, "juil": 7,
|
||||
"juillet": 7, "aout": 8, "sept": 9, "septembre": 9, "oct": 10,
|
||||
"octobre": 10, "nov": 11, "novembre": 11, "dec": 12, "decembre": 12,
|
||||
}
|
||||
|
||||
var dueKeywordPattern = regexp.MustCompile(`\b(echeance|a payer avant|date limite|avant le|regler avant|paiement avant)\b`)
|
||||
|
||||
func fullYear(yy int) int {
|
||||
if yy < 100 {
|
||||
if yy >= 70 {
|
||||
return 1900 + yy
|
||||
}
|
||||
return 2000 + yy
|
||||
}
|
||||
return yy
|
||||
}
|
||||
|
||||
func isoDate(y, m, d int) (string, bool) {
|
||||
if m < 1 || m > 12 || d < 1 || d > 31 || y < 1900 || y > 2100 {
|
||||
return "", false
|
||||
}
|
||||
return fmt.Sprintf("%04d-%02d-%02d", y, m, d), true
|
||||
}
|
||||
|
||||
func (e *extractor) extractDates(l line) {
|
||||
isDue := dueKeywordPattern.MatchString(l.folded)
|
||||
for _, m := range numericDatePattern.FindAllStringSubmatch(l.raw, -1) {
|
||||
d, _ := strconv.Atoi(m[1])
|
||||
mo, _ := strconv.Atoi(m[2])
|
||||
y, _ := strconv.Atoi(m[3])
|
||||
iso, ok := isoDate(fullYear(y), mo, d)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
typ := FactDate
|
||||
conf := 0.6
|
||||
if isDue {
|
||||
typ, conf = FactDueDate, 0.8
|
||||
}
|
||||
e.add(Fact{Type: typ, ValueDate: iso, ValueText: iso, Confidence: conf})
|
||||
}
|
||||
for _, m := range writtenDatePattern.FindAllStringSubmatch(l.folded, -1) {
|
||||
d, _ := strconv.Atoi(m[1])
|
||||
mo := monthNumbers[m[2]]
|
||||
y, _ := strconv.Atoi(m[3])
|
||||
iso, ok := isoDate(y, mo, d)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
typ := FactDate
|
||||
conf := 0.7
|
||||
if isDue {
|
||||
typ, conf = FactDueDate, 0.85
|
||||
}
|
||||
e.add(Fact{Type: typ, ValueDate: iso, ValueText: iso, Confidence: conf})
|
||||
}
|
||||
for _, m := range periodPattern.FindAllStringSubmatch(l.folded, -1) {
|
||||
mo := monthNumbers[m[1]]
|
||||
y, _ := strconv.Atoi(m[2])
|
||||
if mo == 0 || y < 1900 || y > 2100 {
|
||||
continue
|
||||
}
|
||||
e.add(Fact{Type: FactPeriod, ValueText: fmt.Sprintf("%04d-%02d", y, mo), Confidence: 0.7})
|
||||
}
|
||||
}
|
||||
|
||||
// --- references ------------------------------------------------------------
|
||||
|
||||
// referencePattern catches "facture n° FA-2026-0031", "contrat : ABC123",
|
||||
// "client 42" — the code must contain a digit (avoids "FACTURE DU MOIS").
|
||||
var referencePattern = regexp.MustCompile(`(?i)\b(facture|contrat|client|compte|dossier|bulletin|commande|devis|attestation|police)\s*(n°|no|num(?:ero)?|number|ref(?:erence)?)?\s*[:\-#]?\s*([A-Z0-9][A-Z0-9\-/\.]{1,29})`)
|
||||
|
||||
var bareRefPattern = regexp.MustCompile(`\bn°\s*([A-Z0-9][A-Z0-9\-/]{2,20})`)
|
||||
|
||||
var siretPattern = regexp.MustCompile(`(?i)\b(siret|siren|rcs)\b[^\d]{0,10}(\d{3}\s?\d{3}\s?\d{3}(?:\s?\d{5})?)`)
|
||||
|
||||
var ibanPattern = regexp.MustCompile(`\bFR\s?\d{2}(?:\s?[0-9A-Z]{4}){5}\s?[0-9A-Z]{0,3}\b`)
|
||||
|
||||
func hasDigit(s string) bool {
|
||||
for _, r := range s {
|
||||
if r >= '0' && r <= '9' {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (e *extractor) extractReferences(l line) {
|
||||
for _, m := range referencePattern.FindAllStringSubmatch(l.raw, -1) {
|
||||
code := strings.Trim(m[3], "-/.")
|
||||
if !hasDigit(code) || len(code) < 3 {
|
||||
continue
|
||||
}
|
||||
kind := strings.ToLower(m[1])
|
||||
e.add(Fact{Type: FactReference, ValueText: kind + ":" + code, Confidence: 0.75})
|
||||
}
|
||||
for _, m := range bareRefPattern.FindAllStringSubmatch(l.raw, -1) {
|
||||
code := m[1]
|
||||
if !hasDigit(code) {
|
||||
continue
|
||||
}
|
||||
e.add(Fact{Type: FactReference, ValueText: "ref:" + code, Confidence: 0.6})
|
||||
}
|
||||
if m := siretPattern.FindStringSubmatch(l.raw); m != nil {
|
||||
digits := strings.Join(strings.Fields(m[2]), " ")
|
||||
e.add(Fact{Type: FactReference, ValueText: strings.ToLower(m[1]) + ":" + digits, Confidence: 0.9})
|
||||
}
|
||||
if m := ibanPattern.FindString(l.raw); m != "" {
|
||||
compact := strings.ReplaceAll(m, " ", "")
|
||||
if len(compact) == 27 {
|
||||
// Store masked (privacy) : only the last 4 chars.
|
||||
e.add(Fact{Type: FactReference, ValueText: "rib:••••" + compact[len(compact)-4:], Confidence: 0.9})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- issuers ---------------------------------------------------------------
|
||||
|
||||
var companySuffixPattern = regexp.MustCompile(`(?i)\b(sarl|sas|sa|eurl|sasu|sci|eirl|snc|sasau)\b`)
|
||||
|
||||
func isCapsLine(s string) bool {
|
||||
letters := 0
|
||||
for _, r := range s {
|
||||
if unicode.IsLetter(r) {
|
||||
letters++
|
||||
if unicode.IsLower(r) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return letters >= 3
|
||||
}
|
||||
|
||||
func (e *extractor) extractIssuers(lines []line) {
|
||||
found := 0
|
||||
checked := 0
|
||||
for _, l := range lines {
|
||||
if checked >= 8 || found >= 2 {
|
||||
break
|
||||
}
|
||||
raw := strings.TrimSpace(l.raw)
|
||||
if len([]rune(raw)) < 3 || len([]rune(raw)) > 60 {
|
||||
continue
|
||||
}
|
||||
if strings.Contains(raw, "@") || strings.HasPrefix(strings.ToLower(raw), "http") {
|
||||
continue
|
||||
}
|
||||
checked++
|
||||
if !isCapsLine(raw) {
|
||||
continue
|
||||
}
|
||||
// Skip lines that are really dates/amounts/references in disguise.
|
||||
if numericDatePattern.MatchString(raw) || amountPattern.MatchString(raw) || bareRefPattern.MatchString(raw) {
|
||||
continue
|
||||
}
|
||||
conf := 0.45
|
||||
if found == 0 {
|
||||
conf = 0.55
|
||||
}
|
||||
if companySuffixPattern.MatchString(raw) {
|
||||
conf = 0.75
|
||||
}
|
||||
e.add(Fact{Type: FactIssuer, ValueText: raw, Confidence: conf})
|
||||
found++
|
||||
}
|
||||
}
|
||||
|
||||
// --- doc_type suggestion ---------------------------------------------------
|
||||
|
||||
// docTypeKeywords maps a suggested type to weighted French keywords (folded).
|
||||
// A type is emitted when its score reaches docTypeThreshold — the type is a
|
||||
// humble suggestion (S3-correctable), never a gate.
|
||||
var docTypeKeywords = []struct {
|
||||
typ string
|
||||
keywords map[string]int
|
||||
}{
|
||||
{"facture", map[string]int{"facture": 2, "tva": 1, "montant ttc": 2, "total ttc": 2, "a payer": 1, "devis": 1}},
|
||||
{"bulletin", map[string]int{"bulletin": 2, "salaire": 2, "paie": 1, "net social": 2, "brut": 1}},
|
||||
{"impot", map[string]int{"impot": 2, "revenu fiscal": 2, "taxe fonciere": 2, "taxe d habitation": 1, "avis d impot": 2}},
|
||||
{"contrat", map[string]int{"contrat": 2, "bail": 2, "convention": 1, "article ": 1, "assurance": 1}},
|
||||
{"ordonnance", map[string]int{"ordonnance": 2, "posologie": 2, "docteur": 1, "traitement": 1}},
|
||||
{"attestation", map[string]int{"attestation": 2, "certifie": 1, "certificat": 1, "atteste": 1}},
|
||||
{"rib", map[string]int{"rib": 2, "iban": 2, "bic": 1}},
|
||||
{"identite", map[string]int{"carte d identite": 2, "passeport": 2, "permis de conduire": 2, "nationalite": 1}},
|
||||
}
|
||||
|
||||
const docTypeThreshold = 2
|
||||
|
||||
func (e *extractor) extractDocType(lines []line) {
|
||||
joined := "\n"
|
||||
for _, l := range lines {
|
||||
joined += l.folded + "\n"
|
||||
}
|
||||
best, bestScore := "", 0
|
||||
for _, dt := range docTypeKeywords {
|
||||
score := 0
|
||||
for kw, w := range dt.keywords {
|
||||
if strings.Contains(joined, kw) {
|
||||
score += w
|
||||
}
|
||||
}
|
||||
if score > bestScore {
|
||||
best, bestScore = dt.typ, score
|
||||
}
|
||||
}
|
||||
if bestScore >= docTypeThreshold {
|
||||
conf := 0.5 + 0.1*float64(bestScore)
|
||||
if conf > 0.85 {
|
||||
conf = 0.85
|
||||
}
|
||||
e.add(Fact{Type: FactDocType, ValueText: best, Confidence: conf})
|
||||
}
|
||||
}
|
||||
|
||||
// --- folding ---------------------------------------------------------------
|
||||
|
||||
// fold lowercases French text and strips accents (œ→oe, æ→ae, ç→c) for
|
||||
// keyword matching. stdlib-only (no x/text dependency churn).
|
||||
func fold(s string) string {
|
||||
var b strings.Builder
|
||||
b.Grow(len(s))
|
||||
for _, r := range strings.ToLower(s) {
|
||||
switch r {
|
||||
case 'à', 'â', 'ä':
|
||||
b.WriteRune('a')
|
||||
case 'é', 'è', 'ê', 'ë':
|
||||
b.WriteRune('e')
|
||||
case 'î', 'ï':
|
||||
b.WriteRune('i')
|
||||
case 'ô', 'ö':
|
||||
b.WriteRune('o')
|
||||
case 'ù', 'û', 'ü':
|
||||
b.WriteRune('u')
|
||||
case 'ÿ':
|
||||
b.WriteRune('y')
|
||||
case 'ç':
|
||||
b.WriteRune('c')
|
||||
case 'œ':
|
||||
b.WriteString("oe")
|
||||
case 'æ':
|
||||
b.WriteString("ae")
|
||||
default:
|
||||
b.WriteRune(r)
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
package extract
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func factsByType(facts []Fact) map[FactType][]Fact {
|
||||
out := map[FactType][]Fact{}
|
||||
for _, f := range facts {
|
||||
out[f.Type] = append(out[f.Type], f)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func hasText(facts []Fact, want string) bool {
|
||||
for _, f := range facts {
|
||||
if f.ValueText == want {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
const edfBill = `EDF - SERVICE CLIENTS
|
||||
TSA 12345
|
||||
Facture n° FA-2026-0031 du 12/03/2026
|
||||
Période : mars 2026
|
||||
Montant total TTC : 1 234,56 €
|
||||
À payer avant le 12/04/2026
|
||||
Référence client : 123456789
|
||||
`
|
||||
|
||||
func TestFactureEDF(t *testing.T) {
|
||||
by := factsByType(FromText(edfBill))
|
||||
|
||||
if !hasText(by[FactReference], "facture:FA-2026-0031") {
|
||||
t.Errorf("référence facture manquante: %+v", by[FactReference])
|
||||
}
|
||||
if !hasText(by[FactDate], "2026-03-12") {
|
||||
t.Errorf("date émission manquante: %+v", by[FactDate])
|
||||
}
|
||||
if !hasText(by[FactDueDate], "2026-04-12") {
|
||||
t.Errorf("échéance manquante: %+v", by[FactDueDate])
|
||||
}
|
||||
if !hasText(by[FactPeriod], "2026-03") {
|
||||
t.Errorf("période manquante: %+v", by[FactPeriod])
|
||||
}
|
||||
found := false
|
||||
for _, f := range by[FactAmount] {
|
||||
if f.HasNum && f.ValueNum == 1234.56 && f.Confidence >= 0.9 {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Errorf("montant 1234.56 (conf 0.9) manquant: %+v", by[FactAmount])
|
||||
}
|
||||
if !hasText(by[FactIssuer], "EDF - SERVICE CLIENTS") {
|
||||
t.Errorf("émetteur manquant: %+v", by[FactIssuer])
|
||||
}
|
||||
if !hasText(by[FactDocType], "facture") {
|
||||
t.Errorf("doc_type facture manquant: %+v", by[FactDocType])
|
||||
}
|
||||
}
|
||||
|
||||
const bulletin = `BULLETIN DE SALAIRE
|
||||
DUPONT Jean
|
||||
Période : janvier 2026
|
||||
Salaire brut : 2 845,00 €
|
||||
Net social : 2 210,35 €
|
||||
SIRET 123 456 789 00012
|
||||
`
|
||||
|
||||
func TestBulletinSalaire(t *testing.T) {
|
||||
by := factsByType(FromText(bulletin))
|
||||
if !hasText(by[FactDocType], "bulletin") {
|
||||
t.Errorf("doc_type bulletin manquant: %+v", by[FactDocType])
|
||||
}
|
||||
if !hasText(by[FactPeriod], "2026-01") {
|
||||
t.Errorf("période manquante: %+v", by[FactPeriod])
|
||||
}
|
||||
if !hasText(by[FactReference], "siret:123 456 789 00012") {
|
||||
t.Errorf("siret manquant: %+v", by[FactReference])
|
||||
}
|
||||
amounts := map[float64]bool{}
|
||||
for _, f := range by[FactAmount] {
|
||||
if f.HasNum {
|
||||
amounts[f.ValueNum] = true
|
||||
}
|
||||
}
|
||||
if !amounts[2845.00] || !amounts[2210.35] {
|
||||
t.Errorf("montants bulletin manquants: %+v", by[FactAmount])
|
||||
}
|
||||
}
|
||||
|
||||
const ordonnance = `Dr MARTIN Sophie
|
||||
Ordonnance du 3 mars 2026
|
||||
Doliprane 1000mg — 1 matin et soir pendant 7 jours
|
||||
`
|
||||
|
||||
func TestOrdonnance(t *testing.T) {
|
||||
by := factsByType(FromText(ordonnance))
|
||||
if !hasText(by[FactDocType], "ordonnance") {
|
||||
t.Errorf("doc_type ordonnance manquant: %+v", by[FactDocType])
|
||||
}
|
||||
if !hasText(by[FactDate], "2026-03-03") {
|
||||
t.Errorf("date manquante: %+v", by[FactDate])
|
||||
}
|
||||
if len(by[FactAmount]) != 0 {
|
||||
t.Errorf("1000mg ne doit pas être un montant: %+v", by[FactAmount])
|
||||
}
|
||||
}
|
||||
|
||||
const rib = `RELEVÉ D'IDENTITÉ BANCAIRE
|
||||
Titulaire : DUPONT Jean
|
||||
IBAN : FR76 1234 5678 9012 3456 7890 123
|
||||
BIC : AGRIFRPP
|
||||
`
|
||||
|
||||
func TestRIBMasque(t *testing.T) {
|
||||
by := factsByType(FromText(rib))
|
||||
if !hasText(by[FactDocType], "rib") {
|
||||
t.Errorf("doc_type rib manquant: %+v", by[FactDocType])
|
||||
}
|
||||
if !hasText(by[FactReference], "rib:••••0123") {
|
||||
t.Errorf("IBAN masqué manquant: %+v", by[FactReference])
|
||||
}
|
||||
// L'IBAN complet ne doit jamais fuiter dans les faits.
|
||||
for _, f := range by[FactReference] {
|
||||
if len(f.ValueText) > 20 {
|
||||
t.Errorf("fuite IBAN en clair: %q", f.ValueText)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTexteVideEtBruit(t *testing.T) {
|
||||
if got := FromText(""); len(got) != 0 {
|
||||
t.Errorf("vide → %d faits, attendu 0", len(got))
|
||||
}
|
||||
// Bruit OCR sans structure : pas de doc_type, pas de montant.
|
||||
by := factsByType(FromText("lorem ipsum\nbla bla 99\n///"))
|
||||
if len(by[FactDocType]) != 0 {
|
||||
t.Errorf("doc_type sur bruit: %+v", by[FactDocType])
|
||||
}
|
||||
if len(by[FactAmount]) != 0 {
|
||||
t.Errorf("montant sur bruit: %+v", by[FactAmount])
|
||||
}
|
||||
}
|
||||
|
||||
func TestCourrierSimpleSansType(t *testing.T) {
|
||||
// Tous les documents comptent : un courrier sans mots-clés métier donne
|
||||
// des faits (date) mais aucun doc_type — jamais pénalisé.
|
||||
by := factsByType(FromText("Bonjour,\nmerci de me recontacter.\nCordialement.\nParis, le 05/06/2026\n"))
|
||||
if !hasText(by[FactDate], "2026-06-05") {
|
||||
t.Errorf("date courrier manquante: %+v", by[FactDate])
|
||||
}
|
||||
if len(by[FactDocType]) != 0 {
|
||||
t.Errorf("pas de doc_type attendu: %+v", by[FactDocType])
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnneesCourtesEtFormats(t *testing.T) {
|
||||
by := factsByType(FromText("Fait le 01.02.99\nÉchéance 15-08-26\n"))
|
||||
if !hasText(by[FactDate], "1999-02-01") {
|
||||
t.Errorf("année courte 99 → 1999: %+v", by[FactDate])
|
||||
}
|
||||
if !hasText(by[FactDueDate], "2026-08-15") {
|
||||
t.Errorf("année courte 26 → 2026: %+v", by[FactDueDate])
|
||||
}
|
||||
}
|
||||
|
||||
func TestBornes(t *testing.T) {
|
||||
// Dates invalides ignorées, pas de crash.
|
||||
by := factsByType(FromText("le 45/13/2026 et le 00/00/0000"))
|
||||
if len(by[FactDate])+len(by[FactDueDate]) != 0 {
|
||||
t.Errorf("dates invalides extraites: %+v", by)
|
||||
}
|
||||
// Confiances toujours dans [0,1].
|
||||
for _, f := range FromText(edfBill + bulletin + ordonnance + rib) {
|
||||
if f.Confidence < 0 || f.Confidence > 1 {
|
||||
t.Errorf("confiance hors bornes: %+v", f)
|
||||
}
|
||||
}
|
||||
}
|
||||
+7
-5
@@ -1,4 +1,4 @@
|
||||
module github.com/vaultdrop/backend
|
||||
module github.com/kazier/backend
|
||||
|
||||
go 1.26.4
|
||||
|
||||
@@ -7,11 +7,15 @@ require (
|
||||
github.com/gin-gonic/gin v1.12.0
|
||||
github.com/golang-migrate/migrate/v4 v4.19.1
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/ledongthuc/pdf v0.0.0-20260907135840-6c8c28e0e8a0
|
||||
github.com/lib/pq v1.12.3
|
||||
golang.org/x/crypto v0.52.0
|
||||
golang.org/x/image v0.46.0
|
||||
)
|
||||
|
||||
require (
|
||||
aidanwoods.dev/go-result v0.3.1 // indirect
|
||||
github.com/alexmullins/zip v0.0.0-20180717182244-4affb64b04d0 // indirect
|
||||
github.com/bytedance/gopkg v0.1.3 // indirect
|
||||
github.com/bytedance/sonic v1.15.0 // indirect
|
||||
github.com/bytedance/sonic/loader v0.5.0 // indirect
|
||||
@@ -25,7 +29,6 @@ require (
|
||||
github.com/goccy/go-yaml v1.19.2 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
|
||||
github.com/ledongthuc/pdf v0.0.0-20260907135840-6c8c28e0e8a0 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
@@ -37,9 +40,8 @@ require (
|
||||
github.com/ugorji/go/codec v1.3.1 // indirect
|
||||
go.mongodb.org/mongo-driver/v2 v2.5.0 // indirect
|
||||
golang.org/x/arch v0.22.0 // indirect
|
||||
golang.org/x/crypto v0.52.0 // indirect
|
||||
golang.org/x/net v0.54.0 // indirect
|
||||
golang.org/x/sys v0.45.0 // indirect
|
||||
golang.org/x/text v0.37.0 // indirect
|
||||
golang.org/x/sys v0.48.0 // indirect
|
||||
golang.org/x/text v0.42.0 // indirect
|
||||
google.golang.org/protobuf v1.36.10 // indirect
|
||||
)
|
||||
|
||||
+8
-4
@@ -6,6 +6,8 @@ github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25
|
||||
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
|
||||
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
|
||||
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
|
||||
github.com/alexmullins/zip v0.0.0-20180717182244-4affb64b04d0 h1:BVts5dexXf4i+JX8tXlKT0aKoi38JwTXSe+3WUneX0k=
|
||||
github.com/alexmullins/zip v0.0.0-20180717182244-4affb64b04d0/go.mod h1:FDIQmoMNJJl5/k7upZEnGvgWVZfFeE6qHeN7iCMbCsA=
|
||||
github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M=
|
||||
github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM=
|
||||
github.com/bytedance/sonic v1.15.0 h1:/PXeWFaR5ElNcVE84U0dOHjiMHQOwNIx3K4ymzh/uSE=
|
||||
@@ -136,13 +138,15 @@ golang.org/x/arch v0.22.0 h1:c/Zle32i5ttqRXjdLyyHZESLD/bB90DCU1g9l/0YBDI=
|
||||
golang.org/x/arch v0.22.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A=
|
||||
golang.org/x/crypto v0.52.0 h1:RMs7fP2rXdep0CftQlK8Uf+kibLm7qkCcradZWYz988=
|
||||
golang.org/x/crypto v0.52.0/go.mod h1:1QgfPxDqh0T2M/elOJtp9RvuR95kVjir0e6/BvEmGbc=
|
||||
golang.org/x/image v0.46.0 h1:b1+oYj0Jbp6K5MDT4i4/eZpYlk3V8SJhhDKh6LBHAyQ=
|
||||
golang.org/x/image v0.46.0/go.mod h1:3B3W05VGVQyuXucLINLjXKrqISASfi4Xj+iCVkLMwew=
|
||||
golang.org/x/net v0.54.0 h1:2zJIZAxAHV/OHCDTCOHAYehQzLfSXuf/5SoL/Dv6w/w=
|
||||
golang.org/x/net v0.54.0/go.mod h1:Sj4oj8jK6XmHpBZU/zWHw3BV3abl4Kvi+Ut7cQcY+cQ=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.45.0 h1:dO4czNzziLiiXplLQgBCEpCvXQ3dnkn0SdaZSYdQ+FY=
|
||||
golang.org/x/sys v0.45.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
golang.org/x/text v0.37.0 h1:Cqjiwd9eSg8e0QAkyCaQTNHFIIzWtidPahFWR83rTrc=
|
||||
golang.org/x/text v0.37.0/go.mod h1:a5sjxXGs9hsn/AJVwuElvCAo9v8QYLzvavO5z2PiM38=
|
||||
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
|
||||
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||
golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI=
|
||||
golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E=
|
||||
google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE=
|
||||
google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
|
||||
+92
-14
@@ -2,17 +2,18 @@ package handlers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/vaultdrop/backend/pkg/api"
|
||||
"github.com/vaultdrop/backend/pkg/auth"
|
||||
"github.com/vaultdrop/backend/pkg/passwd"
|
||||
"github.com/vaultdrop/backend/repository"
|
||||
"github.com/vaultdrop/backend/service"
|
||||
"github.com/kazier/backend/pkg/api"
|
||||
"github.com/kazier/backend/pkg/passwd"
|
||||
"github.com/kazier/backend/repository"
|
||||
"github.com/kazier/backend/service"
|
||||
)
|
||||
|
||||
type LoginRequest struct {
|
||||
@@ -33,9 +34,55 @@ type LoginResponse struct {
|
||||
User LoginUserDTO `json:"user"`
|
||||
}
|
||||
|
||||
// Anti-bruteforce du login (FEEDBACK #12) : limiteur basique en mémoire,
|
||||
// N tentatives / minute / IP sur POST /auth/login, placé AVANT la vérification
|
||||
// argon2 (coûteuse) pour protéger le CPU. Au-delà → 429 RATE_LIMITED.
|
||||
// TODO(suivi) : rate-limit distribué (ex. Redis) + métriques pour les déploiements
|
||||
// multi-réplicas, et budget global coordonné avec le rate-limit POST /devices
|
||||
// (LOT 6) contre le remplissage de table.
|
||||
var (
|
||||
loginRateLimitPerMinute = 60
|
||||
loginRateWindow = time.Minute
|
||||
loginAttemptsMu sync.Mutex
|
||||
loginAttempts = map[string][]time.Time{}
|
||||
)
|
||||
|
||||
// OnLoginRateLimited est un hook d'observabilité appelé quand le limiteur
|
||||
// rejette une tentative (nil par défaut = no-op). Les tests peuvent l'utiliser
|
||||
// pour asserter le déclenchement sans dépendre du timing.
|
||||
var OnLoginRateLimited func(ip string)
|
||||
|
||||
// ResetLoginRateLimit vide le compteur (tests uniquement).
|
||||
func ResetLoginRateLimit() {
|
||||
loginAttemptsMu.Lock()
|
||||
defer loginAttemptsMu.Unlock()
|
||||
loginAttempts = map[string][]time.Time{}
|
||||
}
|
||||
|
||||
// loginRateLimited enregistre une tentative et retourne true si le quota
|
||||
// IP est dépassé (fenêtre glissante).
|
||||
func loginRateLimited(ip string, now time.Time) bool {
|
||||
loginAttemptsMu.Lock()
|
||||
defer loginAttemptsMu.Unlock()
|
||||
cutoff := now.Add(-loginRateWindow)
|
||||
kept := make([]time.Time, 0, len(loginAttempts[ip])+1)
|
||||
for _, t := range loginAttempts[ip] {
|
||||
if t.After(cutoff) {
|
||||
kept = append(kept, t)
|
||||
}
|
||||
}
|
||||
if len(kept) >= loginRateLimitPerMinute {
|
||||
loginAttempts[ip] = kept
|
||||
return true
|
||||
}
|
||||
loginAttempts[ip] = append(kept, now)
|
||||
return false
|
||||
}
|
||||
|
||||
// AuthLogin est la SEULE porte d'émission de token (V1). L'erreur est
|
||||
// volontairement indistinguable entre « username inconnu » et « mauvais mot de
|
||||
// passe » — même code, même message, délai égalisé via passwd.VerifyTimedEqual.
|
||||
// volontairement indistinguable entre « username inconnu », « mauvais mot de
|
||||
// passe » et « device non enregistré » — même code 401, même message, délai
|
||||
// égalisé via passwd.VerifyTimedEqual (anti-oracle, FEEDBACK #12).
|
||||
func AuthLogin(c *gin.Context) {
|
||||
if Store == nil || Store.Repository == nil || Auth == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
@@ -51,12 +98,24 @@ func AuthLogin(c *gin.Context) {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "username, password and device_id are required")
|
||||
return
|
||||
}
|
||||
if !deviceIDPattern.MatchString(req.DeviceID) {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_DEVICE_ID", "device_id must be 32 lowercase hex chars")
|
||||
return
|
||||
}
|
||||
|
||||
if loginRateLimited(c.ClientIP(), time.Now()) {
|
||||
if OnLoginRateLimited != nil {
|
||||
OnLoginRateLimited(c.ClientIP())
|
||||
}
|
||||
api.Error(c, http.StatusTooManyRequests, "RATE_LIMITED", "too many login attempts, retry later")
|
||||
return
|
||||
}
|
||||
|
||||
normalized := service.NormalizeUsername(req.Username)
|
||||
user, userErr := Store.Repository.Users.GetByUsernameNormalized(normalized)
|
||||
|
||||
// Même durée de traitement que le compte existe ou non (username inconnu =>
|
||||
// stored vide → vérification contre dummyHash).
|
||||
// stored vide → vérification contre dummyHash aléatoire par boot).
|
||||
stored := ""
|
||||
if userErr == nil && user != nil {
|
||||
stored = user.PasswordHash
|
||||
@@ -69,6 +128,12 @@ func AuthLogin(c *gin.Context) {
|
||||
api.Error(c, http.StatusInternalServerError, "INTERNAL", "could not verify credentials")
|
||||
return
|
||||
}
|
||||
// A1 : ne jamais déréférencer user si inconnu — même si Verify (par
|
||||
// extraordinaire) réussissait, on reste en 401.
|
||||
if userErr != nil || user == nil {
|
||||
api.Error(c, http.StatusUnauthorized, "UNAUTHORIZED", "invalid credentials")
|
||||
return
|
||||
}
|
||||
|
||||
exists, err := Store.Repository.Devices.Exists(req.DeviceID)
|
||||
if err != nil {
|
||||
@@ -76,24 +141,31 @@ func AuthLogin(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
if !exists {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_DEVICE_ID", "device not registered")
|
||||
// Volontairement indistinguable d'un mauvais password (anti-oracle).
|
||||
api.Error(c, http.StatusUnauthorized, "UNAUTHORIZED", "invalid credentials")
|
||||
return
|
||||
}
|
||||
|
||||
if err := Store.Repository.Devices.MarkUser(req.DeviceID, user.ID); err != nil {
|
||||
if errors.Is(err, repository.ErrNotFound) {
|
||||
// Course : device supprimé entre Exists et MarkUser → même 401
|
||||
// indistinguable, jamais de token pour un device fantôme.
|
||||
api.Error(c, http.StatusUnauthorized, "UNAUTHORIZED", "invalid credentials")
|
||||
return
|
||||
}
|
||||
api.Error(c, http.StatusInternalServerError, "INTERNAL", "could not update device")
|
||||
return
|
||||
}
|
||||
|
||||
token, err := Auth.Issue(user.ID, req.DeviceID)
|
||||
token, expiresAt, err := Auth.IssueWithExpiry(user.ID, req.DeviceID, user.TokenVersion)
|
||||
if err != nil {
|
||||
api.Error(c, http.StatusInternalServerError, "TOKEN_ERROR", "could not issue token")
|
||||
api.Error(c, http.StatusInternalServerError, "INTERNAL", "could not issue token")
|
||||
return
|
||||
}
|
||||
|
||||
api.OK(c, LoginResponse{
|
||||
Token: token,
|
||||
ExpiresAt: time.Now().Add(auth.TokenTTL).UnixMilli(),
|
||||
ExpiresAt: expiresAt.UnixMilli(),
|
||||
User: LoginUserDTO{
|
||||
ID: user.ID,
|
||||
Username: user.Username,
|
||||
@@ -108,8 +180,9 @@ type changePasswordRequest struct {
|
||||
}
|
||||
|
||||
// ChangePassword met à jour le hash du compte connecté. Le mot de passe
|
||||
// courant est requis. Limite connue V1 : les tokens déjà émis restent valides
|
||||
// jusqu'à expiration (7 j, pas de révocation) — cf. docs/api-v1.md §7.
|
||||
// courant est requis. V3 S6 : le changement bumpe `token_version` — tous les
|
||||
// autres tokens (autres devices, sessions volées) sont révoqués aussitôt
|
||||
// (le token courant aussi : le client doit se reconnecter).
|
||||
func ChangePassword(c *gin.Context) {
|
||||
if Store == nil || Store.Repository == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
@@ -146,6 +219,11 @@ func ChangePassword(c *gin.Context) {
|
||||
api.Error(c, http.StatusInternalServerError, "INTERNAL", "could not update password")
|
||||
return
|
||||
}
|
||||
// Révocation (V3 S6) : bump best-effort — un échec ici ne doit pas faire
|
||||
// croire que le password n'a pas changé (il l'est, ligne ci-dessus).
|
||||
if err := Store.Repository.Users.BumpTokenVersion(userID); err != nil {
|
||||
log.Printf("auth: bump token_version(%s): %v", userID, err)
|
||||
}
|
||||
|
||||
api.OK(c, gin.H{"id": userID})
|
||||
}
|
||||
|
||||
@@ -9,17 +9,17 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/vaultdrop/backend/dbtest"
|
||||
"github.com/vaultdrop/backend/pkg/auth"
|
||||
"github.com/vaultdrop/backend/pkg/passwd"
|
||||
"github.com/vaultdrop/backend/repository"
|
||||
"github.com/vaultdrop/backend/service"
|
||||
"github.com/kazier/backend/dbtest"
|
||||
"github.com/kazier/backend/pkg/auth"
|
||||
"github.com/kazier/backend/pkg/passwd"
|
||||
"github.com/kazier/backend/repository"
|
||||
"github.com/kazier/backend/service"
|
||||
)
|
||||
|
||||
const authTestURL = "postgres://vaultdrop:vaultdrop@localhost:5432/vaultdrop_handlers_auth_test?sslmode=disable"
|
||||
const authTestURL = "postgres://kazier:kazier@localhost:5432/kazier_handlers_auth_test?sslmode=disable"
|
||||
|
||||
const (
|
||||
authTestPassword = "vaultdrop-test-password"
|
||||
authTestPassword = "kazier-test-password"
|
||||
authTestDevice = "0123456789abcdef0123456789abcdef"
|
||||
)
|
||||
|
||||
@@ -42,6 +42,8 @@ func setTestStore(t *testing.T) *repository.Repository {
|
||||
func setTestAuth(t *testing.T) {
|
||||
t.Helper()
|
||||
Auth, _ = auth.NewManager("test-secret")
|
||||
ResetLoginRateLimit()
|
||||
ResetDevicesRateLimit()
|
||||
t.Cleanup(func() { Auth = nil })
|
||||
}
|
||||
|
||||
@@ -202,6 +204,71 @@ func TestAuthLoginWrongCredentials(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestAuthLoginUnknownUserRegisteredDevice — régression A1 : un username
|
||||
// inconnu ne doit JAMAIS déréférencer user (panic → 500 via Gin recovery).
|
||||
// Le device est enregistré pour isoler la branche « user inconnu » : on attend
|
||||
// 401 UNAUTHORIZED, corps identique au cas « mauvais mot de passe ».
|
||||
func TestAuthLoginUnknownUserRegisteredDevice(t *testing.T) {
|
||||
setTestAuth(t)
|
||||
repo := setTestStore(t)
|
||||
createAdmin(t, repo, "admin", authTestPassword)
|
||||
r := newTestRouterForAuth()
|
||||
|
||||
doAuth(t, r, http.MethodPost, "/api/v1/devices", "", `{"deviceId":"`+authTestDevice+`"}`)
|
||||
|
||||
badPassword := doAuth(t, r, http.MethodPost, "/api/v1/auth/login", "", `{"username":"admin","password":"wrong-password","device_id":"`+authTestDevice+`"}`)
|
||||
if badPassword.Code != 401 {
|
||||
t.Fatalf("mauvais mot de passe: status %d body %s", badPassword.Code, badPassword.Body.String())
|
||||
}
|
||||
|
||||
ghost := doAuth(t, r, http.MethodPost, "/api/v1/auth/login", "", `{"username":"ghost","password":"wrong-password","device_id":"`+authTestDevice+`"}`)
|
||||
if ghost.Code != 401 {
|
||||
t.Fatalf("username inconnu: status %d body %s (attendu 401, jamais 500)", ghost.Code, ghost.Body.String())
|
||||
}
|
||||
if !strings.Contains(ghost.Body.String(), "UNAUTHORIZED") {
|
||||
t.Errorf("code attendu UNAUTHORIZED, got %s", ghost.Body.String())
|
||||
}
|
||||
if ghost.Body.String() != badPassword.Body.String() {
|
||||
t.Errorf("réponses distinguables (sécurité):\n%q\n%q", ghost.Body.String(), badPassword.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthLoginRateLimited(t *testing.T) {
|
||||
setTestAuth(t)
|
||||
repo := setTestStore(t)
|
||||
createAdmin(t, repo, "admin", authTestPassword)
|
||||
r := newTestRouterForAuth()
|
||||
doAuth(t, r, http.MethodPost, "/api/v1/devices", "", `{"deviceId":"`+authTestDevice+`"}`)
|
||||
|
||||
oldLimit := loginRateLimitPerMinute
|
||||
loginRateLimitPerMinute = 2
|
||||
hookCalls := 0
|
||||
oldHook := OnLoginRateLimited
|
||||
OnLoginRateLimited = func(ip string) { hookCalls++ }
|
||||
t.Cleanup(func() {
|
||||
loginRateLimitPerMinute = oldLimit
|
||||
OnLoginRateLimited = oldHook
|
||||
ResetLoginRateLimit()
|
||||
})
|
||||
|
||||
badBody := `{"username":"admin","password":"wrong-password","device_id":"` + authTestDevice + `"}`
|
||||
for i := 0; i < 2; i++ {
|
||||
if rec := doAuth(t, r, http.MethodPost, "/api/v1/auth/login", "", badBody); rec.Code != 401 {
|
||||
t.Fatalf("tentative %d: status %d, attendu 401", i+1, rec.Code)
|
||||
}
|
||||
}
|
||||
rec := doAuth(t, r, http.MethodPost, "/api/v1/auth/login", "", badBody)
|
||||
if rec.Code != 429 {
|
||||
t.Fatalf("quota dépassé: status %d body %s, attendu 429", rec.Code, rec.Body.String())
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), "RATE_LIMITED") {
|
||||
t.Errorf("code attendu RATE_LIMITED, got %s", rec.Body.String())
|
||||
}
|
||||
if hookCalls != 1 {
|
||||
t.Errorf("OnLoginRateLimited appelé %d fois, attendu 1", hookCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthLoginUnregisteredDevice(t *testing.T) {
|
||||
setTestAuth(t)
|
||||
repo := setTestStore(t)
|
||||
@@ -209,11 +276,11 @@ func TestAuthLoginUnregisteredDevice(t *testing.T) {
|
||||
r := newTestRouterForAuth()
|
||||
|
||||
rec := doAuth(t, r, http.MethodPost, "/api/v1/auth/login", "", `{"username":"admin","password":"`+authTestPassword+`","device_id":"`+authTestDevice+`"}`)
|
||||
if rec.Code != 400 {
|
||||
if rec.Code != 401 {
|
||||
t.Fatalf("device non enregistré: status %d body %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), "INVALID_DEVICE_ID") {
|
||||
t.Errorf("code attendu INVALID_DEVICE_ID, got %s", rec.Body.String())
|
||||
if !strings.Contains(rec.Body.String(), "UNAUTHORIZED") {
|
||||
t.Errorf("code attendu UNAUTHORIZED (anti-oracle), got %s", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -311,7 +378,7 @@ func TestResolveUser(t *testing.T) {
|
||||
t.Errorf("resolve: %+v", env.Data)
|
||||
}
|
||||
// Jamais email ni is_admin
|
||||
if len(rec.Body.Bytes()) < 0 || strings.Contains(rec.Body.String(), "is_admin") || strings.Contains(rec.Body.String(), "email") {
|
||||
if len(rec.Body.Bytes()) == 0 || strings.Contains(rec.Body.String(), "is_admin") || strings.Contains(rec.Body.String(), "email") {
|
||||
t.Error("resolve ne doit pas exposer email/is_admin")
|
||||
}
|
||||
|
||||
|
||||
+114
-2
@@ -1,20 +1,76 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"regexp"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/vaultdrop/backend/pkg/api"
|
||||
"github.com/kazier/backend/pkg/api"
|
||||
)
|
||||
|
||||
var deviceIDPattern = regexp.MustCompile(`^[0-9a-f]{32}$`)
|
||||
|
||||
// Anti-remplissage de POST /devices (FEEDBACK §moyenne) : l'endpoint est
|
||||
// public et bon marché (un upsert), donc anonyme et floodable. Limiteur
|
||||
// basique en mémoire, N enregistrements / minute / IP → 429 RATE_LIMITED.
|
||||
// Budget large (300) : les usages légitimes (1 device = 1 appel) sont très
|
||||
// loin du seuil ; la protection vise le flood massif, un WAF frontal fera
|
||||
// le reste en prod multi-réplicas.
|
||||
// TODO(suivi) : rate-limit distribué + budget global coordonné avec le
|
||||
// rate-limit login (cf. handlers/auth.go).
|
||||
var (
|
||||
devicesRateLimitPerMinute = 300
|
||||
devicesRateWindow = time.Minute
|
||||
devicesAttemptsMu sync.Mutex
|
||||
devicesAttempts = map[string][]time.Time{}
|
||||
)
|
||||
|
||||
// OnDevicesRateLimited est un hook d'observabilité (nil = no-op).
|
||||
var OnDevicesRateLimited func(ip string)
|
||||
|
||||
// ResetDevicesRateLimit vide le compteur (tests uniquement).
|
||||
func ResetDevicesRateLimit() {
|
||||
devicesAttemptsMu.Lock()
|
||||
defer devicesAttemptsMu.Unlock()
|
||||
devicesAttempts = map[string][]time.Time{}
|
||||
}
|
||||
|
||||
func devicesRateLimited(ip string, now time.Time) bool {
|
||||
devicesAttemptsMu.Lock()
|
||||
defer devicesAttemptsMu.Unlock()
|
||||
cutoff := now.Add(-devicesRateWindow)
|
||||
kept := make([]time.Time, 0, len(devicesAttempts[ip])+1)
|
||||
for _, t := range devicesAttempts[ip] {
|
||||
if t.After(cutoff) {
|
||||
kept = append(kept, t)
|
||||
}
|
||||
}
|
||||
if len(kept) >= devicesRateLimitPerMinute {
|
||||
devicesAttempts[ip] = kept
|
||||
return true
|
||||
}
|
||||
devicesAttempts[ip] = append(kept, now)
|
||||
return false
|
||||
}
|
||||
|
||||
type DeviceRegisterRequest struct {
|
||||
DeviceID string `json:"deviceId"`
|
||||
}
|
||||
|
||||
func DevicesRegister(c *gin.Context) {
|
||||
|
||||
// POST /devices est PUBLIC (sans token) : le device s'enregistre pour
|
||||
// exister, puis le client appelle POST /auth/login pour obtenir son
|
||||
// token user-first (cf. docs/api-v1.md §2).
|
||||
//
|
||||
// NOTE rate-limit : limiteur en mémoire ci-dessus (300/min/IP, 429
|
||||
// au-delà). Si le budget doit changer, ajuster
|
||||
// devicesRateLimitPerMinute + documenter dans docs/api-v1.md.
|
||||
//
|
||||
// NOTE MarkUser : repository.Devices.MarkUser retourne ErrNotFound quand
|
||||
// RowsAffected == 0 (device supprimé entre Exists et UPDATE) ; le login
|
||||
// mappe ce cas en 401 indistinguable (cf. handlers/auth.go).
|
||||
var req DeviceRegisterRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil || req.DeviceID == "" {
|
||||
api.Error(c, 400, "INVALID_REQUEST", "missing deviceId")
|
||||
@@ -25,6 +81,14 @@ func DevicesRegister(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if devicesRateLimited(c.ClientIP(), time.Now()) {
|
||||
if OnDevicesRateLimited != nil {
|
||||
OnDevicesRateLimited(c.ClientIP())
|
||||
}
|
||||
api.Error(c, http.StatusTooManyRequests, "RATE_LIMITED", "too many device registrations, retry later")
|
||||
return
|
||||
}
|
||||
|
||||
if Store == nil || Store.Repository == nil {
|
||||
api.Error(c, 503, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
@@ -41,3 +105,51 @@ func DevicesRegister(c *gin.Context) {
|
||||
api.OK(c, gin.H{"deviceId": req.DeviceID})
|
||||
|
||||
}
|
||||
|
||||
// DeviceDTO expose un device du compte (A1 — voir / révoquer).
|
||||
type DeviceDTO struct {
|
||||
DeviceID string `json:"deviceId"`
|
||||
LastSeenAt *int64 `json:"lastSeenAt"`
|
||||
}
|
||||
|
||||
// DevicesList renvoie les devices du compte connecté (`GET /devices`,
|
||||
// dernier login = ce compte, les plus récemment vus d'abord).
|
||||
func DevicesList(c *gin.Context) {
|
||||
if Store == nil || Store.Repository == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
rows, err := Store.Repository.Devices.ListByUser(userID)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
out := make([]DeviceDTO, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
out = append(out, DeviceDTO{DeviceID: row.DeviceID, LastSeenAt: row.LastSeenAt})
|
||||
}
|
||||
api.OK(c, out)
|
||||
}
|
||||
|
||||
// DeviceRevoke révoque un device du compte (`DELETE /devices/:id`, A1 —
|
||||
// téléphone perdu). Avec EnforceDeviceExists, ses tokens deviennent 401 et
|
||||
// son login est refusé (ré-enregistrement explicite requis). Ligne absente
|
||||
// ou d'un autre compte → 404 (pas d'énumération).
|
||||
func DeviceRevoke(c *gin.Context) {
|
||||
if Store == nil || Store.Repository == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "device not found")
|
||||
return
|
||||
}
|
||||
if err := Store.Repository.Devices.Revoke(userID, id); err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, gin.H{"id": id})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/kazier/backend/pkg/api"
|
||||
)
|
||||
|
||||
type exportRequest struct {
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
// ExportFull streame un zip AES-256 du compte (V3 S7 C3) :
|
||||
// `POST /export { password }` (mot de passe en corps JSON, jamais en query
|
||||
// — pas de fuite dans les logs). Chaque entrée (fichiers + manifest.json)
|
||||
// est chiffrée. Scope : fichiers vifs possédés uniquement.
|
||||
// Le mot de passe exige 8+ caractères (400 sinon) ; le zip peut être long
|
||||
// (pas de cap S7 — donnée de l'utilisateur, self-hosté).
|
||||
func ExportFull(c *gin.Context) {
|
||||
if Store == nil || Store.Repository == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
var req exportRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid JSON body (expected { password })")
|
||||
return
|
||||
}
|
||||
if len(req.Password) < 8 {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "password must be at least 8 characters")
|
||||
return
|
||||
}
|
||||
filename := fmt.Sprintf("kazier-export-%s.zip", time.Now().UTC().Format("20060102-150405"))
|
||||
c.Header("Content-Type", "application/zip")
|
||||
c.Header("Content-Disposition", "attachment; filename=\""+filename+"\"")
|
||||
// Erreur mid-stream : headers déjà partis — log + abort (zip tronqué,
|
||||
// documenté ; le manifest chiffré en fin d'archive manque alors).
|
||||
if err := Store.Export(c.Writer, userID, req.Password); err != nil {
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package handlers_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// TestExportEndpoint : POST /export exige un mot de passe 8+ (400 sinon) et
|
||||
// streame un zip (200 + application/zip).
|
||||
func TestExportEndpoint(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "ex"), "export-test-password", device)
|
||||
|
||||
for _, body := range []string{`{}`, `{"password":"court"}`, `not-json`} {
|
||||
rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/export", token, []byte(body), "application/json")
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "export-400:"+body)
|
||||
}
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/export", token, []byte(`{"password":"motdepasse-8"}`), "application/json")
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("export: attendu 200, got %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if ct := rec.Header().Get("Content-Type"); ct != "application/zip" {
|
||||
t.Fatalf("content-type = %q", ct)
|
||||
}
|
||||
if cd := rec.Header().Get("Content-Disposition"); cd == "" {
|
||||
t.Fatal("content-disposition manquant")
|
||||
}
|
||||
if rec.Body.Len() == 0 {
|
||||
t.Fatal("zip vide")
|
||||
}
|
||||
if rec.Body.Bytes()[0] != 'P' || rec.Body.Bytes()[1] != 'K' {
|
||||
t.Fatal("pas une archive zip (signature PK manquante)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
package handlers_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/kazier/backend/repository"
|
||||
"github.com/kazier/backend/service"
|
||||
)
|
||||
|
||||
// TestFactsEndpoint : GET /files/:id/facts — faits auto visibles (viewer+),
|
||||
// [] si rien d'extractible, 404 si trashé/absent/étranger.
|
||||
func TestFactsEndpoint(t *testing.T) {
|
||||
r, store, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, userID := registerAndLogin(t, r, repo, testUserUsername(device, "fa"), "facts-test-password", device)
|
||||
|
||||
fileID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(userID, fileID, "facture.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
if err := repo.Resources.UpdateOcrText(userID, fileID, "EDF\nFacture n° F-1 du 01/02/2026\nTotal : 10,00 €\n"); err != nil {
|
||||
t.Fatalf("ocr text: %v", err)
|
||||
}
|
||||
extractSvc := service.NewExtract(repo)
|
||||
if err := extractSvc.ProcessText(userID, fileID, "EDF\nFacture n° F-1 du 01/02/2026\nTotal : 10,00 €\n"); err != nil {
|
||||
t.Fatalf("extract: %v", err)
|
||||
}
|
||||
_ = store
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/facts", token, nil, "")
|
||||
env := expectOK(t, rec, "facts-get")
|
||||
var facts []struct {
|
||||
FactType string `json:"factType"`
|
||||
ValueText string `json:"valueText"`
|
||||
ValueNum *float64 `json:"valueNum"`
|
||||
Confidence float64 `json:"confidence"`
|
||||
Origin string `json:"origin"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &facts); err != nil {
|
||||
t.Fatalf("facts unmarshal: %v", err)
|
||||
}
|
||||
if len(facts) == 0 {
|
||||
t.Fatal("faits attendus, got []")
|
||||
}
|
||||
types := map[string]bool{}
|
||||
for _, f := range facts {
|
||||
types[f.FactType] = true
|
||||
if f.Origin != "auto" {
|
||||
t.Errorf("origine = %q, attendu auto", f.Origin)
|
||||
}
|
||||
}
|
||||
for _, want := range []string{"reference", "amount", "date", "doc_type"} {
|
||||
if !types[want] {
|
||||
t.Errorf("fait %q manquant: %+v", want, facts)
|
||||
}
|
||||
}
|
||||
|
||||
// Fichier visible sans faits → [] (200, jamais 404).
|
||||
emptyID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(userID, emptyID, "vide.txt", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert empty: %v", err)
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+emptyID+"/facts", token, nil, "")
|
||||
env = expectOK(t, rec, "facts-empty")
|
||||
if string(env.Data) != "[]" {
|
||||
t.Fatalf("sans faits : attendu [], got %s", env.Data)
|
||||
}
|
||||
|
||||
// Trashé → 404 (comme l'OCR).
|
||||
if _, err := store.Trash(userID, fileID); err != nil {
|
||||
t.Fatalf("trash: %v", err)
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/facts", token, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "facts-trashed")
|
||||
|
||||
// Étranger → 404.
|
||||
deviceB := repository.NewID()
|
||||
tokenB, _ := registerAndLogin(t, r, repo, testUserUsername(deviceB, "fb"), "facts-test-password", deviceB)
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+emptyID+"/facts", tokenB, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "facts-foreign")
|
||||
}
|
||||
|
||||
// TestCorrectFactsEndpoint : PATCH /files/:id/facts — remplace le jeu user
|
||||
// (editor+ = owner ici), renvoie le jeu complet ; 400 sur corps invalide ;
|
||||
// 404 si trashé/étranger.
|
||||
func TestCorrectFactsEndpoint(t *testing.T) {
|
||||
r, store, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, userID := registerAndLogin(t, r, repo, testUserUsername(device, "fc"), "facts-test-password", device)
|
||||
|
||||
fileID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(userID, fileID, "facture.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
extractSvc := service.NewExtract(repo)
|
||||
if err := extractSvc.ProcessText(userID, fileID, "EDF\nFacture n° F-1 du 01/02/2026\nTotal : 10,00 €\n"); err != nil {
|
||||
t.Fatalf("extract: %v", err)
|
||||
}
|
||||
_ = store
|
||||
|
||||
patch := func(body string) (int, envelope) {
|
||||
t.Helper()
|
||||
rec, _ := doRequest(t, r, http.MethodPatch, "/api/v1/files/"+fileID+"/facts", token, []byte(body), "application/json")
|
||||
var env envelope
|
||||
if rec.Code == http.StatusOK {
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &env); err != nil {
|
||||
t.Fatalf("patch unmarshal: %v body=%s", err, rec.Body.String())
|
||||
}
|
||||
}
|
||||
return rec.Code, env
|
||||
}
|
||||
|
||||
// Correction : émetteur fixé par l'utilisateur.
|
||||
code, env := patch(`{"facts":[{"factType":"issuer","valueText":"Vrai Fournisseur","confidence":1}]}`)
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("patch: attendu 200, got %d", code)
|
||||
}
|
||||
var facts []struct {
|
||||
FactType string `json:"factType"`
|
||||
ValueText string `json:"valueText"`
|
||||
Origin string `json:"origin"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &facts); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
foundUser, foundAuto := false, false
|
||||
for _, f := range facts {
|
||||
if f.FactType == "issuer" && f.Origin == "user" && f.ValueText == "Vrai Fournisseur" {
|
||||
foundUser = true
|
||||
}
|
||||
if f.Origin == "auto" {
|
||||
foundAuto = true
|
||||
}
|
||||
}
|
||||
if !foundUser || !foundAuto {
|
||||
t.Fatalf("jeu après correction inattendu: %+v", facts)
|
||||
}
|
||||
|
||||
// Corps invalides → 400.
|
||||
for _, body := range []string{
|
||||
`{}`,
|
||||
`{"facts":[{"factType":"couleur","valueText":"bleu"}]}`,
|
||||
`{"facts":[{"factType":"date","valueDate":"12/03/2026"}]}`,
|
||||
`{"facts":[{"factType":"issuer"}]}`,
|
||||
`not-json`,
|
||||
} {
|
||||
rec, _ := doRequest(t, r, http.MethodPatch, "/api/v1/files/"+fileID+"/facts", token, []byte(body), "application/json")
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "facts-patch-400:"+body)
|
||||
}
|
||||
|
||||
// Étranger (viewer simulé : partage + snapshot hors scope ici, un autre
|
||||
// user sans grant suffit — CorrectFacts exige editor+) → 404.
|
||||
deviceB := repository.NewID()
|
||||
tokenB, _ := registerAndLogin(t, r, repo, testUserUsername(deviceB, "fd"), "facts-test-password", deviceB)
|
||||
rec, _ := doRequest(t, r, http.MethodPatch, "/api/v1/files/"+fileID+"/facts", tokenB,
|
||||
[]byte(`{"facts":[{"factType":"issuer","valueText":"X"}]}`), "application/json")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "facts-patch-foreign")
|
||||
|
||||
// Trashé → 404.
|
||||
if _, err := store.Trash(userID, fileID); err != nil {
|
||||
t.Fatalf("trash: %v", err)
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodPatch, "/api/v1/files/"+fileID+"/facts", token,
|
||||
[]byte(`{"facts":[{"factType":"issuer","valueText":"X"}]}`), "application/json")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "facts-patch-trashed")
|
||||
}
|
||||
+451
-20
@@ -5,20 +5,43 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/vaultdrop/backend/pkg/api"
|
||||
"github.com/kazier/backend/pkg/api"
|
||||
"github.com/kazier/backend/repository"
|
||||
"github.com/kazier/backend/service"
|
||||
)
|
||||
|
||||
func intParam(value string, fallback int) int {
|
||||
if value == "" {
|
||||
return fallback
|
||||
// parsePageParams valide page/pageSize (400 si garbage, cf. FEEDBACK A7/A16)
|
||||
// au lieu du fallback silencieux. Cap pageSize 200 et offset 100000.
|
||||
func parsePageParams(c *gin.Context) (page, pageSize int, ok bool) {
|
||||
page = 1
|
||||
pageSize = 50
|
||||
if v := c.Query("page"); v != "" {
|
||||
p, err := strconv.Atoi(v)
|
||||
if err != nil || p < 1 {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid query param `page`")
|
||||
return 0, 0, false
|
||||
}
|
||||
parsed, err := strconv.Atoi(value)
|
||||
if err != nil || parsed < 1 {
|
||||
return fallback
|
||||
page = p
|
||||
}
|
||||
return parsed
|
||||
if v := c.Query("pageSize"); v != "" {
|
||||
ps, err := strconv.Atoi(v)
|
||||
if err != nil || ps < 1 {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid query param `pageSize`")
|
||||
return 0, 0, false
|
||||
}
|
||||
pageSize = ps
|
||||
}
|
||||
if pageSize > 200 {
|
||||
pageSize = 200
|
||||
}
|
||||
if (page-1)*pageSize > 100000 {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "page offset too large")
|
||||
return 0, 0, false
|
||||
}
|
||||
return page, pageSize, true
|
||||
}
|
||||
|
||||
func FilesList(c *gin.Context) {
|
||||
@@ -27,12 +50,16 @@ func FilesList(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
page := intParam(c.Query("page"), 1)
|
||||
pageSize := intParam(c.Query("pageSize"), 50)
|
||||
if pageSize > 200 {
|
||||
pageSize = 200
|
||||
page, pageSize, ok := parsePageParams(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
files, total, err := Store.ListFiles(userID, c.Query("folderId"), page, pageSize, c.Query("sort"), c.Query("order"))
|
||||
folderID := c.Query("folderId")
|
||||
if folderID != "" && !deviceIDPattern.MatchString(folderID) {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid query param `folderId`")
|
||||
return
|
||||
}
|
||||
files, total, err := Store.ListFiles(userID, folderID, page, pageSize, c.Query("sort"), c.Query("order"))
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
@@ -59,6 +86,247 @@ func FilesGet(c *gin.Context) {
|
||||
api.OK(c, file)
|
||||
}
|
||||
|
||||
// FilesGetOcr expose l'extrait OCR server-side d'un fichier visible (viewer+,
|
||||
// own ou shared) : GET /files/:id/ocr. Réponse { text } ("" si pas encore
|
||||
// extrait) + updatedAt. Permet à un second device de récupérer le texte déjà
|
||||
// calculé sans re-téléverser les octets (cf. docs/api-v1.md §5).
|
||||
func FilesGetOcr(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
dto, err := Store.GetFileOcr(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, dto)
|
||||
}
|
||||
|
||||
// FilesGetFacts expose les faits génériques d'un fichier visible (V3 S2) :
|
||||
// GET /files/:id/facts. Même portée que l'OCR (viewer+, own ou shared) —
|
||||
// les faits dérivent du texte. `[]` si rien d'extractible (jamais 404 pour
|
||||
// un fichier visible sans faits) ; 404 si trashé/absent/étranger.
|
||||
func FilesGetFacts(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
facts, err := Store.GetFacts(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
if facts == nil {
|
||||
facts = []service.FactDTO{}
|
||||
}
|
||||
api.OK(c, facts)
|
||||
}
|
||||
|
||||
// FilesCorrectFacts remplace le jeu `user` des faits (V3 S3) :
|
||||
// PATCH /files/:id/facts `{ facts: [...] }` (full-replace, jamais de diff).
|
||||
// Nécessite editor+ (comme update_metadata) ; viewer- → 404 NOT_FOUND.
|
||||
// Corps invalide (type inconnu, date non ISO, fait sans valeur) → 400.
|
||||
func FilesCorrectFacts(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
Facts []service.FactDTO `json:"facts"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid JSON body (expected { facts: [...] })")
|
||||
return
|
||||
}
|
||||
if req.Facts == nil {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "missing required field `facts`")
|
||||
return
|
||||
}
|
||||
if len(req.Facts) > 100 {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "`facts` too long (max 100)")
|
||||
return
|
||||
}
|
||||
if err := Store.CorrectFacts(userID, id, req.Facts); err != nil {
|
||||
// Erreurs de validation (type/date/valeur) → 400 explicite, pas 500 :
|
||||
// ce sont des fautes client, jamais des pannes (cf. FEEDBACK #8).
|
||||
if errors.Is(err, service.ErrInvalidFact) {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", err.Error())
|
||||
return
|
||||
}
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
facts, err := Store.GetFacts(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, facts)
|
||||
}
|
||||
|
||||
// RelationsList expose le graphe documentaire d'une ressource possédée
|
||||
// (V3 S5) : `GET /files/:id/relations`. Réconcilie d'abord (les arêtes
|
||||
// déterministes convergent à la lecture), puis renvoie les arêtes vivantes
|
||||
// (l'autre bout non trashé), confiance décroissante. Owner-only : le graphe
|
||||
// d'autrui ne s'énumère pas (404, comme `/shares`).
|
||||
func RelationsList(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
relations, err := Store.ListRelations(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
if relations == nil {
|
||||
relations = []service.RelationDTO{}
|
||||
}
|
||||
api.OK(c, relations)
|
||||
}
|
||||
|
||||
// RelationVerdict enregistre un verdict explicite sur une arête (V3 S5) :
|
||||
// `POST /files/:id/relations/verdict { otherId, linkType, confirmed }`.
|
||||
// Confirmé → 1.0, infirmé → 0.0 (conservé, bloque la re-création auto).
|
||||
// Owner-only, les deux bouts possédés et vifs, sinon 404 ; type/paire
|
||||
// invalide → 400.
|
||||
func RelationVerdict(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
OtherID string `json:"otherId"`
|
||||
LinkType string `json:"linkType"`
|
||||
Confirmed bool `json:"confirmed"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid JSON body (expected { otherId, linkType, confirmed })")
|
||||
return
|
||||
}
|
||||
if !deviceIDPattern.MatchString(req.OtherID) {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid `otherId` (32 lowercase hex)")
|
||||
return
|
||||
}
|
||||
if err := Store.LinkVerdict(userID, id, req.OtherID, req.LinkType, req.Confirmed); err != nil {
|
||||
if errors.Is(err, service.ErrInvalidLinkType) {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", err.Error())
|
||||
return
|
||||
}
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
relations, err := Store.ListRelations(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, relations)
|
||||
}
|
||||
|
||||
// DuplicatesList expose les doublons d'octets du compte (V3 S5 B2) :
|
||||
// `GET /duplicates`. Groupes par hash, plus gros d'abord, avec le gain
|
||||
// récupérable (« garder un seul »). Scope owner — `[]` quand propre.
|
||||
func DuplicatesList(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
groups, err := Store.ListDuplicates(userID)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, groups)
|
||||
}
|
||||
|
||||
// FilesContent streame les octets physiques d'un fichier visible (viewer+,
|
||||
// own ou shared) : GET /files/:id/content. Réponse binaire (Content-Type du
|
||||
// fichier, Content-Disposition inline) — jamais d'enveloppe {data}. Supporte
|
||||
// Range via http.ServeContent (Gin c.File). Sans bytes serveur (ressource
|
||||
// metadata-only jamais uploadée) → 404 NOT_FOUND, comme invisible (pas
|
||||
// d'énumération). V1 streaming : le client ne persiste rien.
|
||||
func FilesContent(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
info, err := Store.OpenContent(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
c.Header("Content-Type", info.MimeType)
|
||||
c.Header("Content-Disposition", "inline; filename=\""+info.Name+"\"")
|
||||
c.File(info.Path)
|
||||
}
|
||||
|
||||
// FilesPreview sert la vignette JPEG d'un fichier visible (V3 S7) :
|
||||
// `GET /files/:id/preview` (binaire brut, hors enveloppe, `inline`).
|
||||
// Générée à la demande (images pur Go, PDF page 1 via `pdftoppm`) et cachée
|
||||
// jusqu'au prochain changement (taille/`updated_at`). 404 NOT_FOUND si
|
||||
// invisible/metadata-only (indistinguables, pas d'énumération), 404
|
||||
// NO_PREVIEW si le type ne se prévisualise pas (l'UI montre l'icône).
|
||||
func FilesPreview(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
path, err := Store.OpenPreview(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
c.Header("Content-Type", "image/jpeg")
|
||||
c.Header("Content-Disposition", "inline; filename=\""+id+".jpg\"")
|
||||
c.Header("Cache-Control", "private, max-age=3600")
|
||||
c.File(path)
|
||||
}
|
||||
|
||||
func FilesDelete(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
@@ -70,7 +338,19 @@ func FilesDelete(c *gin.Context) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
deletedID, err := Store.DeleteFile(userID, id)
|
||||
// V3 corbeille : DELETE = trash par défaut ; ?mode=purge = destruction
|
||||
// définitive (lignes + versions + octets). Mode inconnu → 400.
|
||||
var deletedID string
|
||||
var err error
|
||||
switch c.Query("mode") {
|
||||
case "", "trash":
|
||||
deletedID, err = Store.Trash(userID, id)
|
||||
case "purge":
|
||||
deletedID, err = Store.Purge(userID, id)
|
||||
default:
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid query param `mode` (trash|purge)")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
@@ -78,6 +358,94 @@ func FilesDelete(c *gin.Context) {
|
||||
api.OK(c, gin.H{"id": deletedID})
|
||||
}
|
||||
|
||||
// FilesRestore restaure une ressource trashée (POST /files/:id/restore).
|
||||
// Absente/étrangère → 404 NOT_FOUND ; déjà vivante → succès idempotent.
|
||||
func FilesRestore(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
restoredID, err := Store.Restore(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, gin.H{"id": restoredID})
|
||||
}
|
||||
|
||||
// TrashList expose la corbeille (GET /trash) : ressources trashées du user,
|
||||
// les plus récentes d'abord, paginé comme les autres listes.
|
||||
func TrashList(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
page, pageSize, ok := parsePageParams(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
items, total, err := Store.ListTrashed(userID, page, pageSize)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OKList(c, items, page, pageSize, total)
|
||||
}
|
||||
|
||||
// VersionsList expose l'historique des octets (GET /files/:id/versions),
|
||||
// le plus récent d'abord. Ressource trashée/absente/étrangère → 404.
|
||||
func VersionsList(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
versions, err := Store.ListVersions(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, versions)
|
||||
}
|
||||
|
||||
// VersionRestore recopie la génération n vers le live
|
||||
// (POST /files/:id/versions/:n/restore). Génération inconnue → 404.
|
||||
func VersionRestore(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
n, err := strconv.Atoi(c.Param("n"))
|
||||
if err != nil || n < 1 {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "version not found")
|
||||
return
|
||||
}
|
||||
dto, err := Store.RestoreVersion(userID, id, n)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, dto)
|
||||
}
|
||||
|
||||
func FilesUpload(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
@@ -85,6 +453,17 @@ func FilesUpload(c *gin.Context) {
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
folderID := c.PostForm("folderId")
|
||||
if folderID != "" && !deviceIDPattern.MatchString(folderID) {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid `folderId`")
|
||||
return
|
||||
}
|
||||
// resourceId optionnel : cible un fichier déjà connu (metadata-only de
|
||||
// l'outbox) pour lui attacher ses octets physiques (ex. OCR serveur).
|
||||
resourceID := c.PostForm("resourceId")
|
||||
if resourceID != "" && !deviceIDPattern.MatchString(resourceID) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
|
||||
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, Store.MaxFileSize+1)
|
||||
file, err := c.FormFile("file")
|
||||
@@ -102,8 +481,14 @@ func FilesUpload(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
dto, err := Store.Upload(userID, file, folderID)
|
||||
dto, err := Store.Upload(userID, file, folderID, resourceID)
|
||||
if err != nil {
|
||||
// Nom/extension rejetés côté service (TrimSpace, cap 255,
|
||||
// anti-slash/retour-ligne, cf. FEEDBACK A16) → 400, jamais 500.
|
||||
if errors.Is(err, service.ErrInvalidUpload) {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid file name")
|
||||
return
|
||||
}
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
@@ -135,15 +520,61 @@ func FilesSearch(c *gin.Context) {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "missing required query param `q`")
|
||||
return
|
||||
}
|
||||
page := intParam(c.Query("page"), 1)
|
||||
pageSize := intParam(c.Query("pageSize"), 50)
|
||||
if pageSize > 200 {
|
||||
pageSize = 200
|
||||
if len(q) > 200 {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "query param `q` too long (max 200)")
|
||||
return
|
||||
}
|
||||
files, total, err := Store.SearchFiles(userID, q, page, pageSize)
|
||||
page, pageSize, ok := parsePageParams(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
facets, ok := parseSearchFacets(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
files, total, err := Store.SearchFiles(userID, q, page, pageSize, facets)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OKList(c, files, page, pageSize, total)
|
||||
}
|
||||
|
||||
// parseSearchFacets lit les facettes V3 (`issuer`, `from`/`to` ISO,
|
||||
// `min_amount`/`max_amount` numériques, `doc_type`) — 400 si malformées.
|
||||
func parseSearchFacets(c *gin.Context) (repository.SearchFacets, bool) {
|
||||
var facets repository.SearchFacets
|
||||
facets.Issuer = strings.TrimSpace(c.Query("issuer"))
|
||||
facets.DocType = strings.TrimSpace(c.Query("doc_type"))
|
||||
if v := strings.TrimSpace(c.Query("from")); v != "" {
|
||||
if _, err := time.Parse("2006-01-02", v); err != nil {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "query param `from` must be ISO YYYY-MM-DD")
|
||||
return facets, false
|
||||
}
|
||||
facets.From = v
|
||||
}
|
||||
if v := strings.TrimSpace(c.Query("to")); v != "" {
|
||||
if _, err := time.Parse("2006-01-02", v); err != nil {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "query param `to` must be ISO YYYY-MM-DD")
|
||||
return facets, false
|
||||
}
|
||||
facets.To = v
|
||||
}
|
||||
if v := strings.TrimSpace(c.Query("min_amount")); v != "" {
|
||||
f, err := strconv.ParseFloat(strings.ReplaceAll(v, ",", "."), 64)
|
||||
if err != nil {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "query param `min_amount` must be numeric")
|
||||
return facets, false
|
||||
}
|
||||
facets.MinAmount = &f
|
||||
}
|
||||
if v := strings.TrimSpace(c.Query("max_amount")); v != "" {
|
||||
f, err := strconv.ParseFloat(strings.ReplaceAll(v, ",", "."), 64)
|
||||
if err != nil {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "query param `max_amount` must be numeric")
|
||||
return facets, false
|
||||
}
|
||||
facets.MaxAmount = &f
|
||||
}
|
||||
return facets, true
|
||||
}
|
||||
|
||||
+562
-12
@@ -7,19 +7,21 @@ import (
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/vaultdrop/backend/dbtest"
|
||||
"github.com/vaultdrop/backend/handlers"
|
||||
"github.com/vaultdrop/backend/pkg/auth"
|
||||
"github.com/vaultdrop/backend/pkg/passwd"
|
||||
"github.com/vaultdrop/backend/repository"
|
||||
"github.com/vaultdrop/backend/service"
|
||||
"github.com/kazier/backend/dbtest"
|
||||
"github.com/kazier/backend/handlers"
|
||||
"github.com/kazier/backend/pkg/auth"
|
||||
"github.com/kazier/backend/pkg/passwd"
|
||||
"github.com/kazier/backend/repository"
|
||||
"github.com/kazier/backend/service"
|
||||
)
|
||||
|
||||
const handlersTestURL = "postgres://vaultdrop:vaultdrop@localhost:5432/vaultdrop_handlers_test?sslmode=disable"
|
||||
const handlersTestURL = "postgres://kazier:kazier@localhost:5432/kazier_handlers_test?sslmode=disable"
|
||||
|
||||
type envelope struct {
|
||||
Data json.RawMessage `json:"data"`
|
||||
@@ -39,6 +41,11 @@ type fileDTO struct {
|
||||
CreatedAt string `json:"createdAt"`
|
||||
}
|
||||
|
||||
type fileOcrDTO struct {
|
||||
Text string `json:"text"`
|
||||
UpdatedAt string `json:"updatedAt"`
|
||||
}
|
||||
|
||||
type apiError struct {
|
||||
Error struct {
|
||||
Code string `json:"code"`
|
||||
@@ -68,6 +75,8 @@ func setup(t *testing.T) (*gin.Engine, *service.Resources, *repository.Repositor
|
||||
}
|
||||
handlers.Auth = manager
|
||||
handlers.Store = store
|
||||
handlers.ResetDevicesRateLimit()
|
||||
handlers.ResetLoginRateLimit()
|
||||
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
@@ -183,6 +192,29 @@ func uploadMultipart(t *testing.T, r *gin.Engine, token, folderID, filename stri
|
||||
return rec
|
||||
}
|
||||
|
||||
// uploadMultipartToResource uploads bytes targeted at an existing resource_id
|
||||
// (metadata-only file from the outbox), le cas d'usage OCR.
|
||||
func uploadMultipartToResource(t *testing.T, r *gin.Engine, token, resourceID, filename string, content []byte) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
var body bytes.Buffer
|
||||
writer := multipart.NewWriter(&body)
|
||||
part, err := writer.CreateFormFile("file", filename)
|
||||
if err != nil {
|
||||
t.Fatalf("create form file: %v", err)
|
||||
}
|
||||
if _, err := part.Write(content); err != nil {
|
||||
t.Fatalf("write body: %v", err)
|
||||
}
|
||||
if err := writer.WriteField("resourceId", resourceID); err != nil {
|
||||
t.Fatalf("write resourceId: %v", err)
|
||||
}
|
||||
if err := writer.Close(); err != nil {
|
||||
t.Fatalf("close writer: %v", err)
|
||||
}
|
||||
rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/files/upload", token, body.Bytes(), writer.FormDataContentType())
|
||||
return rec
|
||||
}
|
||||
|
||||
func TestFilesFlow(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
deviceA := repository.NewID()
|
||||
@@ -328,6 +360,83 @@ func TestSearchFiles(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestSearchFacets : filtres V3 sur faits + 400 si malformés.
|
||||
func TestSearchFacets(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, user := registerAndLogin(t, r, repo, testUserUsername(device, "sf"), "search-test-password", device)
|
||||
|
||||
edfID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(user, edfID, "scan_1.pdf", "", 100, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
if err := repo.Resources.UpdateOcrText(user, edfID, "Facture EDF de mars"); err != nil {
|
||||
t.Fatalf("ocr: %v", err)
|
||||
}
|
||||
num := 84.20
|
||||
if err := repo.Resources.UpsertAutoFacts(edfID, []repository.FactParam{
|
||||
{Type: "issuer", ValueText: "EDF", Confidence: 0.9},
|
||||
{Type: "amount", ValueText: "84.20 €", ValueNum: &num, Confidence: 0.9},
|
||||
{Type: "doc_type", ValueText: "facture", Confidence: 0.8},
|
||||
}); err != nil {
|
||||
t.Fatalf("facts: %v", err)
|
||||
}
|
||||
otherID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(user, otherID, "scan_2.pdf", "", 100, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
|
||||
get := func(query string) []fileDTO {
|
||||
t.Helper()
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/search"+query, token, nil, "")
|
||||
env := expectOK(t, rec, "search-facets:"+query)
|
||||
var files []fileDTO
|
||||
if err := json.Unmarshal(env.Data, &files); err != nil {
|
||||
t.Fatalf("unmarshal %s: %v", query, err)
|
||||
}
|
||||
return files
|
||||
}
|
||||
|
||||
// FTS via OCR (nom opaque).
|
||||
if files := get("?q=EDF"); len(files) == 0 || files[0].ID != edfID {
|
||||
t.Errorf("q=EDF via OCR: %+v", files)
|
||||
}
|
||||
// Facette émetteur : filtre.
|
||||
if files := get("?q=facture&issuer=edf"); len(files) != 1 || files[0].ID != edfID {
|
||||
t.Errorf("issuer=edf: %+v", files)
|
||||
}
|
||||
if files := get("?q=facture&issuer=engie"); len(files) != 0 {
|
||||
t.Errorf("issuer=engie doit filtrer: %+v", files)
|
||||
}
|
||||
// Facette montant.
|
||||
if files := get("?q=facture&min_amount=100"); len(files) != 0 {
|
||||
t.Errorf("min_amount=100 doit exclure 84.20: %+v", files)
|
||||
}
|
||||
if files := get("?q=facture&max_amount=100"); len(files) != 1 {
|
||||
t.Errorf("max_amount=100: %+v", files)
|
||||
}
|
||||
// Facette doc_type.
|
||||
if files := get("?q=scan&doc_type=facture"); len(files) != 1 || files[0].ID != edfID {
|
||||
t.Errorf("doc_type=facture: %+v", files)
|
||||
}
|
||||
|
||||
// Malformées → 400.
|
||||
for _, query := range []string{
|
||||
"?q=facture&from=12/03/2026",
|
||||
"?q=facture&to=demain",
|
||||
"?q=facture&min_amount=beaucoup",
|
||||
"?q=facture&max_amount=",
|
||||
} {
|
||||
// max_amount vide = absent (pas d'erreur) — seul le cas non-vide
|
||||
// non-numérique est rejeté ; on l'exclut ici.
|
||||
if query == "?q=facture&max_amount=" {
|
||||
continue
|
||||
}
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/search"+query, token, nil, "")
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "search-facets-400:"+query)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchFilesEscapesPercent(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
@@ -385,6 +494,67 @@ func TestUploadNameConflict(t *testing.T) {
|
||||
expectError(t, rec, http.StatusConflict, "NAME_CONFLICT", "upload-dupe")
|
||||
}
|
||||
|
||||
func TestUploadTargetsExistingResource(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, user := registerAndLogin(t, r, repo, testUserUsername(device, "tr"), "upload-test-password", device)
|
||||
|
||||
// Fiche metadata-only (même sémantique que l'outbox create_resource).
|
||||
resourceID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(user, resourceID, "scan.png", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert metadata-only file: %v", err)
|
||||
}
|
||||
|
||||
rec := uploadMultipartToResource(t, r, token, resourceID, "scan.png", []byte("PNGDATA"))
|
||||
env := expectOK(t, rec, "upload-target")
|
||||
var uploaded fileDTO
|
||||
if err := json.Unmarshal(env.Data, &uploaded); err != nil {
|
||||
t.Fatalf("upload-target: unmarshal: %v", err)
|
||||
}
|
||||
if uploaded.ID != resourceID {
|
||||
t.Errorf("id attendu %s, got %s", resourceID, uploaded.ID)
|
||||
}
|
||||
if uploaded.Size != 7 {
|
||||
t.Errorf("taille = %d, attendu 7", uploaded.Size)
|
||||
}
|
||||
|
||||
// Toujours une seule ligne (pas de doublon).
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files", token, nil, "")
|
||||
env = expectOK(t, rec, "list-after-targeted-upload")
|
||||
var files []fileDTO
|
||||
if err := json.Unmarshal(env.Data, &files); err != nil {
|
||||
t.Fatalf("list: unmarshal: %v", err)
|
||||
}
|
||||
if len(files) != 1 || files[0].ID != resourceID {
|
||||
t.Errorf("aucun doublon attendu, got %+v", files)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUploadTargetForeignResourceNotFound(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
deviceA := repository.NewID()
|
||||
deviceB := repository.NewID()
|
||||
tokenA, _ := registerAndLogin(t, r, repo, testUserUsername(deviceA, "tf"), "upload-test-password", deviceA)
|
||||
_, userB := registerAndLogin(t, r, repo, testUserUsername(deviceB, "tg"), "upload-test-password-b", deviceB)
|
||||
|
||||
resourceID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(userB, resourceID, "mine.png", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert B file: %v", err)
|
||||
}
|
||||
|
||||
rec := uploadMultipartToResource(t, r, tokenA, resourceID, "mine.png", []byte("DATA"))
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "upload-foreign")
|
||||
}
|
||||
|
||||
func TestUploadTargetInvalidID(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "ti"), "upload-test-password", device)
|
||||
|
||||
rec := uploadMultipartToResource(t, r, token, "NOT-HEX", "x.png", []byte("DATA"))
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "upload-invalid-id")
|
||||
}
|
||||
|
||||
func TestFoldersListAndScoping(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
deviceA := repository.NewID()
|
||||
@@ -428,12 +598,12 @@ func TestFilesPaginationDefaultsAndClamp(t *testing.T) {
|
||||
t.Errorf("clamp pageSize: %+v", env.Meta)
|
||||
}
|
||||
|
||||
// Paramètres invalides → défauts (page=1, pageSize=50)
|
||||
// Paramètres invalides → 400 (plus de fallback silencieux, cf. A16).
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files?page=0&pageSize=-5", token, nil, "")
|
||||
env = expectOK(t, rec, "invalid-params")
|
||||
if env.Meta == nil || env.Meta.Page != 1 || env.Meta.PageSize != 50 {
|
||||
t.Errorf("defauts page/pageSize: %+v", env.Meta)
|
||||
}
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "negative-params")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files?page=abc&pageSize=zz", token, nil, "")
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "garbage-params")
|
||||
|
||||
// sort/order inconnus → pas d'erreur (défaut created_at desc)
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files?sort=zzz&order=up&pageSize=10", token, nil, "")
|
||||
@@ -493,3 +663,383 @@ func TestFilesGetDeleteRejectInvalidID(t *testing.T) {
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID, otherToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "get-cross-user")
|
||||
}
|
||||
|
||||
func TestFilesGetOcrOwnerShareAndScoping(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
ownerDevice := repository.NewID()
|
||||
ownerToken, owner := registerAndLogin(t, r, repo, testUserUsername(ownerDevice, "oc"), "ocr-get-test-password", ownerDevice)
|
||||
granteeDevice := repository.NewID()
|
||||
granteeToken, grantee := registerAndLogin(t, r, repo, testUserUsername(granteeDevice, "og"), "ocr-get-test-password-b", granteeDevice)
|
||||
outsiderDevice := repository.NewID()
|
||||
outsiderToken, _ := registerAndLogin(t, r, repo, testUserUsername(outsiderDevice, "oh"), "ocr-get-test-password-c", outsiderDevice)
|
||||
|
||||
fileID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(owner, fileID, "scan.png", "", 128, nil, nil); err != nil {
|
||||
t.Fatalf("insert file: %v", err)
|
||||
}
|
||||
if err := repo.Resources.UpdateOcrText(owner, fileID, "extrait du scan"); err != nil {
|
||||
t.Fatalf("write ocr_text: %v", err)
|
||||
}
|
||||
|
||||
// Owner : texte + updatedAt.
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/ocr", ownerToken, nil, "")
|
||||
envOcr := expectOK(t, rec, "ocr-owner")
|
||||
var ownerOcr fileOcrDTO
|
||||
if err := json.Unmarshal(envOcr.Data, &ownerOcr); err != nil {
|
||||
t.Fatalf("ocr-owner: unmarshal: %v", err)
|
||||
}
|
||||
if ownerOcr.Text != "extrait du scan" || ownerOcr.UpdatedAt == "" {
|
||||
t.Errorf("ocr owner = %+v, attendu texte+updatedAt", ownerOcr)
|
||||
}
|
||||
|
||||
// Grantée viewer+ : même texte (partage rend l'OCR lisible).
|
||||
if err := repo.Shares.Upsert(fileID, grantee, "viewer", false, nil, owner); err != nil {
|
||||
t.Fatalf("share: %v", err)
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/ocr", granteeToken, nil, "")
|
||||
envOcr = expectOK(t, rec, "ocr-grantee")
|
||||
var granteeOcr fileOcrDTO
|
||||
if err := json.Unmarshal(envOcr.Data, &granteeOcr); err != nil {
|
||||
t.Fatalf("ocr-grantee: unmarshal: %v", err)
|
||||
}
|
||||
if granteeOcr.Text != "extrait du scan" {
|
||||
t.Errorf("ocr grantee = %+v, attendu texte partagé", granteeOcr)
|
||||
}
|
||||
|
||||
// Outsider : invisible (404).
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/ocr", outsiderToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "ocr-outsider")
|
||||
|
||||
// ID invalide → 404.
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/not-hex/ocr", ownerToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "ocr-invalid-id")
|
||||
}
|
||||
|
||||
func TestFilesUploadRejectsBadFilename(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "bn"), "badname-test-password", device)
|
||||
|
||||
// Body multipart construit à la main : filename brut, NON neutralisé par
|
||||
// le writer stdlib (qui basename/encode de son côté). Le serveur doit
|
||||
// rejeter en 400, jamais 500 (cf. A16).
|
||||
rawUpload := func(filename string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
boundary := "testboundary123"
|
||||
body := "--" + boundary + "\r\n" +
|
||||
"Content-Disposition: form-data; name=\"file\"; filename=\"" + filename + "\"\r\n" +
|
||||
"Content-Type: application/octet-stream\r\n\r\n" +
|
||||
"hi\r\n" +
|
||||
"--" + boundary + "--\r\n"
|
||||
rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/files/upload", token, []byte(body), "multipart/form-data; boundary="+boundary)
|
||||
return rec
|
||||
}
|
||||
|
||||
rec := rawUpload("../../evil.txt")
|
||||
// La stdlib Go basename déjà côté lecture (défense en profondeur côté
|
||||
// service via sanitizeFileName, testé en unitaire + backslash) : le nom
|
||||
// stocké est "evil.txt" et aucun chemin ne s'échappe (destPath dérivé
|
||||
// uniquement de IDs 32-hex + extension sanitizée).
|
||||
env := expectOK(t, rec, "upload-traversal-name")
|
||||
var uploaded fileDTO
|
||||
if err := json.Unmarshal(env.Data, &uploaded); err != nil {
|
||||
t.Fatalf("upload-traversal-name: unmarshal: %v", err)
|
||||
}
|
||||
if uploaded.Name != "evil.txt" {
|
||||
t.Errorf("nom traversal neutralisé attendu \"evil.txt\", got %q", uploaded.Name)
|
||||
}
|
||||
|
||||
// Backslash : préservé par la stdlib → rejet service 400, jamais 500.
|
||||
rec = rawUpload(`bad\name.txt`)
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "upload-backslash-name")
|
||||
}
|
||||
|
||||
func TestSearchQTooLong(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "ql"), "search-test-password", device)
|
||||
|
||||
long := make([]byte, 201)
|
||||
for i := range long {
|
||||
long[i] = 'a'
|
||||
}
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/search?q="+string(long), token, nil, "")
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "search-q-too-long")
|
||||
}
|
||||
|
||||
func TestFilesContentStreaming(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
ownerDevice := repository.NewID()
|
||||
ownerToken, owner := registerAndLogin(t, r, repo, testUserUsername(ownerDevice, "ct"), "content-test-password", ownerDevice)
|
||||
granteeDevice := repository.NewID()
|
||||
granteeToken, grantee := registerAndLogin(t, r, repo, testUserUsername(granteeDevice, "cg"), "content-test-password-b", granteeDevice)
|
||||
outsiderDevice := repository.NewID()
|
||||
outsiderToken, _ := registerAndLogin(t, r, repo, testUserUsername(outsiderDevice, "ch"), "content-test-password-c", outsiderDevice)
|
||||
|
||||
// Ressource metadata-only (outbox) puis upload ciblé des octets (≤10o, cf. setup).
|
||||
fileID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(owner, fileID, "note.txt", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert file: %v", err)
|
||||
}
|
||||
rec := uploadMultipartToResource(t, r, ownerToken, fileID, "note.txt", []byte("hello"))
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("upload ciblé: attendu 200, got %d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
// Owner : octets + Content-Type.
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/content", ownerToken, nil, "")
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("content-owner: attendu 200, got %d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if rec.Body.String() != "hello" {
|
||||
t.Errorf("content-owner = %q, attendu %q", rec.Body.String(), "hello")
|
||||
}
|
||||
if ct := rec.Header().Get("Content-Type"); ct == "" {
|
||||
t.Errorf("content-owner: Content-Type manquant")
|
||||
}
|
||||
|
||||
// Grantée viewer : mêmes octets (résolution dir owner, pas requester).
|
||||
if err := repo.Shares.Upsert(fileID, grantee, "viewer", false, nil, owner); err != nil {
|
||||
t.Fatalf("share: %v", err)
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/content", granteeToken, nil, "")
|
||||
if rec.Code != http.StatusOK || rec.Body.String() != "hello" {
|
||||
t.Errorf("content-grantee: attendu 200 hello, got %d %q", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
// Outsider : 404 (pas d'énumération).
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/content", outsiderToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "content-outsider")
|
||||
|
||||
// Metadata-only sans octets : 404 (jamais uploadé).
|
||||
bareID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(owner, bareID, "vide.txt", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert bare: %v", err)
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+bareID+"/content", ownerToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "content-no-bytes")
|
||||
|
||||
// ID invalide → 404 ; sans token → 401.
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/not-hex/content", ownerToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "content-invalid-id")
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/content", "", nil, "")
|
||||
if rec.Code != http.StatusUnauthorized {
|
||||
t.Errorf("content-sans-token: attendu 401, got %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesHasContentLazy(t *testing.T) {
|
||||
r, store, repo := setup(t)
|
||||
ownerDevice := repository.NewID()
|
||||
ownerToken, owner := registerAndLogin(t, r, repo, testUserUsername(ownerDevice, "hc"), "hascontent-password", ownerDevice)
|
||||
|
||||
getHasContent := func(fileID, label string) bool {
|
||||
t.Helper()
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID, ownerToken, nil, "")
|
||||
env := expectOK(t, rec, label)
|
||||
var dto struct {
|
||||
HasContent bool `json:"hasContent"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &dto); err != nil {
|
||||
t.Fatalf("%s: unmarshal: %v", label, err)
|
||||
}
|
||||
return dto.HasContent
|
||||
}
|
||||
|
||||
// Upload avec octets → TRUE ; metadata-only → FALSE.
|
||||
fileID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(owner, fileID, "note.txt", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert file: %v", err)
|
||||
}
|
||||
if getHasContent(fileID, "hascontent-bare") {
|
||||
t.Errorf("metadata-only : hasContent attendu false")
|
||||
}
|
||||
rec := uploadMultipartToResource(t, r, ownerToken, fileID, "note.txt", []byte("hello"))
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("upload ciblé: attendu 200, got %d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if !getHasContent(fileID, "hascontent-uploaded") {
|
||||
t.Errorf("après upload : hasContent attendu true")
|
||||
}
|
||||
|
||||
// Suppression manuelle sous UPLOAD_DIR puis GET content → 404 + heal FALSE.
|
||||
matches, _ := filepath.Glob(filepath.Join(store.UploadDir, owner, fileID+".*"))
|
||||
if len(matches) == 0 {
|
||||
t.Fatalf("fichier physique introuvable sous %s", store.UploadDir)
|
||||
}
|
||||
if err := os.Remove(matches[0]); err != nil {
|
||||
t.Fatalf("remove physique: %v", err)
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/content", ownerToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "hascontent-missing-bytes")
|
||||
if getHasContent(fileID, "hascontent-healed-down") {
|
||||
t.Errorf("octets disparus : hasContent attendu false après heal")
|
||||
}
|
||||
|
||||
// Octets replacés à la main sur une ligne FALSE → GET content 200 + heal TRUE.
|
||||
bareID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(owner, bareID, "manuel.txt", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert bare: %v", err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(store.UploadDir, owner), 0o700); err != nil {
|
||||
t.Fatalf("mkdir: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(store.UploadDir, owner, bareID+".txt"), []byte("hi"), 0o600); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+bareID+"/content", ownerToken, nil, "")
|
||||
if rec.Code != http.StatusOK || rec.Body.String() != "hi" {
|
||||
t.Fatalf("content-heal-up: attendu 200 hi, got %d %q", rec.Code, rec.Body.String())
|
||||
}
|
||||
if !getHasContent(bareID, "hascontent-healed-up") {
|
||||
t.Errorf("octets constatés : hasContent attendu true après heal")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesSharesLinksListing(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
ownerDevice := repository.NewID()
|
||||
ownerToken, owner := registerAndLogin(t, r, repo, testUserUsername(ownerDevice, "sl"), "shares-list-password", ownerDevice)
|
||||
granteeDevice := repository.NewID()
|
||||
granteeToken, grantee := registerAndLogin(t, r, repo, testUserUsername(granteeDevice, "sg"), "shares-list-password-b", granteeDevice)
|
||||
outsiderDevice := repository.NewID()
|
||||
outsiderToken, _ := registerAndLogin(t, r, repo, testUserUsername(outsiderDevice, "sh"), "shares-list-password-c", outsiderDevice)
|
||||
|
||||
fileID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(owner, fileID, "shared.txt", "", 4, nil, nil); err != nil {
|
||||
t.Fatalf("insert file: %v", err)
|
||||
}
|
||||
|
||||
// Vide au départ (pas d'erreur).
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/shares", ownerToken, nil, "")
|
||||
env := expectOK(t, rec, "shares-empty")
|
||||
var emptyShares []map[string]any
|
||||
if err := json.Unmarshal(env.Data, &emptyShares); err != nil {
|
||||
t.Fatalf("shares-empty: unmarshal: %v", err)
|
||||
}
|
||||
if len(emptyShares) != 0 {
|
||||
t.Errorf("shares-empty = %d grants, attendu 0", len(emptyShares))
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/links", ownerToken, nil, "")
|
||||
env = expectOK(t, rec, "links-empty")
|
||||
var emptyLinks []map[string]any
|
||||
if err := json.Unmarshal(env.Data, &emptyLinks); err != nil {
|
||||
t.Fatalf("links-empty: unmarshal: %v", err)
|
||||
}
|
||||
if len(emptyLinks) != 0 {
|
||||
t.Errorf("links-empty = %d links, attendu 0", len(emptyLinks))
|
||||
}
|
||||
|
||||
// Un grant + un lien côté serveur (comme après des ops `share`/`create_link`).
|
||||
if err := repo.Shares.Upsert(fileID, grantee, "editor", true, nil, owner); err != nil {
|
||||
t.Fatalf("share: %v", err)
|
||||
}
|
||||
linkToken := repository.NewID()
|
||||
if err := repo.Shares.CreateLink(linkToken, fileID, "viewer", nil, owner); err != nil {
|
||||
t.Fatalf("create link: %v", err)
|
||||
}
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/shares", ownerToken, nil, "")
|
||||
env = expectOK(t, rec, "shares-owner")
|
||||
var grants []struct {
|
||||
GranteeUserID string `json:"granteeUserId"`
|
||||
Username string `json:"username"`
|
||||
Access string `json:"access"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &grants); err != nil {
|
||||
t.Fatalf("shares-owner: unmarshal: %v", err)
|
||||
}
|
||||
if len(grants) != 1 || grants[0].GranteeUserID != grantee || grants[0].Access != "editor" || grants[0].Username == "" {
|
||||
t.Errorf("shares-owner = %+v, attendu 1 grant editor du grantee", grants)
|
||||
}
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/links", ownerToken, nil, "")
|
||||
env = expectOK(t, rec, "links-owner")
|
||||
var links []struct {
|
||||
Token string `json:"token"`
|
||||
Access string `json:"access"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &links); err != nil {
|
||||
t.Fatalf("links-owner: unmarshal: %v", err)
|
||||
}
|
||||
if len(links) != 1 || links[0].Token != linkToken || links[0].Access != "viewer" {
|
||||
t.Errorf("links-owner = %+v, attendu 1 lien viewer", links)
|
||||
}
|
||||
|
||||
// Granté et outsider : 404 (pas d'énumération).
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/shares", granteeToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "shares-grantee")
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/links", granteeToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "links-grantee")
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/shares", outsiderToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "shares-outsider")
|
||||
|
||||
// ID invalide → 404.
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/not-hex/shares", ownerToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "shares-invalid-id")
|
||||
}
|
||||
|
||||
func TestFilesPlacementsListing(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
ownerDevice := repository.NewID()
|
||||
ownerToken, owner := registerAndLogin(t, r, repo, testUserUsername(ownerDevice, "pl"), "placements-password", ownerDevice)
|
||||
granteeDevice := repository.NewID()
|
||||
granteeToken, grantee := registerAndLogin(t, r, repo, testUserUsername(granteeDevice, "pg"), "placements-password-b", granteeDevice)
|
||||
outsiderDevice := repository.NewID()
|
||||
outsiderToken, _ := registerAndLogin(t, r, repo, testUserUsername(outsiderDevice, "ph"), "placements-password-c", outsiderDevice)
|
||||
|
||||
fileID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(owner, fileID, "placed.txt", "", 4, nil, nil); err != nil {
|
||||
t.Fatalf("insert file: %v", err)
|
||||
}
|
||||
|
||||
// Sans report : seule l'entrée serveur (hasContent false, metadata-only).
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/placements", ownerToken, nil, "")
|
||||
env := expectOK(t, rec, "placements-server-only")
|
||||
var only []map[string]any
|
||||
if err := json.Unmarshal(env.Data, &only); err != nil {
|
||||
t.Fatalf("placements-server-only: unmarshal: %v", err)
|
||||
}
|
||||
if len(only) != 1 || only[0]["locationType"] != "server" || only[0]["hasBytes"] != false {
|
||||
t.Errorf("placements-server-only = %v, attendu 1 entrée serveur false", only)
|
||||
}
|
||||
|
||||
// Report via l'outbox (owner) : l'entrée device apparaît.
|
||||
reportBody := fmt.Sprintf(
|
||||
`{"operations":[{"operation_id":%q,"resource_id":%q,"resource_type":"file","operation":"report_bytes","payload":{"hasBytes":true}}]}`,
|
||||
repository.NewID(), fileID,
|
||||
)
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/sync/ops", ownerToken, []byte(reportBody), "application/json")
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("sync/ops report: attendu 200, got %d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/placements", ownerToken, nil, "")
|
||||
env = expectOK(t, rec, "placements-owner")
|
||||
var entries []struct {
|
||||
LocationType string `json:"locationType"`
|
||||
DeviceID string `json:"deviceId"`
|
||||
HasBytes bool `json:"hasBytes"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &entries); err != nil {
|
||||
t.Fatalf("placements-owner: unmarshal: %v", err)
|
||||
}
|
||||
if len(entries) != 2 || entries[0].LocationType != "server" {
|
||||
t.Fatalf("placements-owner = %+v, attendu serveur + 1 device", entries)
|
||||
}
|
||||
if entries[1].LocationType != "mobile" || !entries[1].HasBytes || entries[1].DeviceID != ownerDevice {
|
||||
t.Errorf("placements-owner[1] = %+v, attendu device owner true", entries[1])
|
||||
}
|
||||
|
||||
// Granté et outsider : 404 (owner-only, pas d'énumération).
|
||||
if err := repo.Shares.Upsert(fileID, grantee, "viewer", false, nil, owner); err != nil {
|
||||
t.Fatalf("share: %v", err)
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/placements", granteeToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "placements-grantee")
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/placements", outsiderToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "placements-outsider")
|
||||
|
||||
// ID invalide → 404.
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/not-hex/placements", ownerToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "placements-invalid-id")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/kazier/backend/pkg/api"
|
||||
"github.com/kazier/backend/service"
|
||||
)
|
||||
|
||||
// groupIDParam validates a 32-hex group id path param (404, never 400 —
|
||||
// group ids are unguessable, no enumeration).
|
||||
func groupIDParam(c *gin.Context, name string) (string, bool) {
|
||||
id := c.Param(name)
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "group not found")
|
||||
return "", false
|
||||
}
|
||||
return id, true
|
||||
}
|
||||
|
||||
// GroupsCreate stores an owned group : POST /groups {name}.
|
||||
func GroupsCreate(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&body); err != nil {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid JSON body")
|
||||
return
|
||||
}
|
||||
dto, err := Store.CreateGroup(c.GetString(UserIDKey), body.Name)
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrInvalidUpload) {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid group name")
|
||||
return
|
||||
}
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, dto)
|
||||
}
|
||||
|
||||
// GroupsList returns owned + joined groups : GET /groups.
|
||||
func GroupsList(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
dtos, err := Store.ListGroups(c.GetString(UserIDKey))
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, dtos)
|
||||
}
|
||||
|
||||
// GroupMembersAdd adds an existing user : POST /groups/:id/members {userId}.
|
||||
func GroupMembersAdd(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
groupID, ok := groupIDParam(c, "id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
UserID string `json:"userId"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&body); err != nil || !deviceIDPattern.MatchString(body.UserID) {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid `userId`")
|
||||
return
|
||||
}
|
||||
if err := Store.AddGroupMember(c.GetString(UserIDKey), groupID, body.UserID); err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, gin.H{"id": groupID})
|
||||
}
|
||||
|
||||
// GroupMembersRemove removes a membership :
|
||||
// DELETE /groups/:id/members/:userId.
|
||||
func GroupMembersRemove(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
groupID, ok := groupIDParam(c, "id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
memberID, ok := groupIDParam(c, "userId")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := Store.RemoveGroupMember(c.GetString(UserIDKey), groupID, memberID); err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, gin.H{"id": groupID})
|
||||
}
|
||||
|
||||
// GroupsDelete removes an owned group : DELETE /groups/:id.
|
||||
func GroupsDelete(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
groupID, ok := groupIDParam(c, "id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := Store.DeleteGroup(c.GetString(UserIDKey), groupID); err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, gin.H{"id": groupID})
|
||||
}
|
||||
|
||||
// GroupMembersList returns memberships of an owned group :
|
||||
// GET /groups/:id/members.
|
||||
func GroupMembersList(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
groupID, ok := groupIDParam(c, "id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
members, err := Store.ListMembers(c.GetString(UserIDKey), groupID)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, members)
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package handlers_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// V2-6 : CRUD groupes + membres via HTTP, scoping owner, 404 sans énumération.
|
||||
func TestGroupsHttpFlow(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
ownerDevice := repository.NewID()
|
||||
ownerToken, _ := registerAndLogin(t, r, repo, testUserUsername(ownerDevice, "go"), "groups-http-password", ownerDevice)
|
||||
otherDevice := repository.NewID()
|
||||
otherToken, _ := registerAndLogin(t, r, repo, testUserUsername(otherDevice, "gx"), "groups-http-password-b", otherDevice)
|
||||
|
||||
// Création (nom trimé).
|
||||
rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/groups", ownerToken, []byte(`{"name":" Famille "}`), "application/json")
|
||||
env := expectOK(t, rec, "groups-create")
|
||||
var created struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Role string `json:"role"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &created); err != nil {
|
||||
t.Fatalf("groups-create: unmarshal: %v", err)
|
||||
}
|
||||
if created.Name != "Famille" || created.Role != "owner" {
|
||||
t.Errorf("groups-create = %+v", created)
|
||||
}
|
||||
// Nom vide → 400.
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/groups", ownerToken, []byte(`{"name":" "}`), "application/json")
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "groups-create-blank")
|
||||
|
||||
// Liste owner (1, owner) ; tiers (0).
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/groups", ownerToken, nil, "")
|
||||
env = expectOK(t, rec, "groups-list-owner")
|
||||
var listed []map[string]any
|
||||
if err := json.Unmarshal(env.Data, &listed); err != nil {
|
||||
t.Fatalf("groups-list: unmarshal: %v", err)
|
||||
}
|
||||
if len(listed) != 1 {
|
||||
t.Errorf("groups-list-owner = %d, attendu 1", len(listed))
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/groups", otherToken, nil, "")
|
||||
env = expectOK(t, rec, "groups-list-other")
|
||||
listed = nil
|
||||
if err := json.Unmarshal(env.Data, &listed); err != nil {
|
||||
t.Fatalf("groups-list-other: unmarshal: %v", err)
|
||||
}
|
||||
if len(listed) != 0 {
|
||||
t.Errorf("groups-list-other = %d, attendu 0", len(listed))
|
||||
}
|
||||
|
||||
// Ajout membre : besoin d'un user existant — on le crée via un login.
|
||||
memberDevice := repository.NewID()
|
||||
_, memberID := registerAndLogin(t, r, repo, testUserUsername(memberDevice, "gm"), "groups-http-password-c", memberDevice)
|
||||
rec, _ = doRequest(
|
||||
t, r, http.MethodPost, "/api/v1/groups/"+created.ID+"/members", ownerToken,
|
||||
[]byte(fmt.Sprintf(`{"userId":%q}`, memberID)), "application/json",
|
||||
)
|
||||
expectOK(t, rec, "groups-add-member")
|
||||
// Liste membres (owner) : le membre ajouté ; tiers → 404.
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/groups/"+created.ID+"/members", ownerToken, nil, "")
|
||||
envMembers := expectOK(t, rec, "groups-list-members")
|
||||
var members []map[string]any
|
||||
if err := json.Unmarshal(envMembers.Data, &members); err != nil {
|
||||
t.Fatalf("groups-list-members: unmarshal: %v", err)
|
||||
}
|
||||
if len(members) != 1 || members[0]["id"] != memberID {
|
||||
t.Errorf("groups-list-members = %v, attendu 1 membre", members)
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/groups/"+created.ID+"/members", otherToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "groups-list-members-other")
|
||||
// Non-owner → 404.
|
||||
rec, _ = doRequest(
|
||||
t, r, http.MethodPost, "/api/v1/groups/"+created.ID+"/members", otherToken,
|
||||
[]byte(fmt.Sprintf(`{"userId":%q}`, memberID)), "application/json",
|
||||
)
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "groups-add-other")
|
||||
// Membre inconnu → GRANTEE_NOT_FOUND.
|
||||
rec, _ = doRequest(
|
||||
t, r, http.MethodPost, "/api/v1/groups/"+created.ID+"/members", ownerToken,
|
||||
[]byte(fmt.Sprintf(`{"userId":%q}`, repository.NewID())), "application/json",
|
||||
)
|
||||
expectError(t, rec, http.StatusNotFound, "GRANTEE_NOT_FOUND", "groups-add-unknown")
|
||||
|
||||
// Groupe inconnu → 404 partout (pas d'énumération).
|
||||
unknown := repository.NewID()
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/groups/"+unknown+"/members", ownerToken,
|
||||
[]byte(fmt.Sprintf(`{"userId":%q}`, memberID)), "application/json")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "groups-add-unknown-group")
|
||||
rec, _ = doRequest(t, r, http.MethodDelete, "/api/v1/groups/"+unknown, ownerToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "groups-delete-unknown")
|
||||
rec, _ = doRequest(t, r, http.MethodDelete, "/api/v1/groups/not-hex", ownerToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "groups-invalid-id")
|
||||
|
||||
// Retrait puis suppression.
|
||||
rec, _ = doRequest(t, r, http.MethodDelete, "/api/v1/groups/"+created.ID+"/members/"+memberID, ownerToken, nil, "")
|
||||
expectOK(t, rec, "groups-remove-member")
|
||||
rec, _ = doRequest(t, r, http.MethodDelete, "/api/v1/groups/"+created.ID, otherToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "groups-delete-other")
|
||||
rec, _ = doRequest(t, r, http.MethodDelete, "/api/v1/groups/"+created.ID, ownerToken, nil, "")
|
||||
expectOK(t, rec, "groups-delete")
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package handlers
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/vaultdrop/backend/pkg/api"
|
||||
"github.com/kazier/backend/pkg/api"
|
||||
)
|
||||
|
||||
func Health(c *gin.Context) {
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
package handlers_test
|
||||
|
||||
// Tests contrat LOT 6 : ordre statique/param (§1), validation 32-hex et
|
||||
// curseur `after` stricts (A7), validation POST /devices.
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/kazier/backend/handlers"
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// TestFoldersRouteServesListNotParam (§1) : GET /files/folders via le routeur
|
||||
// atteint FoldersList (200), jamais FilesGet avec id="folders".
|
||||
func TestFoldersRouteServesListNotParam(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "l6f"), "lot6-test-password", device)
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/folders", token, nil, "")
|
||||
expectOK(t, rec, "folders-static-wins")
|
||||
}
|
||||
|
||||
// TestFilesGetRejectsFoldersLiteral (§1) : si le routeur tombait un jour sur
|
||||
// FilesGet avec id="folders", le garde 32-hex répond 404 (jamais 200, jamais
|
||||
// 500). Appel direct du handler pour forcer le cas paramétré.
|
||||
func TestFilesGetRejectsFoldersLiteral(t *testing.T) {
|
||||
setup(t)
|
||||
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/files/folders", nil)
|
||||
c.Params = gin.Params{{Key: "id", Value: "folders"}}
|
||||
c.Set(handlers.UserIDKey, "00000000000000000000000000000001")
|
||||
|
||||
handlers.FilesGet(c)
|
||||
|
||||
expectError(t, w, http.StatusNotFound, "NOT_FOUND", "files-get-folders-literal")
|
||||
}
|
||||
|
||||
// TestShareLinkMalformedToken400 (A7) : token non 32-hex → 400
|
||||
// INVALID_REQUEST (pas 404).
|
||||
func TestShareLinkMalformedToken400(t *testing.T) {
|
||||
r, _, _ := setup(t)
|
||||
for _, path := range []string{
|
||||
"/api/v1/shares/links/NOT-HEX",
|
||||
"/api/v1/shares/links/xyz",
|
||||
"/api/v1/shares/links/ABCDEF0123456789ABCDEF0123456789", // majuscules rejetées
|
||||
} {
|
||||
rec, _ := doRequest(t, r, http.MethodGet, path, "", nil, "")
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "share-malformed-token")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSyncPermissionsAfterValidation (A7) : garbage/négatif → 400, epoch-ms
|
||||
// 64-bit accepté.
|
||||
func TestSyncPermissionsAfterValidation(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "l6a"), "lot6-test-password", device)
|
||||
|
||||
for _, path := range []string{
|
||||
"/api/v1/sync/permissions?after=garbage",
|
||||
"/api/v1/sync/permissions?after=-5",
|
||||
"/api/v1/sync/permissions?after=12.5",
|
||||
"/api/v1/sync/permissions?after=",
|
||||
} {
|
||||
// "?after=" vide = curseur absent → 200 (seul le dernier cas).
|
||||
rec, _ := doRequest(t, r, http.MethodGet, path, token, nil, "")
|
||||
if path == "/api/v1/sync/permissions?after=" {
|
||||
expectOK(t, rec, "after-empty-ok")
|
||||
} else {
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "after-invalid:"+path)
|
||||
}
|
||||
}
|
||||
|
||||
// Epoch-ms au-delà de 2^31 (64-bit) → 200, pas de troncation/rejet.
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/sync/permissions?after=9999999999999", token, nil, "")
|
||||
expectOK(t, rec, "after-64bit-ok")
|
||||
}
|
||||
|
||||
// TestDevicesRegisterValidation : 32-hex malformé/absent → 400, valide → 200.
|
||||
func TestDevicesRegisterValidation(t *testing.T) {
|
||||
r, _, _ := setup(t)
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/devices", "", []byte(`{"deviceId":"NOT-HEX"}`), "application/json")
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_DEVICE_ID", "devices-malformed")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/devices", "", []byte(`{}`), "application/json")
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "devices-missing")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/devices", "", []byte(`{"deviceId":`+`"`+repository.NewID()+`"}`), "application/json")
|
||||
expectOK(t, rec, "devices-valid")
|
||||
}
|
||||
|
||||
// TestDevicesRegisterRateLimited : flood → 429 RATE_LIMITED (budget 300/min/IP,
|
||||
// cf. handlers/devices.go). Les requêtes invalides ne consomment pas le budget.
|
||||
func TestDevicesRegisterRateLimited(t *testing.T) {
|
||||
r, _, _ := setup(t)
|
||||
|
||||
hookCalls := 0
|
||||
handlers.OnDevicesRateLimited = func(ip string) { hookCalls++ }
|
||||
t.Cleanup(func() { handlers.OnDevicesRateLimited = nil })
|
||||
|
||||
// Les requêtes invalides (avant le compteur) ne doivent pas déclencher.
|
||||
rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/devices", "", []byte(`{"deviceId":"BAD"}`), "application/json")
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_DEVICE_ID", "devices-ratelimit-invalid")
|
||||
|
||||
for i := 0; i < 300; i++ {
|
||||
rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/devices", "", []byte(`{"deviceId":`+`"`+repository.NewID()+`"}`), "application/json")
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("enregistrement %d: attendu 200, got %d body=%s", i, rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/devices", "", []byte(`{"deviceId":`+`"`+repository.NewID()+`"}`), "application/json")
|
||||
expectError(t, rec, http.StatusTooManyRequests, "RATE_LIMITED", "devices-ratelimited")
|
||||
if hookCalls != 1 {
|
||||
t.Errorf("OnDevicesRateLimited appelé %d fois, attendu 1", hookCalls)
|
||||
}
|
||||
}
|
||||
@@ -2,13 +2,14 @@ package handlers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/vaultdrop/backend/pkg/api"
|
||||
"github.com/vaultdrop/backend/pkg/auth"
|
||||
"github.com/vaultdrop/backend/repository"
|
||||
"github.com/kazier/backend/pkg/api"
|
||||
"github.com/kazier/backend/pkg/auth"
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -19,6 +20,15 @@ const (
|
||||
// Auth issues/verifies device bearer tokens; set once at startup (cmd/server).
|
||||
var Auth *auth.Manager
|
||||
|
||||
// EnforceDeviceExists durcit RequireAuth (FEEDBACK A15) : quand true, le
|
||||
// device_id porté par le token doit exister en base (POST /devices), sinon la
|
||||
// requête est rejetée en 401 et le rejet est loggé côté serveur.
|
||||
// Défaut false (V1) : le device reste un claim porté NON autorisant —
|
||||
// l'autorisation vient du subject user_id + existence du compte. Passer à true
|
||||
// coûte une lecture DB par requête authentifiée ; ne pas activer sans mesurer
|
||||
// sur les routes à fort trafic (snapshot/sync).
|
||||
var EnforceDeviceExists = false
|
||||
|
||||
// RequireAuth authenticates the bearer paseto token. Le subject (user_id)
|
||||
// autorise — c'est la clé de scoping de toutes les ressources ; device_id est
|
||||
// porté (non autorisant) pour l'idempotence outbox et les jobs OCR. Le compte
|
||||
@@ -26,7 +36,13 @@ var Auth *auth.Manager
|
||||
func RequireAuth(c *gin.Context) {
|
||||
|
||||
header := c.GetHeader("Authorization")
|
||||
token, found := strings.CutPrefix(header, "Bearer ")
|
||||
token := ""
|
||||
found := false
|
||||
// Préfixe Bearer insensible à la casse (cf. FEEDBACK A7).
|
||||
if len(header) > 7 && strings.EqualFold(header[:7], "Bearer ") {
|
||||
token = strings.TrimSpace(header[7:])
|
||||
found = token != ""
|
||||
}
|
||||
|
||||
if Auth == nil || !found || token == "" {
|
||||
api.Error(c, http.StatusUnauthorized, "UNAUTHORIZED", "missing bearer token")
|
||||
@@ -44,7 +60,7 @@ func RequireAuth(c *gin.Context) {
|
||||
// Le store peut être nil dans les tests middleware purs ; sinon on vérifie
|
||||
// que le compte existe toujours (supprimé → accès refusé).
|
||||
if Store != nil && Store.Repository != nil {
|
||||
_, err := Store.Repository.Users.GetByID(identity.UserID)
|
||||
user, err := Store.Repository.Users.GetByID(identity.UserID)
|
||||
if errors.Is(err, repository.ErrNotFound) {
|
||||
api.Error(c, http.StatusUnauthorized, "UNAUTHORIZED", "account no longer exists")
|
||||
c.Abort()
|
||||
@@ -55,6 +71,30 @@ func RequireAuth(c *gin.Context) {
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
// V3 S6 : révocation — un changement de mot de passe (bump de
|
||||
// token_version) invalide tous les tokens antérieurs, même non expirés.
|
||||
if identity.TokenVersion != user.TokenVersion {
|
||||
log.Printf("auth: token révoqué rejeté (user=%s tv=%d, attendu %d)", identity.UserID, identity.TokenVersion, user.TokenVersion)
|
||||
api.Error(c, http.StatusUnauthorized, "UNAUTHORIZED", "token revoked")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if EnforceDeviceExists && Store != nil && Store.Repository != nil {
|
||||
// Verify garantit déjà le format 32-hex : Exists reçoit une entrée saine.
|
||||
exists, err := Store.Repository.Devices.Exists(identity.DeviceID)
|
||||
if err != nil {
|
||||
api.Error(c, http.StatusInternalServerError, "INTERNAL", "could not check device")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
if !exists {
|
||||
log.Printf("auth: token d'un device inconnu rejeté (user=%s device=%s)", identity.UserID, identity.DeviceID)
|
||||
api.Error(c, http.StatusUnauthorized, "UNAUTHORIZED", "unknown device")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.Set(UserIDKey, identity.UserID)
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const middlewareUnknownDevice = "ffffffffffffffffffffffffffffffff"
|
||||
|
||||
// Par défaut (EnforceDeviceExists=false), un token portant un device inconnu
|
||||
// passe le middleware : le device est un claim non autorisant (V1).
|
||||
func TestRequireAuthUnknownDevicePassesByDefault(t *testing.T) {
|
||||
setTestAuth(t)
|
||||
repo := setTestStore(t)
|
||||
userID := createAdmin(t, repo, "admin", authTestPassword)
|
||||
r := newTestRouterForAuth()
|
||||
|
||||
if EnforceDeviceExists {
|
||||
t.Fatal("EnforceDeviceExists devrait être false par défaut")
|
||||
}
|
||||
|
||||
signed, err := Auth.Issue(userID, middlewareUnknownDevice)
|
||||
if err != nil {
|
||||
t.Fatalf("issue: %v", err)
|
||||
}
|
||||
rec := doAuth(t, r, http.MethodGet, "/api/v1/files", signed, "")
|
||||
if rec.Code == 401 {
|
||||
t.Fatalf("device inconnu rejeté alors que EnforceDeviceExists=false: %s", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// Avec EnforceDeviceExists=true, le device inconnu est rejeté en 401 (et
|
||||
// loggé), le device enregistré passe.
|
||||
func TestRequireAuthEnforceDeviceExists(t *testing.T) {
|
||||
setTestAuth(t)
|
||||
repo := setTestStore(t)
|
||||
userID := createAdmin(t, repo, "admin", authTestPassword)
|
||||
_ = repo.Devices.Upsert(authTestDevice)
|
||||
r := newTestRouterForAuth()
|
||||
|
||||
old := EnforceDeviceExists
|
||||
EnforceDeviceExists = true
|
||||
t.Cleanup(func() { EnforceDeviceExists = old })
|
||||
|
||||
unknown, _ := Auth.Issue(userID, middlewareUnknownDevice)
|
||||
rec := doAuth(t, r, http.MethodGet, "/api/v1/files", unknown, "")
|
||||
if rec.Code != 401 {
|
||||
t.Fatalf("device inconnu: status %d body %s, attendu 401", rec.Code, rec.Body.String())
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), "UNAUTHORIZED") {
|
||||
t.Errorf("code attendu UNAUTHORIZED, got %s", rec.Body.String())
|
||||
}
|
||||
|
||||
known, _ := Auth.Issue(userID, authTestDevice)
|
||||
rec = doAuth(t, r, http.MethodGet, "/api/v1/files", known, "")
|
||||
if rec.Code == 401 {
|
||||
t.Fatalf("device enregistré rejeté avec EnforceDeviceExists=true: %s", rec.Body.String())
|
||||
}
|
||||
}
|
||||
@@ -5,8 +5,8 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/vaultdrop/backend/pkg/api"
|
||||
"github.com/vaultdrop/backend/repository"
|
||||
"github.com/kazier/backend/pkg/api"
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
type ocrJobRequest struct {
|
||||
|
||||
@@ -11,11 +11,11 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/vaultdrop/backend/dbtest"
|
||||
"github.com/vaultdrop/backend/handlers"
|
||||
"github.com/vaultdrop/backend/pkg/auth"
|
||||
"github.com/vaultdrop/backend/repository"
|
||||
"github.com/vaultdrop/backend/service"
|
||||
"github.com/kazier/backend/dbtest"
|
||||
"github.com/kazier/backend/handlers"
|
||||
"github.com/kazier/backend/pkg/auth"
|
||||
"github.com/kazier/backend/repository"
|
||||
"github.com/kazier/backend/service"
|
||||
)
|
||||
|
||||
type stubEngine struct {
|
||||
@@ -40,8 +40,15 @@ func setupOcr(t *testing.T) (*gin.Engine, string, *repository.Repository) {
|
||||
manager, _ := auth.NewManager("test-secret")
|
||||
handlers.Auth = manager
|
||||
handlers.Store = store
|
||||
handlers.ResetDevicesRateLimit()
|
||||
handlers.Ocr = service.NewOcr(repo, uploadDir, "fra+eng", stubEngine{text: "HELLO OCR"})
|
||||
|
||||
// Worker OCR borné — le handler n'enfile plus que le job, le traitement
|
||||
// tourne sur un worker de contexte.
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
go handlers.Ocr.Run(ctx, 1)
|
||||
t.Cleanup(cancel)
|
||||
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
handlers.RegisterRoutes(r)
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
package handlers_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/jpeg"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// previewImageBytes génère un JPEG 640x480 uni (décodable sans fixture).
|
||||
func previewImageBytes(t *testing.T) []byte {
|
||||
t.Helper()
|
||||
img := image.NewRGBA(image.Rect(0, 0, 640, 480))
|
||||
for y := 0; y < 480; y++ {
|
||||
for x := 0; x < 640; x++ {
|
||||
img.Set(x, y, color.RGBA{uint8(x % 256), 128, 64, 255})
|
||||
}
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
if err := jpeg.Encode(&buf, img, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
// TestFilesPreview : JPEG 200 + image/jpeg pour une image, NO_PREVIEW pour
|
||||
// un txt, NOT_FOUND si trashé/étranger. L'upload HTTP de test est capé à
|
||||
// 10 o : le physique est écrit directement (même layout UPLOAD_DIR).
|
||||
func TestFilesPreview(t *testing.T) {
|
||||
r, store, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, userID := registerAndLogin(t, r, repo, testUserUsername(device, "pv"), "preview-test-password", device)
|
||||
|
||||
createdID := seedPhysical(t, store.UploadDir, repo, userID, "photo.jpg", previewImageBytes(t))
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/"+createdID+"/preview", token, nil, "")
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("preview: attendu 200, got %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if ct := rec.Header().Get("Content-Type"); ct != "image/jpeg" {
|
||||
t.Fatalf("content-type = %q, attendu image/jpeg", ct)
|
||||
}
|
||||
if _, err := jpeg.Decode(bytes.NewReader(rec.Body.Bytes())); err != nil {
|
||||
t.Fatalf("corps non-JPEG: %v", err)
|
||||
}
|
||||
|
||||
// Txt → NO_PREVIEW (404 typé, l'UI montre l'icône).
|
||||
txtID := seedPhysical(t, store.UploadDir, repo, userID, "notes.txt", []byte("hello"))
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+txtID+"/preview", token, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NO_PREVIEW", "preview-txt")
|
||||
|
||||
// Trashé → NOT_FOUND (invisible d'abord, pas NO_PREVIEW).
|
||||
if err := repo.Resources.TrashResource(userID, createdID); err != nil {
|
||||
t.Fatalf("trash: %v", err)
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+createdID+"/preview", token, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "preview-trashed")
|
||||
|
||||
// Étranger → NOT_FOUND.
|
||||
deviceB := repository.NewID()
|
||||
tokenB, _ := registerAndLogin(t, r, repo, testUserUsername(deviceB, "pb"), "preview-test-password", deviceB)
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+txtID+"/preview", tokenB, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "preview-foreign")
|
||||
}
|
||||
|
||||
// seedPhysical insère la métadonnée + écrit les octets (même layout que
|
||||
// l'upload, sans passer par le multipart capé des tests).
|
||||
func seedPhysical(t *testing.T, uploadDir string, repo *repository.Repository, userID, name string, content []byte) string {
|
||||
t.Helper()
|
||||
id := repository.NewID()
|
||||
ext := ""
|
||||
if i := strings.LastIndex(name, "."); i >= 0 {
|
||||
ext = name[i+1:]
|
||||
}
|
||||
var extPtr *string
|
||||
if ext != "" {
|
||||
extPtr = &ext
|
||||
}
|
||||
mime := "application/octet-stream"
|
||||
if ext == "jpg" {
|
||||
mime = "image/jpeg"
|
||||
}
|
||||
if err := repo.Resources.InsertFile(userID, id, name, "", int64(len(content)), &mime, extPtr); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
dir := filepath.Join(uploadDir, userID)
|
||||
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||
t.Fatalf("mkdir: %v", err)
|
||||
}
|
||||
dest := id
|
||||
if ext != "" {
|
||||
dest = id + "." + ext
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, dest), content, 0o600); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
if err := repo.Resources.SetHasContent(userID, id, true); err != nil {
|
||||
t.Fatalf("has_content: %v", err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package handlers_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// TestRelationsEndpoints : GET /files/:id/relations (réconciliation à la
|
||||
// lecture) + POST verdict (confirmé/infirmé, 400/404).
|
||||
func TestRelationsEndpoints(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, userID := registerAndLogin(t, r, repo, testUserUsername(device, "rl"), "relations-test-password", device)
|
||||
|
||||
session := repository.NewID()
|
||||
page1, page2 := repository.NewID(), repository.NewID()
|
||||
for _, id := range []string{page1, page2} {
|
||||
if err := repo.Resources.InsertFile(userID, id, id+".png", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
if err := repo.Resources.SetScanSession(userID, id, session); err != nil {
|
||||
t.Fatalf("session: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/"+page1+"/relations", token, nil, "")
|
||||
env := expectOK(t, rec, "relations-get")
|
||||
var rels []struct {
|
||||
ID string `json:"id"`
|
||||
OtherID string `json:"otherId"`
|
||||
Type string `json:"type"`
|
||||
Confidence float64 `json:"confidence"`
|
||||
CreatedBy string `json:"createdBy"`
|
||||
OtherName string `json:"otherName"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &rels); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if len(rels) != 1 || rels[0].Type != "same_scan" || rels[0].OtherID != page2 {
|
||||
t.Fatalf("relations inattendues: %+v", rels)
|
||||
}
|
||||
|
||||
// Verdict infirmé : arête conservée à 0.0, origine user.
|
||||
body := fmt.Sprintf(`{"otherId":%q,"linkType":"same_scan","confirmed":false}`, page2)
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/files/"+page1+"/relations/verdict", token, []byte(body), "application/json")
|
||||
env = expectOK(t, rec, "relations-verdict")
|
||||
rels = nil
|
||||
if err := json.Unmarshal(env.Data, &rels); err != nil {
|
||||
t.Fatalf("unmarshal verdict: %v", err)
|
||||
}
|
||||
if len(rels) != 1 || rels[0].Confidence != 0 || rels[0].CreatedBy != "user" {
|
||||
t.Fatalf("verdict inattendu: %+v", rels)
|
||||
}
|
||||
|
||||
// Type inconnu → 400 ; paire non-hex → 400 ; étranger → 404.
|
||||
for _, tc := range []struct {
|
||||
name, body string
|
||||
status int
|
||||
code string
|
||||
}{
|
||||
{"type", fmt.Sprintf(`{"otherId":%q,"linkType":"copains","confirmed":true}`, page2), http.StatusBadRequest, "INVALID_REQUEST"},
|
||||
{"hex", `{"otherId":"nope","linkType":"same_scan","confirmed":true}`, http.StatusBadRequest, "INVALID_REQUEST"},
|
||||
} {
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/files/"+page1+"/relations/verdict", token, []byte(tc.body), "application/json")
|
||||
expectError(t, rec, tc.status, tc.code, "relations-verdict-400:"+tc.name)
|
||||
}
|
||||
|
||||
deviceB := repository.NewID()
|
||||
tokenB, _ := registerAndLogin(t, r, repo, testUserUsername(deviceB, "rb"), "relations-test-password", deviceB)
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+page1+"/relations", tokenB, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "relations-foreign")
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/files/"+page1+"/relations/verdict", tokenB,
|
||||
[]byte(fmt.Sprintf(`{"otherId":%q,"linkType":"same_scan","confirmed":true}`, page2)), "application/json")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "relations-verdict-foreign")
|
||||
|
||||
// Trashé → 404 (l'autre bout trashé disparaît aussi de la liste).
|
||||
if err := repo.Resources.TrashResource(userID, page2); err != nil {
|
||||
t.Fatalf("trash: %v", err)
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+page1+"/relations", token, nil, "")
|
||||
env = expectOK(t, rec, "relations-trashed-other")
|
||||
rels = nil
|
||||
if err := json.Unmarshal(env.Data, &rels); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if len(rels) != 0 {
|
||||
t.Fatalf("bout trashé encore listé: %+v", rels)
|
||||
}
|
||||
}
|
||||
+34
-11
@@ -2,11 +2,10 @@ package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/vaultdrop/backend/pkg/api"
|
||||
"github.com/kazier/backend/pkg/api"
|
||||
)
|
||||
|
||||
// RegisterRoutes wires the full /api/v1 surface (public + protected).
|
||||
@@ -26,14 +25,45 @@ func RegisterRoutes(r *gin.Engine) {
|
||||
protected.Use(RequireAuth)
|
||||
{
|
||||
protected.GET("/files", FilesList)
|
||||
// Verrou LOT 6 §1 : les routes statiques restent déclarées AVANT
|
||||
// /files/:id (Gin priorise déjà le statique sur le paramétré, mais
|
||||
// l'ordre source verrouille l'intention : /files/folders ne doit
|
||||
// jamais être avalé par FilesGet avec id="folders").
|
||||
protected.GET("/files/search", FilesSearch)
|
||||
protected.GET("/files/:id", FilesGet)
|
||||
protected.DELETE("/files/:id", FilesDelete)
|
||||
protected.GET("/files/folders", FoldersList)
|
||||
protected.GET("/files/:id", FilesGet)
|
||||
protected.GET("/files/:id/ocr", FilesGetOcr)
|
||||
protected.GET("/files/:id/facts", FilesGetFacts)
|
||||
protected.PATCH("/files/:id/facts", FilesCorrectFacts)
|
||||
protected.GET("/files/:id/content", FilesContent)
|
||||
protected.GET("/files/:id/preview", FilesPreview)
|
||||
protected.GET("/files/:id/shares", SharesList)
|
||||
protected.GET("/files/:id/links", LinksList)
|
||||
protected.GET("/files/:id/placements", PlacementsList)
|
||||
protected.GET("/files/:id/versions", VersionsList)
|
||||
protected.GET("/files/:id/relations", RelationsList)
|
||||
protected.POST("/files/:id/relations/verdict", RelationVerdict)
|
||||
protected.GET("/duplicates", DuplicatesList)
|
||||
protected.POST("/export", ExportFull)
|
||||
protected.POST("/files/:id/restore", FilesRestore)
|
||||
protected.POST("/files/:id/versions/:n/restore", VersionRestore)
|
||||
protected.GET("/trash", TrashList)
|
||||
protected.DELETE("/files/:id", FilesDelete)
|
||||
protected.POST("/files/upload", FilesUpload)
|
||||
|
||||
protected.GET("/users/resolve", ResolveUser)
|
||||
protected.PATCH("/users/me/password", ChangePassword)
|
||||
protected.GET("/users/me/usage", UsersUsage)
|
||||
|
||||
protected.GET("/devices", DevicesList)
|
||||
protected.DELETE("/devices/:id", DeviceRevoke)
|
||||
|
||||
protected.POST("/groups", GroupsCreate)
|
||||
protected.GET("/groups", GroupsList)
|
||||
protected.POST("/groups/:id/members", GroupMembersAdd)
|
||||
protected.GET("/groups/:id/members", GroupMembersList)
|
||||
protected.DELETE("/groups/:id/members/:userId", GroupMembersRemove)
|
||||
protected.DELETE("/groups/:id", GroupsDelete)
|
||||
|
||||
protected.POST("/ocr/jobs", OcrJobsCreate)
|
||||
protected.GET("/ocr/jobs/:id", OcrJobsGet)
|
||||
@@ -47,10 +77,3 @@ func RegisterRoutes(r *gin.Engine) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "route not found")
|
||||
})
|
||||
}
|
||||
|
||||
// userID lit la clé de scoping posée par RequireAuth. Un empty string est
|
||||
// impossible en conditions normales (le middleware l'a validé) ; garde-fou
|
||||
// pour un appel direct dans les tests.
|
||||
func userID(c *gin.Context) string {
|
||||
return strings.TrimSpace(c.GetString(UserIDKey))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
package handlers_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/kazier/backend/handlers"
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// TestPasswordChangeRevokesTokens : le bump de token_version invalide tous
|
||||
// les tokens antérieurs (V3 S6) — l'ancien token devient 401, un nouveau
|
||||
// login fonctionne.
|
||||
func TestPasswordChangeRevokesTokens(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "rv"), "revoke-test-password", device)
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files", token, nil, "")
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("token initial rejeté: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
body := `{"current_password":"revoke-test-password","new_password":"revoke-test-password-2"}`
|
||||
rec, _ = doRequest(t, r, http.MethodPatch, "/api/v1/users/me/password", token, []byte(body), "application/json")
|
||||
expectOK(t, rec, "password-change")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files", token, nil, "")
|
||||
expectError(t, rec, http.StatusUnauthorized, "UNAUTHORIZED", "revoked-token")
|
||||
|
||||
// Nouveau login (nouvelle version) : fonctionne.
|
||||
body = fmt.Sprintf(`{"username":%q,"password":"revoke-test-password-2","device_id":%q}`,
|
||||
testUserUsername(device, "rv"), device)
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/auth/login", "", []byte(body), "application/json")
|
||||
expectOK(t, rec, "relogin")
|
||||
var login struct {
|
||||
Data struct {
|
||||
Token string `json:"token"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &login); err != nil || login.Data.Token == "" {
|
||||
t.Fatalf("relogin sans token: %s", rec.Body.String())
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files", login.Data.Token, nil, "")
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("nouveau token rejeté: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestDevicesListAndRevoke : liste les devices du compte, révoque (A1) —
|
||||
// avec EnforceDeviceExists, le token du device révoqué devient 401 et son
|
||||
// login est refusé (ré-enregistrement requis).
|
||||
func TestDevicesListAndRevoke(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
handlers.EnforceDeviceExists = true
|
||||
defer func() { handlers.EnforceDeviceExists = false }()
|
||||
device := repository.NewID()
|
||||
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "dv"), "devices-test-password", device)
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/devices", token, nil, "")
|
||||
env := expectOK(t, rec, "devices-list")
|
||||
var devices []struct {
|
||||
DeviceID string `json:"deviceId"`
|
||||
LastSeenAt *int64 `json:"lastSeenAt"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &devices); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
found := false
|
||||
for _, d := range devices {
|
||||
if d.DeviceID == device {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("device absent de la liste: %+v", devices)
|
||||
}
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodDelete, "/api/v1/devices/"+device, token, nil, "")
|
||||
expectOK(t, rec, "devices-revoke")
|
||||
|
||||
// Token existant → 401 (device inconnu).
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files", token, nil, "")
|
||||
expectError(t, rec, http.StatusUnauthorized, "UNAUTHORIZED", "revoked-device-token")
|
||||
|
||||
// Login refusé (device inconnu, indistinguable — 401).
|
||||
body := fmt.Sprintf(`{"username":%q,"password":"devices-test-password","device_id":%q}`,
|
||||
testUserUsername(device, "dv"), device)
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/auth/login", "", []byte(body), "application/json")
|
||||
expectError(t, rec, http.StatusUnauthorized, "UNAUTHORIZED", "revoked-device-login")
|
||||
|
||||
// Révoquer l'inexistant / malformé → 404 (avec un token encore valide).
|
||||
deviceB := repository.NewID()
|
||||
tokenB, _ := registerAndLogin(t, r, repo, testUserUsername(deviceB, "dv2"), "devices-test-password", deviceB)
|
||||
rec, _ = doRequest(t, r, http.MethodDelete, "/api/v1/devices/"+repository.NewID(), tokenB, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "revoke-unknown")
|
||||
rec, _ = doRequest(t, r, http.MethodDelete, "/api/v1/devices/nope", tokenB, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "revoke-malformed")
|
||||
}
|
||||
|
||||
// TestUsageAndQuota : GET /users/me/usage reflète l'état, et un upload
|
||||
// au-delà du quota est rejeté en 413 QUOTA_EXCEEDED (V3 S6 A2).
|
||||
func TestUsageAndQuota(t *testing.T) {
|
||||
r, store, repo := setup(t)
|
||||
_ = store
|
||||
device := repository.NewID()
|
||||
token, userID := registerAndLogin(t, r, repo, testUserUsername(device, "qu"), "quota-test-password", device)
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/users/me/usage", token, nil, "")
|
||||
env := expectOK(t, rec, "usage-empty")
|
||||
var usage struct {
|
||||
UsedBytes int64 `json:"usedBytes"`
|
||||
QuotaBytes int64 `json:"quotaBytes"`
|
||||
FileCount int `json:"fileCount"`
|
||||
TrashCount int `json:"trashCount"`
|
||||
TrashedBytes int64 `json:"trashedBytes"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &usage); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if usage.UsedBytes != 0 || usage.QuotaBytes != 10737418240 || usage.FileCount != 0 {
|
||||
t.Fatalf("usage initial inattendu: %+v", usage)
|
||||
}
|
||||
|
||||
// Quota miniature (10 octets) → l'upload passe sous le seuil puis casse.
|
||||
if _, err := repo.Users.DB.Exec(`UPDATE users SET storage_quota_bytes = 10 WHERE id = $1`, userID); err != nil {
|
||||
t.Fatalf("shrink quota: %v", err)
|
||||
}
|
||||
rec = uploadMultipart(t, r, token, "", "a.txt", []byte("12345"))
|
||||
expectOK(t, rec, "usage-under-quota")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/users/me/usage", token, nil, "")
|
||||
env = expectOK(t, rec, "usage-after")
|
||||
usage = struct {
|
||||
UsedBytes int64 `json:"usedBytes"`
|
||||
QuotaBytes int64 `json:"quotaBytes"`
|
||||
FileCount int `json:"fileCount"`
|
||||
TrashCount int `json:"trashCount"`
|
||||
TrashedBytes int64 `json:"trashedBytes"`
|
||||
}{}
|
||||
if err := json.Unmarshal(env.Data, &usage); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if usage.UsedBytes != 5 || usage.FileCount != 1 {
|
||||
t.Fatalf("usage après upload: %+v", usage)
|
||||
}
|
||||
|
||||
rec = uploadMultipart(t, r, token, "", "b.txt", []byte("123456"))
|
||||
expectError(t, rec, http.StatusRequestEntityTooLarge, "QUOTA_EXCEEDED", "usage-over-quota")
|
||||
}
|
||||
@@ -1,12 +1,13 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/vaultdrop/backend/pkg/api"
|
||||
"github.com/vaultdrop/backend/repository"
|
||||
"github.com/kazier/backend/pkg/api"
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// ShareLinkGet resolves a public share link without authentication
|
||||
@@ -18,9 +19,18 @@ func ShareLinkGet(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
token := c.Param("token")
|
||||
// Contrat V1 : les tokens de lien sont du 32-hex (cf. docs/api-v1.md).
|
||||
// Malformé → 400 INVALID_REQUEST (pas 404 : ce n'est pas « introuvable »,
|
||||
// c'est « requête invalide »). deviceIDPattern (devices.go, même package)
|
||||
// exprime exactement ce format.
|
||||
if !deviceIDPattern.MatchString(token) {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "malformed link token")
|
||||
return
|
||||
}
|
||||
link, err := Store.Repository.Shares.GetPublicLink(token)
|
||||
if err != nil {
|
||||
if err == repository.ErrNotFound {
|
||||
// A6 : errors.Is (pas ==) — le repo peut envelopper ErrNotFound.
|
||||
if errors.Is(err, repository.ErrNotFound) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "link not found or revoked")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/vaultdrop/backend/repository"
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
func TestShareLinkPublicResolution(t *testing.T) {
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/kazier/backend/pkg/api"
|
||||
)
|
||||
|
||||
// SharesList exposes the active user-to-user grants of a resource the caller
|
||||
// OWNS : GET /files/:id/shares. Non-owner (or unknown resource) → 404
|
||||
// NOT_FOUND (no enumeration, same scoping as the share outbox ops).
|
||||
func SharesList(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
grants, err := Store.ListShares(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, grants)
|
||||
}
|
||||
|
||||
// LinksList exposes the active public links of a resource the caller OWNS :
|
||||
// GET /files/:id/links. Same owner-only scoping as SharesList.
|
||||
func LinksList(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
links, err := Store.ListLinks(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, links)
|
||||
}
|
||||
|
||||
// PlacementsList exposes byte presence of a resource the caller OWNS
|
||||
// (server entry + device reports, newest first) : GET /files/:id/placements.
|
||||
// Owner-only (404 otherwise, no enumeration).
|
||||
func PlacementsList(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
placements, err := Store.ListPlacements(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, placements)
|
||||
}
|
||||
@@ -2,11 +2,12 @@ package handlers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/vaultdrop/backend/pkg/api"
|
||||
"github.com/vaultdrop/backend/repository"
|
||||
"github.com/vaultdrop/backend/service"
|
||||
"github.com/kazier/backend/pkg/api"
|
||||
"github.com/kazier/backend/repository"
|
||||
"github.com/kazier/backend/service"
|
||||
)
|
||||
|
||||
// Store is the business layer used by handlers; set once at startup
|
||||
@@ -23,11 +24,24 @@ func writeError(c *gin.Context, err error) {
|
||||
api.Error(c, 404, "NOT_FOUND", "resource not found")
|
||||
case errors.Is(err, repository.ErrGranteeNotFound):
|
||||
api.Error(c, 404, "GRANTEE_NOT_FOUND", "the specified user does not exist")
|
||||
case errors.Is(err, repository.ErrGroupNotFound):
|
||||
api.Error(c, 404, "GROUP_NOT_FOUND", "the specified group does not exist")
|
||||
case errors.Is(err, repository.ErrNameConflict):
|
||||
api.Error(c, 409, "NAME_CONFLICT", "a resource with this name already exists here")
|
||||
case errors.Is(err, service.FileTooLargeError):
|
||||
case errors.Is(err, service.ErrFileTooLarge):
|
||||
api.Error(c, 413, "FILE_TOO_LARGE", "file exceeds the maximum allowed size")
|
||||
case errors.Is(err, service.ErrQuotaExceeded):
|
||||
api.Error(c, 413, "QUOTA_EXCEEDED", "storage quota exceeded")
|
||||
case errors.Is(err, service.ErrNoPreview):
|
||||
api.Error(c, 404, "NO_PREVIEW", "no preview available for this file")
|
||||
default:
|
||||
api.Error(c, 500, "INTERNAL", err.Error())
|
||||
// Jamais de err.Error() brut au client (fuite SQL/FS/paths, cf.
|
||||
// FEEDBACK #8) : message générique, détail au log serveur.
|
||||
path := ""
|
||||
if c.Request != nil && c.Request.URL != nil {
|
||||
path = c.Request.URL.Path
|
||||
}
|
||||
log.Printf("internal error: path=%s err=%v", path, err)
|
||||
api.Error(c, 500, "INTERNAL", "internal error")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,17 +2,45 @@ package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/vaultdrop/backend/pkg/api"
|
||||
"github.com/vaultdrop/backend/service"
|
||||
"github.com/kazier/backend/pkg/api"
|
||||
"github.com/kazier/backend/service"
|
||||
)
|
||||
|
||||
// parseAfterMs parses the `after` delta cursor of GET /sync/permissions :
|
||||
// epoch-millis, 64-bit (les curseurs dépassent 2^31). Chaîne vide → 0 (sync
|
||||
// complet). Garbage ou négatif → ok=false : le handler répond 400
|
||||
// INVALID_REQUEST, jamais de repli silencieux vers 0 (un curseur malformé
|
||||
// pris pour « sync complet » masquerait une désynchro client).
|
||||
// NOTE : la pagination (files.go) répond 400 sur page/pageSize malformés,
|
||||
// jamais de repli silencieux vers un défaut (un curseur malformé pris pour
|
||||
// « sync complet » masquerait une désynchro client).
|
||||
func parseAfterMs(raw string) (int64, bool) {
|
||||
if raw == "" {
|
||||
return 0, true
|
||||
}
|
||||
ms, err := strconv.ParseInt(raw, 10, 64)
|
||||
if err != nil || ms < 0 {
|
||||
return 0, false
|
||||
}
|
||||
return ms, true
|
||||
}
|
||||
|
||||
type syncOpsRequest struct {
|
||||
Operations []service.SyncOperation `json:"operations"`
|
||||
}
|
||||
|
||||
// Bornes anti-DoS de POST /sync/ops (FEEDBACK §moyenne) : le client envoie des
|
||||
// batchs de 20 ; au-delà de 100 ops ou 1 MiB de body → rejet explicite, jamais
|
||||
// d'OOM sur batch géant.
|
||||
const (
|
||||
maxSyncOpsBodyBytes = 1 << 20
|
||||
maxSyncOpsPerBatch = 100
|
||||
)
|
||||
|
||||
func SyncOpsPush(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
@@ -20,11 +48,16 @@ func SyncOpsPush(c *gin.Context) {
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
|
||||
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxSyncOpsBodyBytes)
|
||||
var req syncOpsRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid JSON body")
|
||||
return
|
||||
}
|
||||
if len(req.Operations) > maxSyncOpsPerBatch {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "too many operations in batch (max 100)")
|
||||
return
|
||||
}
|
||||
|
||||
result, err := Store.ApplyBatch(userID, c.GetString(DeviceIDKey), req.Operations)
|
||||
if err != nil {
|
||||
@@ -40,10 +73,10 @@ func SyncPermissionsGet(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
after := c.Query("after")
|
||||
var afterMs int64
|
||||
if after != "" {
|
||||
afterMs = int64(intParam(after, 0))
|
||||
afterMs, ok := parseAfterMs(c.Query("after"))
|
||||
if !ok {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid `after` cursor, must be non-negative epoch-millis")
|
||||
return
|
||||
}
|
||||
perms, err := Store.Snapshot(userID, afterMs)
|
||||
if err != nil {
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/vaultdrop/backend/repository"
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
func syncOpsBody(ops []map[string]any) []byte {
|
||||
@@ -254,6 +254,20 @@ func TestSyncOpsLargeBatchAccepted(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncOpsBatchTooLargeRejected(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "sx"), "sync-test-password", device)
|
||||
|
||||
ops := make([]map[string]any, 0, 101)
|
||||
for i := 0; i < 101; i++ {
|
||||
ops = append(ops, op(repository.NewID(), repository.NewID(), "create_resource", "file", map[string]any{"name": fmt.Sprintf("g%03d.txt", i)}))
|
||||
}
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/sync/ops", token, syncOpsBody(ops), "application/json")
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "sync-batch-too-large")
|
||||
}
|
||||
|
||||
func TestSyncOpsStopsAtFirstNonIdempotentFailure(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
package handlers_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// TestTrashEndpoints couvre le cycle corbeille via HTTP : DELETE (trash) →
|
||||
// 404 en lecture → GET /trash → POST /files/:id/restore → visible →
|
||||
// DELETE ?mode=purge → corbeille vide, restore → 404.
|
||||
func TestTrashEndpoints(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, userID := registerAndLogin(t, r, repo, testUserUsername(device, "tr"), "trash-test-password", device)
|
||||
|
||||
fileID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(userID, fileID, "acte.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodDelete, "/api/v1/files/"+fileID, token, nil, "")
|
||||
expectOK(t, rec, "trash-delete")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID, token, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "trash-get-hidden")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/trash", token, nil, "")
|
||||
env := expectOK(t, rec, "trash-list")
|
||||
var items []struct {
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
TrashedAt string `json:"trashedAt"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &items); err != nil {
|
||||
t.Fatalf("trash list unmarshal: %v", err)
|
||||
}
|
||||
if len(items) != 1 || items[0].ID != fileID || items[0].TrashedAt == "" {
|
||||
t.Fatalf("corbeille inattendue: %+v", items)
|
||||
}
|
||||
if env.Meta == nil || env.Meta.Total != 1 {
|
||||
t.Fatalf("meta corbeille: %+v", env.Meta)
|
||||
}
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/files/"+fileID+"/restore", token, nil, "")
|
||||
expectOK(t, rec, "trash-restore")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID, token, nil, "")
|
||||
expectOK(t, rec, "trash-get-restored")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodDelete, "/api/v1/files/"+fileID+"?mode=purge", token, nil, "")
|
||||
expectOK(t, rec, "trash-purge")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/trash", token, nil, "")
|
||||
env = expectOK(t, rec, "trash-list-empty")
|
||||
if env.Meta == nil || env.Meta.Total != 0 {
|
||||
t.Fatalf("corbeille doit être vide: %+v", env.Meta)
|
||||
}
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/files/"+fileID+"/restore", token, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "trash-restore-purged")
|
||||
}
|
||||
|
||||
// TestTrashInvalidMode : ?mode inconnu → 400, la ressource reste vivante.
|
||||
func TestTrashInvalidMode(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, userID := registerAndLogin(t, r, repo, testUserUsername(device, "tm"), "trash-test-password", device)
|
||||
|
||||
fileID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(userID, fileID, "doc.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodDelete, "/api/v1/files/"+fileID+"?mode=bogus", token, nil, "")
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "trash-bad-mode")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID, token, nil, "")
|
||||
expectOK(t, rec, "trash-bad-mode-keeps-live")
|
||||
}
|
||||
|
||||
// TestTrashIsolation : un user ne voit ni ne touche la corbeille d'autrui.
|
||||
func TestTrashIsolation(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
deviceA := repository.NewID()
|
||||
deviceB := repository.NewID()
|
||||
tokenA, userA := registerAndLogin(t, r, repo, testUserUsername(deviceA, "ta"), "trash-test-password", deviceA)
|
||||
tokenB, _ := registerAndLogin(t, r, repo, testUserUsername(deviceB, "tb"), "trash-test-password", deviceB)
|
||||
|
||||
fileID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(userA, fileID, "prive.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodDelete, "/api/v1/files/"+fileID, tokenB, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "trash-foreign")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/trash", tokenB, nil, "")
|
||||
env := expectOK(t, rec, "trash-foreign-list")
|
||||
if env.Meta == nil || env.Meta.Total != 0 {
|
||||
t.Fatalf("corbeille d'autrui doit être vide: %+v", env.Meta)
|
||||
}
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodDelete, "/api/v1/files/"+fileID, tokenA, nil, "")
|
||||
expectOK(t, rec, "trash-owner")
|
||||
}
|
||||
|
||||
// TestVersionsEndpoints : upload miniature (MaxFileSize=10 en test) puis
|
||||
// re-upload ciblé → GET /files/:id/versions (1 entrée) → restore → contenu
|
||||
// d'origine. Trashée → versions en 404.
|
||||
func TestVersionsEndpoints(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "ve"), "trash-test-password", device)
|
||||
|
||||
rec := uploadMultipart(t, r, token, "", "doc.txt", []byte("AAA"))
|
||||
env := expectOK(t, rec, "versions-upload")
|
||||
var created fileDTO
|
||||
if err := json.Unmarshal(env.Data, &created); err != nil {
|
||||
t.Fatalf("upload unmarshal: %v", err)
|
||||
}
|
||||
|
||||
rec = uploadMultipartToResource(t, r, token, created.ID, "doc.txt", []byte("BBBB"))
|
||||
expectOK(t, rec, "versions-overwrite")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+created.ID+"/versions", token, nil, "")
|
||||
env = expectOK(t, rec, "versions-list")
|
||||
var vers []struct {
|
||||
VersionNo int `json:"versionNo"`
|
||||
Size int64 `json:"size"`
|
||||
CreatedAt string `json:"createdAt"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &vers); err != nil {
|
||||
t.Fatalf("versions unmarshal: %v", err)
|
||||
}
|
||||
if len(vers) != 1 || vers[0].Size != 3 {
|
||||
t.Fatalf("versions inattendues: %+v", vers)
|
||||
}
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/files/"+created.ID+"/versions/1/restore", token, nil, "")
|
||||
env = expectOK(t, rec, "versions-restore")
|
||||
var restored fileDTO
|
||||
if err := json.Unmarshal(env.Data, &restored); err != nil {
|
||||
t.Fatalf("restore unmarshal: %v", err)
|
||||
}
|
||||
if restored.Size != 3 {
|
||||
t.Fatalf("live restauré taille=%d, attendu 3", restored.Size)
|
||||
}
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/files/"+created.ID+"/versions/99/restore", token, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "versions-restore-unknown")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodDelete, "/api/v1/files/"+created.ID, token, nil, "")
|
||||
expectOK(t, rec, "versions-trash")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+created.ID+"/versions", token, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "versions-trashed-hidden")
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/kazier/backend/pkg/api"
|
||||
)
|
||||
|
||||
// UsersUsage expose l'empreinte du compte (V3 S6 A2) : `GET /users/me/usage`
|
||||
// `{ usedBytes, quotaBytes, fileCount, trashCount, trashedBytes }` —
|
||||
// calculé, jamais stocké. La corbeille est détaillée (octets récupérables).
|
||||
func UsersUsage(c *gin.Context) {
|
||||
if Store == nil || Store.Repository == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
usage, err := Store.Usage(userID)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, usage)
|
||||
}
|
||||
@@ -8,19 +8,19 @@ type Client interface {
|
||||
GetClientVersion() int
|
||||
}
|
||||
|
||||
func NewClient(clientVersion int) (error, Client) {
|
||||
func NewClient(clientVersion int) (Client, error) {
|
||||
|
||||
isCompatible := ServerClientIsCompatibleWithClient(clientVersion)
|
||||
|
||||
if !isCompatible {
|
||||
return fmt.Errorf("You client version %d is not compatible with the server verison %d", clientVersion, SERVER_VERSION), nil
|
||||
return nil, fmt.Errorf("client version %d is not compatible with the server version %d", clientVersion, SERVER_VERSION)
|
||||
}
|
||||
|
||||
client := ClientVerisonOne{
|
||||
Version: clientVersion,
|
||||
}
|
||||
|
||||
return nil, &client
|
||||
return &client, nil
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type Document struct {
|
||||
@@ -37,19 +37,19 @@ func IsDirectory(documentType int) bool {
|
||||
return DIRECTORY == documentType
|
||||
}
|
||||
|
||||
func NewDocument(documentName string, documentType int, documentVersion int) (error, *Document) {
|
||||
func NewDocument(documentName string, documentType int, documentVersion int) (*Document, error) {
|
||||
|
||||
documentTypeIsValid := IsDocumentTypeValid(documentType)
|
||||
|
||||
if !documentTypeIsValid {
|
||||
return fmt.Errorf(ERROR_DOCUMENT_TYPE), nil
|
||||
return nil, errors.New(ERROR_DOCUMENT_TYPE)
|
||||
}
|
||||
|
||||
return nil, &Document{
|
||||
return &Document{
|
||||
Name: &documentName,
|
||||
Type: documentType,
|
||||
Version: documentVersion,
|
||||
}
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -4,8 +4,8 @@ type User struct {
|
||||
Username string
|
||||
}
|
||||
|
||||
func NewUser(username string) (error, *User) {
|
||||
return nil, &User{
|
||||
func NewUser(username string) (*User, error) {
|
||||
return &User{
|
||||
Username: username,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -4,13 +4,42 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/ledongthuc/pdf"
|
||||
)
|
||||
|
||||
const (
|
||||
// tesseractTimeout : budget max par job (A13) — Tesseract ne doit jamais
|
||||
// bloquer un worker indéfiniment (image pathologique).
|
||||
tesseractTimeout = 120 * time.Second
|
||||
// outputCap : plafond stdout/stderr capturés (A13, 2 Mo) — un document
|
||||
// hostile ne doit pas faire exploser la RAM du worker.
|
||||
outputCap = 2 << 20
|
||||
// maxPDFPages : plafond de pages lues (A13) — au-delà, erreur explicite
|
||||
// (pas de troncature silencieuse).
|
||||
maxPDFPages = 50
|
||||
)
|
||||
|
||||
// validTesseractLang : whitelist `^[a-z+]{2,20}$` (A12, défense en profondeur
|
||||
// — le point d'entrée normal est service.normalizeOcrLang). Évite toute
|
||||
// injection via `tesseract -l` (espaces, `;`, options `--...`).
|
||||
func validTesseractLang(lang string) bool {
|
||||
if len(lang) < 2 || len(lang) > 20 {
|
||||
return false
|
||||
}
|
||||
for _, r := range lang {
|
||||
if (r < 'a' || r > 'z') && r != '+' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// TesseractEngine runs the `tesseract` binary in a subprocess.
|
||||
// Images are OCR'd directly; PDFs have their text layer extracted first
|
||||
// (scanned PDFs → empty text, no rendering pipeline in V1).
|
||||
@@ -18,20 +47,55 @@ type TesseractEngine struct{}
|
||||
|
||||
func NewTesseract() *TesseractEngine { return &TesseractEngine{} }
|
||||
|
||||
// ExtractText implements Engine.
|
||||
// ExtractText implements Engine. Chaque job est borné par tesseractTimeout
|
||||
// (contexte dérivé, annulé en fin d'appel).
|
||||
func (t *TesseractEngine) ExtractText(ctx context.Context, filePath, lang string) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, tesseractTimeout)
|
||||
defer cancel()
|
||||
if strings.ToLower(filepath.Ext(filePath)) == ".pdf" {
|
||||
return extractPDFText(filePath)
|
||||
return extractPDFText(ctx, filePath)
|
||||
}
|
||||
return runTesseract(ctx, filePath, lang)
|
||||
}
|
||||
|
||||
// cappedBuffer borne un flux capturé à `limit` octets et signale la
|
||||
// troncature (erreur explicite côté appelant, pas de coupe silencieuse).
|
||||
type cappedBuffer struct {
|
||||
buf bytes.Buffer
|
||||
limit int
|
||||
truncated bool
|
||||
}
|
||||
|
||||
func (c *cappedBuffer) Write(p []byte) (int, error) {
|
||||
remaining := c.limit - c.buf.Len()
|
||||
if remaining <= 0 {
|
||||
c.truncated = true
|
||||
return len(p), nil // déverse : le process ne bloque pas sur un pipe plein
|
||||
}
|
||||
if len(p) > remaining {
|
||||
c.truncated = true
|
||||
p = p[:remaining]
|
||||
}
|
||||
return c.buf.Write(p)
|
||||
}
|
||||
|
||||
func (c *cappedBuffer) String() string { return c.buf.String() }
|
||||
|
||||
func runTesseract(ctx context.Context, filePath, lang string) (string, error) {
|
||||
// Défense en profondeur (A12) : même si l'appelant ne passe pas par
|
||||
// service.normalizeOcrLang, `-l` ne reçoit jamais de métacaractères.
|
||||
if !validTesseractLang(lang) {
|
||||
return "", fmt.Errorf("tesseract: langue %q invalide", lang)
|
||||
}
|
||||
cmd := exec.CommandContext(ctx, "tesseract", filePath, "stdout", "-l", lang)
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
stdout := &cappedBuffer{limit: outputCap}
|
||||
stderr := &cappedBuffer{limit: outputCap}
|
||||
cmd.Stdout = stdout
|
||||
cmd.Stderr = stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
||||
return "", fmt.Errorf("tesseract: délai dépassé (%s): %w", tesseractTimeout, ctx.Err())
|
||||
}
|
||||
if errors.Is(ctx.Err(), context.Canceled) {
|
||||
return "", ctx.Err()
|
||||
}
|
||||
@@ -41,24 +105,37 @@ func runTesseract(ctx context.Context, filePath, lang string) (string, error) {
|
||||
}
|
||||
return "", errors.New("tesseract: " + msg)
|
||||
}
|
||||
if stdout.truncated {
|
||||
return "", fmt.Errorf("tesseract: sortie tronquée (> %d octets)", outputCap)
|
||||
}
|
||||
// stderr tronqué avec un run OK : le texte est complet, mais le diagnostic
|
||||
// est perdu — on l'accepte sans échouer le job.
|
||||
return strings.TrimSpace(stdout.String()), nil
|
||||
}
|
||||
|
||||
func extractPDFText(filePath string) (string, error) {
|
||||
func extractPDFText(ctx context.Context, filePath string) (string, error) {
|
||||
f, r, err := pdf.Open(filePath)
|
||||
if err != nil {
|
||||
return "", errors.New("pdf: " + err.Error())
|
||||
}
|
||||
defer f.Close()
|
||||
if n := r.NumPage(); n > maxPDFPages {
|
||||
return "", fmt.Errorf("pdf: %d pages > plafond %d (tronquer = perdre du texte)", n, maxPDFPages)
|
||||
}
|
||||
var builder strings.Builder
|
||||
for i := 1; i <= r.NumPage(); i++ {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return "", fmt.Errorf("pdf: annulé page %d: %w", i, ctx.Err())
|
||||
default:
|
||||
}
|
||||
p := r.Page(i)
|
||||
if p.V.IsNull() {
|
||||
continue
|
||||
return "", fmt.Errorf("pdf: page %d nulle", i)
|
||||
}
|
||||
plain, err := p.GetPlainText(nil)
|
||||
if err != nil {
|
||||
continue
|
||||
return "", fmt.Errorf("pdf: page %d illisible: %w", i, err)
|
||||
}
|
||||
builder.WriteString(plain)
|
||||
builder.WriteString("\n")
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package ocr
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValidTesseractLang(t *testing.T) {
|
||||
valid := []string{"fra", "eng", "fra+eng", "de", "abcdefghijklmnopqrst"} // 20 chars
|
||||
for _, lang := range valid {
|
||||
if !validTesseractLang(lang) {
|
||||
t.Errorf("langue %q : attendue valide", lang)
|
||||
}
|
||||
}
|
||||
invalid := []string{
|
||||
"", "e", "fra;rm -rf", "fra eng", "fra+eng --psm",
|
||||
"FRA", "fra-eng", "fra/eng", "abcdefghijklmnopqrstu", // 21 chars
|
||||
"fra\neng", "-l fra",
|
||||
}
|
||||
for _, lang := range invalid {
|
||||
if validTesseractLang(lang) {
|
||||
t.Errorf("langue %q : attendue invalide (injection -l)", lang)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCappedBufferTruncation(t *testing.T) {
|
||||
c := &cappedBuffer{limit: 8}
|
||||
if _, err := c.Write([]byte("12345678")); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
if c.truncated {
|
||||
t.Error("8 octets pour limite 8 : ne doit pas être tronqué")
|
||||
}
|
||||
if _, err := c.Write([]byte("9")); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
if !c.truncated {
|
||||
t.Error("9 octets pour limite 8 : doit être marqué tronqué")
|
||||
}
|
||||
if c.String() != "12345678" {
|
||||
t.Errorf("contenu = %q, attendu %q", c.String(), "12345678")
|
||||
}
|
||||
// Écriture massive d'un coup : bornée, jamais plus que `limit`.
|
||||
big := &cappedBuffer{limit: 4}
|
||||
if _, err := big.Write([]byte(strings.Repeat("x", 1<<20))); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
if !big.truncated || len(big.String()) != 4 {
|
||||
t.Errorf("écriture 1 Mo limite 4 : tronqué=%v len=%d", big.truncated, len(big.String()))
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
package api
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const NotImplementedCode = "NOT_IMPLEMENTED"
|
||||
|
||||
@@ -22,6 +26,14 @@ func OKList(c *gin.Context, data any, page, pageSize, total int) {
|
||||
}
|
||||
|
||||
func Error(c *gin.Context, status int, code, message string) {
|
||||
// LOT 6 §5 : le client ne reçoit que l'enveloppe générique (code +
|
||||
// message sans détail interne — jamais de err.Error() brut). Le détail
|
||||
// va au log serveur pour le diagnostic.
|
||||
path := ""
|
||||
if c.Request != nil && c.Request.URL != nil {
|
||||
path = c.Request.URL.Path
|
||||
}
|
||||
log.Printf("api error: status=%d code=%s message=%s path=%s", status, code, message, path)
|
||||
c.JSON(status, gin.H{
|
||||
"error": gin.H{"code": code, "message": message},
|
||||
})
|
||||
|
||||
@@ -3,6 +3,8 @@ package auth
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"aidanwoods.dev/go-paseto"
|
||||
@@ -14,11 +16,16 @@ const TokenTTL = 7 * 24 * time.Hour
|
||||
|
||||
var ErrInvalidToken = errors.New("invalid token")
|
||||
|
||||
// Identity est le résultat de Verify : l'utilisateur (sujet du token, autorisant)
|
||||
// et le device (claim secondaire, porté mais NON autorisant seul).
|
||||
var hex32Pattern = regexp.MustCompile(`^[0-9a-f]{32}$`)
|
||||
|
||||
// Identity est le résultat de Verify : l'utilisateur (sujet du token, autorisant),
|
||||
// le device (claim secondaire, porté mais NON autorisant seul) et la version
|
||||
// de révocation (claim `tv`, V3 S6 — le middleware la compare à
|
||||
// users.token_version).
|
||||
type Identity struct {
|
||||
UserID string
|
||||
DeviceID string
|
||||
TokenVersion int
|
||||
}
|
||||
|
||||
// Manager issues and verifies paseto v4-local bearer tokens.
|
||||
@@ -37,32 +44,55 @@ func NewManager(secret string) (*Manager, error) {
|
||||
return &Manager{key: key}, nil
|
||||
}
|
||||
|
||||
// Issue émet un token lié à un utilisateur ET à un device.
|
||||
// Issue émet un token lié à un utilisateur ET à un device (version 0 —
|
||||
// compat pré-V3, cf. IssueWithExpiry).
|
||||
func (m *Manager) Issue(userID, deviceID string) (string, error) {
|
||||
token, _, err := m.IssueWithExpiry(userID, deviceID, 0)
|
||||
return token, err
|
||||
}
|
||||
|
||||
// IssueWithExpiry émet un token et retourne son expiration réelle (le `exp`
|
||||
// embarqué). Les appelants doivent exposer expiresAt, jamais le recalculer
|
||||
// via Now()+TTL (dérive d'horloge, cf. FEEDBACK A7). `tokenVersion` est le
|
||||
// compteur de révocation du compte (V3 S6) — bumpé à chaque changement de
|
||||
// mot de passe, vérifié par le middleware.
|
||||
func (m *Manager) IssueWithExpiry(userID, deviceID string, tokenVersion int) (string, time.Time, error) {
|
||||
now := time.Now()
|
||||
expiresAt := now.Add(TokenTTL)
|
||||
token := paseto.NewToken()
|
||||
token.SetIssuedAt(now)
|
||||
token.SetNotBefore(now)
|
||||
token.SetExpiration(now.Add(TokenTTL))
|
||||
token.SetExpiration(expiresAt)
|
||||
token.SetSubject(userID)
|
||||
// v1.6.0 : SetString n'expose pas d'erreur (panic si non sérialisable).
|
||||
token.SetString("device_id", deviceID)
|
||||
return token.V4Encrypt(m.key, nil), nil
|
||||
token.SetString("tv", strconv.Itoa(tokenVersion))
|
||||
return token.V4Encrypt(m.key, nil), expiresAt, nil
|
||||
}
|
||||
|
||||
// Verify décode et contrôle le token, retourne l'identité (user + device).
|
||||
// Verify décode et contrôle le token, retourne l'identité (user + device +
|
||||
// version). Les deux identifiants doivent être 32-hex (cf. docs/api-v1.md) —
|
||||
// rejet sinon pour préserver les invariants UNIQUE(device_id,operation_id)/CHECK.
|
||||
// `tv` absent (tokens pré-V3) → 0 ; non numérique → rejet.
|
||||
func (m *Manager) Verify(signed string) (Identity, error) {
|
||||
parsed, err := paseto.NewParserForValidNow().ParseV4Local(m.key, signed, nil)
|
||||
if err != nil {
|
||||
return Identity{}, ErrInvalidToken
|
||||
}
|
||||
userID, err := parsed.GetSubject()
|
||||
if err != nil || userID == "" {
|
||||
if err != nil || !hex32Pattern.MatchString(userID) {
|
||||
return Identity{}, ErrInvalidToken
|
||||
}
|
||||
deviceID, err := parsed.GetString("device_id")
|
||||
if err != nil || deviceID == "" {
|
||||
if err != nil || !hex32Pattern.MatchString(deviceID) {
|
||||
return Identity{}, ErrInvalidToken
|
||||
}
|
||||
return Identity{UserID: userID, DeviceID: deviceID}, nil
|
||||
tv := 0
|
||||
if raw, err := parsed.GetString("tv"); err == nil && raw != "" {
|
||||
tv, err = strconv.Atoi(raw)
|
||||
if err != nil || tv < 0 {
|
||||
return Identity{}, ErrInvalidToken
|
||||
}
|
||||
}
|
||||
return Identity{UserID: userID, DeviceID: deviceID, TokenVersion: tv}, nil
|
||||
}
|
||||
|
||||
@@ -96,6 +96,44 @@ func TestVerifyRequiresDeviceClaim(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyRejectsNonHex32IDs(t *testing.T) {
|
||||
m, _ := NewManager("test-secret")
|
||||
|
||||
issueWith := func(userID, deviceID string) string {
|
||||
token := paseto.NewToken()
|
||||
now := time.Now()
|
||||
token.SetIssuedAt(now)
|
||||
token.SetNotBefore(now)
|
||||
token.SetExpiration(now.Add(TokenTTL))
|
||||
token.SetSubject(userID)
|
||||
token.SetString("device_id", deviceID)
|
||||
return token.V4Encrypt(m.key, nil)
|
||||
}
|
||||
|
||||
cases := map[string][2]string{
|
||||
"subject vide": {"", testDeviceID},
|
||||
"subject non-hex": {"not-hex-at-all-!!!!!!!!!!!!!!!!", testDeviceID},
|
||||
"subject majuscules": {"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", testDeviceID},
|
||||
"subject trop court": {"abc123", testDeviceID},
|
||||
"device vide": {testUserID, ""},
|
||||
"device non-hex": {testUserID, "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"},
|
||||
"device majuscules": {testUserID, "0123456789ABCDEF0123456789ABCDEF"},
|
||||
"device trop long": {testUserID, testDeviceID + "00"},
|
||||
"device avec tirets": {testUserID, "01234567-89ab-cdef-0123-456789abcdef"},
|
||||
}
|
||||
for name, ids := range cases {
|
||||
signed := issueWith(ids[0], ids[1])
|
||||
if _, err := m.Verify(signed); err == nil {
|
||||
t.Errorf("%s: token accepté (user=%q device=%q), attendu rejet", name, ids[0], ids[1])
|
||||
}
|
||||
}
|
||||
|
||||
// Sanity : les IDs 32-hex valides passent toujours.
|
||||
if _, err := m.Verify(issueWith(testUserID, testDeviceID)); err != nil {
|
||||
t.Errorf("IDs valides rejetés: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIssueSetsExpiration(t *testing.T) {
|
||||
m, _ := NewManager("test-secret")
|
||||
before := time.Now()
|
||||
@@ -118,3 +156,49 @@ func TestIssueSetsExpiration(t *testing.T) {
|
||||
t.Errorf("expiration = %v, attendu ≈ now+%v (fenêtre [%v, %v])", exp, TokenTTL, lower, upper)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIssueWithExpiryMatchesTokenExp(t *testing.T) {
|
||||
m, _ := NewManager("test-secret")
|
||||
signed, expiresAt, err := m.IssueWithExpiry(testUserID, testDeviceID, 3)
|
||||
if err != nil {
|
||||
t.Fatalf("IssueWithExpiry: %v", err)
|
||||
}
|
||||
parsed, err := paseto.NewParserForValidNow().ParseV4Local(m.key, signed, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("parse: %v", err)
|
||||
}
|
||||
exp, err := parsed.GetExpiration()
|
||||
if err != nil {
|
||||
t.Fatalf("GetExpiration: %v", err)
|
||||
}
|
||||
// L'expiresAt retourné doit être le exp embarqué (pas un Now()+TTL
|
||||
// recalculé — cf. FEEDBACK A7). Comparaison à la seconde : le token
|
||||
// sérialise sans lecture monotonique ni sous-seconde.
|
||||
if exp.Unix() != expiresAt.Unix() {
|
||||
t.Errorf("expiresAt = %v, exp token = %v", expiresAt, exp)
|
||||
}
|
||||
id, err := m.Verify(signed)
|
||||
if err != nil {
|
||||
t.Fatalf("Verify: %v", err)
|
||||
}
|
||||
if id.TokenVersion != 3 {
|
||||
t.Errorf("tv = %d, attendu 3", id.TokenVersion)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyLegacyTokenWithoutTv(t *testing.T) {
|
||||
m, _ := NewManager("test-secret")
|
||||
// Tokens pré-V3 (Issue, tv=0) : Verify lit 0 — valides tant que le
|
||||
// compte n'a jamais bumpé (montée en version sans coupure).
|
||||
signed, err := m.Issue(testUserID, testDeviceID)
|
||||
if err != nil {
|
||||
t.Fatalf("Issue: %v", err)
|
||||
}
|
||||
id, err := m.Verify(signed)
|
||||
if err != nil {
|
||||
t.Fatalf("Verify legacy: %v", err)
|
||||
}
|
||||
if id.TokenVersion != 0 {
|
||||
t.Errorf("tv legacy = %d, attendu 0", id.TokenVersion)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,12 +30,17 @@ var (
|
||||
ErrMalformed = errors.New("malformed password hash")
|
||||
)
|
||||
|
||||
// dummyHash est un hash argon2id valide d'un mot de passe aléatoire fixe.
|
||||
// Il sert d'égalisation de timing : quand le username est inconnu, on vérifie
|
||||
// quand même contre ce hash pour que le délai de réponse soit identique à un
|
||||
// « mauvais mot de passe ».
|
||||
// dummyHash est un hash argon2id valide d'un mot de passe aléatoire généré
|
||||
// au boot. Il sert d'égalisation de timing : quand le username est inconnu,
|
||||
// on vérifie quand même contre ce hash pour que le délai de réponse soit
|
||||
// identique à un « mauvais mot de passe ». Aléatoire par boot pour éviter
|
||||
// toute préimage hardcodée (cf. FEEDBACK A1).
|
||||
var dummyHash = func() string {
|
||||
h, err := Hash("vaultdrop-constantime-dummy")
|
||||
raw := make([]byte, 32)
|
||||
if _, err := rand.Read(raw); err != nil {
|
||||
panic(fmt.Sprintf("passwd: entropie dummy indisponible: %v", err))
|
||||
}
|
||||
h, err := Hash(string(raw))
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("passwd: impossible de pré-hacher le dummy: %v", err))
|
||||
}
|
||||
@@ -75,6 +80,18 @@ func Verify(password, stored string) error {
|
||||
if _, err := fmt.Sscanf(fields[3], "m=%d,t=%d,p=%d", &memory, &timeCost, &threads); err != nil {
|
||||
return ErrMalformed
|
||||
}
|
||||
// Caps anti-DoS : un hash stocké compromis ne doit pas provoquer un
|
||||
// OOM/CPU arbitraire au prochain login (cf. FEEDBACK §moyenne).
|
||||
const (
|
||||
maxMemory = 256 * 1024 // 256 MiB
|
||||
maxTime = 10
|
||||
maxThreads = 8
|
||||
maxKeyLen = 128
|
||||
maxSaltLen = 128
|
||||
)
|
||||
if memory <= 0 || memory > maxMemory || timeCost <= 0 || timeCost > maxTime || threads <= 0 || threads > maxThreads {
|
||||
return ErrMalformed
|
||||
}
|
||||
|
||||
salt, err := base64.RawStdEncoding.DecodeString(fields[4])
|
||||
if err != nil {
|
||||
@@ -84,6 +101,9 @@ func Verify(password, stored string) error {
|
||||
if err != nil {
|
||||
return ErrMalformed
|
||||
}
|
||||
if len(salt) == 0 || len(salt) > maxSaltLen || len(want) == 0 || len(want) > maxKeyLen {
|
||||
return ErrMalformed
|
||||
}
|
||||
|
||||
got := argon2.IDKey([]byte(password), salt, uint32(timeCost), uint32(memory), uint8(threads), uint32(len(want)))
|
||||
if subtle.ConstantTimeCompare(got, want) != 1 {
|
||||
|
||||
@@ -33,10 +33,80 @@ func (d *Devices) Exists(deviceID string) (bool, error) {
|
||||
|
||||
// MarkUser mémorise le dernier utilisateur connecté sur ce device (INFORMATIF,
|
||||
// jamais autorisant) et rafraîchit last_seen_at. Appelé au login.
|
||||
// Device absent → ErrNotFound (le handler distingue la course
|
||||
// Exists→MarkUser au lieu d'émettre un token pour un device fantôme).
|
||||
func (d *Devices) MarkUser(deviceID, userID string) error {
|
||||
_, err := d.DB.Exec(
|
||||
result, err := d.DB.Exec(
|
||||
`UPDATE devices SET user_id = $2, last_seen_at = NOW() WHERE device_id = $1`,
|
||||
deviceID, userID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if affected == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeviceRow is one registered device of an account (A1 — voir / révoquer).
|
||||
type DeviceRow struct {
|
||||
DeviceID string
|
||||
LastSeenAt *int64 // ms epoch, NULL si jamais vu depuis l'enregistrement
|
||||
}
|
||||
|
||||
// ListByUser returns the devices whose last login was this account
|
||||
// (devices.user_id informatif, jamais autorisant).
|
||||
func (d *Devices) ListByUser(userID string) ([]DeviceRow, error) {
|
||||
rows, err := d.DB.Query(
|
||||
`SELECT device_id, (EXTRACT(EPOCH FROM last_seen_at) * 1000)::bigint
|
||||
FROM devices WHERE user_id = $1 ORDER BY last_seen_at DESC NULLS LAST`,
|
||||
userID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []DeviceRow{}
|
||||
for rows.Next() {
|
||||
var row DeviceRow
|
||||
if err := rows.Scan(&row.DeviceID, &row.LastSeenAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, row)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// Revoke deletes a device registration of this account (A1 — téléphone
|
||||
// perdu). Avec EnforceDeviceExists, les tokens du device deviennent 401 et
|
||||
// le login refuse le device (ré-enregistrement explicite requis).
|
||||
// Ligne absente ou d'un autre compte → ErrNotFound (pas d'énumération).
|
||||
// Les hints de présence (`storage_locations`) suivent.
|
||||
func (d *Devices) Revoke(userID, deviceID string) error {
|
||||
result, err := d.DB.Exec(
|
||||
`DELETE FROM devices WHERE device_id = $1 AND user_id = $2`,
|
||||
deviceID, userID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if affected == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
if _, err := d.DB.Exec(
|
||||
`DELETE FROM storage_locations WHERE device_id = $1 AND user_id = $2`,
|
||||
deviceID, userID,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/vaultdrop/backend/dbtest"
|
||||
"github.com/kazier/backend/dbtest"
|
||||
)
|
||||
|
||||
func newTestDevices(t *testing.T) *Devices {
|
||||
@@ -71,3 +72,12 @@ func TestDevicesMarkUser(t *testing.T) {
|
||||
t.Errorf("user_id mémorisé = %q, attendu %q", storedUser, userID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDevicesMarkUserUnknownDevice(t *testing.T) {
|
||||
d := newTestDevices(t)
|
||||
// Device jamais enregistré → ErrNotFound (pas de UPDATE silencieux à
|
||||
// 0 ligne) : le login mappe ce cas en 401 au lieu d'émettre un token.
|
||||
if err := d.MarkUser(NewID(), NewID()); !errors.Is(err, ErrNotFound) {
|
||||
t.Errorf("MarkUser device inconnu: attendu ErrNotFound, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,186 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
// FactParam is one generic fact to persist (V3 S2, migration 000019).
|
||||
// ValueNum/ValueDate nil when the type carries no numeric/date value.
|
||||
type FactParam struct {
|
||||
Type string
|
||||
ValueText string
|
||||
ValueNum *float64
|
||||
ValueDate *string // ISO YYYY-MM-DD
|
||||
Confidence float64
|
||||
}
|
||||
|
||||
// FactRow is one persisted fact (auto-extracted or user-corrected).
|
||||
type FactRow struct {
|
||||
ID string
|
||||
Type string
|
||||
ValueText string
|
||||
ValueNum *float64
|
||||
ValueDate *string
|
||||
Confidence float64
|
||||
Origin string
|
||||
}
|
||||
|
||||
// UpsertAutoFacts replaces the origin='auto' facts of a resource with a fresh
|
||||
// extraction. Rows with origin='user' (S3 corrections) are never touched.
|
||||
// An empty params clears stale auto facts (e.g. OCR re-run yielding nothing).
|
||||
func (r *Resources) UpsertAutoFacts(resourceID string, params []FactParam) error {
|
||||
tx, err := r.DB.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback() //nolint:errcheck — no-op after Commit.
|
||||
if _, err := tx.Exec(
|
||||
`DELETE FROM resource_facts WHERE resource_id = $1 AND origin = 'auto'`,
|
||||
resourceID,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, p := range params {
|
||||
id, err := NewIDErr()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(
|
||||
`INSERT INTO resource_facts
|
||||
(id, resource_id, fact_type, value_text, value_num, value_date, confidence, origin)
|
||||
VALUES ($1, $2, $3, NULLIF($4, ''), $5, NULLIF($6, '')::date, $7, 'auto')`,
|
||||
id, resourceID, p.Type, p.ValueText, nullableFloat(p.ValueNum), derefStr(p.ValueDate), clamp01(p.Confidence),
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// ListFacts returns the facts of a resource, user-corrected first then auto
|
||||
// (stable order by type). No visibility check — the caller enforces viewer+
|
||||
// (GetFileVisible) like GetFileOcr.
|
||||
func (r *Resources) ListFacts(resourceID string) ([]FactRow, error) {
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT id, fact_type, COALESCE(value_text, ''), value_num,
|
||||
COALESCE(value_date::text, ''), confidence, origin
|
||||
FROM resource_facts WHERE resource_id = $1
|
||||
ORDER BY CASE origin WHEN 'user' THEN 0 ELSE 1 END, fact_type, value_text`,
|
||||
resourceID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := make([]FactRow, 0)
|
||||
for rows.Next() {
|
||||
var row FactRow
|
||||
var num sql.NullFloat64
|
||||
var dat string
|
||||
if err := rows.Scan(&row.ID, &row.Type, &row.ValueText, &num, &dat, &row.Confidence, &row.Origin); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if num.Valid {
|
||||
v := num.Float64
|
||||
row.ValueNum = &v
|
||||
}
|
||||
if dat != "" {
|
||||
d := dat
|
||||
row.ValueDate = &d
|
||||
}
|
||||
out = append(out, row)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// CorrectFacts fully replaces the origin='user' set of a resource (S3 :
|
||||
// correction en un tap — le client envoie le jeu corrigé complet, pas un
|
||||
// diff). Auto facts are never touched. Empty params clears user corrections
|
||||
// (back to pure auto).
|
||||
func (r *Resources) CorrectFacts(resourceID string, params []FactParam) error {
|
||||
tx, err := r.DB.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback() //nolint:errcheck — no-op after Commit.
|
||||
if _, err := tx.Exec(
|
||||
`DELETE FROM resource_facts WHERE resource_id = $1 AND origin = 'user'`,
|
||||
resourceID,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, p := range params {
|
||||
id, err := NewIDErr()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(
|
||||
`INSERT INTO resource_facts
|
||||
(id, resource_id, fact_type, value_text, value_num, value_date, confidence, origin)
|
||||
VALUES ($1, $2, $3, NULLIF($4, ''), $5, NULLIF($6, '')::date, $7, 'user')`,
|
||||
id, resourceID, p.Type, p.ValueText, nullableFloat(p.ValueNum), derefStr(p.ValueDate), clamp01(p.Confidence),
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
type factNeedingExtract struct {
|
||||
UserID string
|
||||
ResourceID string
|
||||
Text string
|
||||
}
|
||||
|
||||
// ListNeedingExtract returns up to limit files with non-empty ocr_text and no
|
||||
// origin='auto' facts (boot backfill, S2). Live rows only.
|
||||
func (r *Resources) ListNeedingExtract(limit int) ([]factNeedingExtract, error) {
|
||||
rows, err := r.DB.Query(
|
||||
`SELECT user_id, resource_id, ocr_text FROM resources
|
||||
WHERE type = 'file' AND deleted_at IS NULL AND user_id IS NOT NULL
|
||||
AND ocr_text IS NOT NULL AND ocr_text <> ''
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM resource_facts
|
||||
WHERE resource_facts.resource_id = resources.resource_id
|
||||
AND origin = 'auto')
|
||||
ORDER BY updated_at ASC LIMIT $1`,
|
||||
limit,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []factNeedingExtract
|
||||
for rows.Next() {
|
||||
var row factNeedingExtract
|
||||
if err := rows.Scan(&row.UserID, &row.ResourceID, &row.Text); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, row)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func nullableFloat(v *float64) any {
|
||||
if v == nil {
|
||||
return nil
|
||||
}
|
||||
return *v
|
||||
}
|
||||
|
||||
func derefStr(v *string) string {
|
||||
if v == nil {
|
||||
return ""
|
||||
}
|
||||
return *v
|
||||
}
|
||||
|
||||
func clamp01(v float64) float64 {
|
||||
if v < 0 {
|
||||
return 0
|
||||
}
|
||||
if v > 1 {
|
||||
return 1
|
||||
}
|
||||
return v
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Group is a named set of users (V2-6) : sharing to a group grants every
|
||||
// member. One level only (no nested groups). The creator owns the group
|
||||
// (member management + deletion) ; membership confers no admin rights.
|
||||
type Group struct {
|
||||
ID string
|
||||
Name string
|
||||
CreatedBy string
|
||||
}
|
||||
|
||||
// Groups implements group persistence.
|
||||
type Groups struct {
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
// Create stores a group owned by ownerID and returns its id.
|
||||
func (g *Groups) Create(name, ownerID string) (string, error) {
|
||||
id := NewID()
|
||||
_, err := g.DB.Exec(
|
||||
`INSERT INTO groups (id, name, created_by) VALUES ($1, $2, $3)`,
|
||||
id, name, ownerID,
|
||||
)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// Get returns a group by id. No-rows → ErrNotFound.
|
||||
func (g *Groups) Get(groupID string) (Group, error) {
|
||||
var group Group
|
||||
err := g.DB.QueryRow(
|
||||
`SELECT id, name, created_by FROM groups WHERE id = $1`,
|
||||
groupID,
|
||||
).Scan(&group.ID, &group.Name, &group.CreatedBy)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return Group{}, ErrNotFound
|
||||
}
|
||||
return group, err
|
||||
}
|
||||
|
||||
// IsOwner reports whether userID created the group.
|
||||
func (g *Groups) IsOwner(groupID, userID string) (bool, error) {
|
||||
var n int
|
||||
err := g.DB.QueryRow(
|
||||
`SELECT COUNT(*) FROM groups WHERE id = $1 AND created_by = $2`,
|
||||
groupID, userID,
|
||||
).Scan(&n)
|
||||
return n > 0, err
|
||||
}
|
||||
|
||||
// IsMember reports group membership (owner excluded : ownership is separate).
|
||||
func (g *Groups) IsMember(groupID, userID string) (bool, error) {
|
||||
var n int
|
||||
err := g.DB.QueryRow(
|
||||
`SELECT COUNT(*) FROM group_members WHERE group_id = $1 AND user_id = $2`,
|
||||
groupID, userID,
|
||||
).Scan(&n)
|
||||
return n > 0, err
|
||||
}
|
||||
|
||||
// GroupWithRole is a group listing row (owned or joined).
|
||||
type GroupWithRole struct {
|
||||
Group
|
||||
Role string // "owner" | "member"
|
||||
}
|
||||
|
||||
// ListForUser returns groups the user owns or joins, owned first.
|
||||
func (g *Groups) ListForUser(userID string) ([]GroupWithRole, error) {
|
||||
rows, err := g.DB.Query(
|
||||
`SELECT id, name, created_by,
|
||||
CASE WHEN created_by = $1 THEN 'owner' ELSE 'member' END AS role
|
||||
FROM groups
|
||||
WHERE created_by = $1 OR id IN (SELECT group_id FROM group_members WHERE user_id = $1)
|
||||
ORDER BY created_at ASC`,
|
||||
userID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []GroupWithRole
|
||||
for rows.Next() {
|
||||
var gr GroupWithRole
|
||||
if err := rows.Scan(&gr.ID, &gr.Name, &gr.CreatedBy, &gr.Role); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, gr)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// AddMember inserts a membership. Idempotent on replay (ON CONFLICT DO
|
||||
// NOTHING) ; returns rows-affected style via ErrNotFound when the group or
|
||||
// the user does not exist (FK violation mapped).
|
||||
func (g *Groups) AddMember(groupID, userID string) error {
|
||||
_, err := g.DB.Exec(
|
||||
`INSERT INTO group_members (group_id, user_id) VALUES ($1, $2)
|
||||
ON CONFLICT DO NOTHING`,
|
||||
groupID, userID,
|
||||
)
|
||||
if isForeignKeyViolation(err) {
|
||||
return ErrNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// RemoveMember deletes a membership. Missing row → ErrNotFound.
|
||||
func (g *Groups) RemoveMember(groupID, userID string) error {
|
||||
result, err := g.DB.Exec(
|
||||
`DELETE FROM group_members WHERE group_id = $1 AND user_id = $2`,
|
||||
groupID, userID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if affected == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Member is a group membership row (owner view).
|
||||
type Member struct {
|
||||
UserID string
|
||||
Username string
|
||||
}
|
||||
|
||||
// ListMembers returns memberships of a group, ordered by username.
|
||||
func (g *Groups) ListMembers(groupID string) ([]Member, error) {
|
||||
rows, err := g.DB.Query(
|
||||
`SELECT u.id, COALESCE(u.username, u.id)
|
||||
FROM group_members gm
|
||||
JOIN users u ON u.id = gm.user_id AND u.deleted_at IS NULL
|
||||
WHERE gm.group_id = $1
|
||||
ORDER BY COALESCE(u.username, u.id) ASC`,
|
||||
groupID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []Member
|
||||
for rows.Next() {
|
||||
var m Member
|
||||
if err := rows.Scan(&m.UserID, &m.Username); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, m)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// Errors specific to groups.
|
||||
var ErrGroupNotFound = errors.New("group not found")
|
||||
|
||||
// Delete removes a group (owner only — caller checks). Shares targeting it
|
||||
// cascade (FK), placements are untouched (device-scoped, not group-scoped).
|
||||
func (g *Groups) Delete(groupID, ownerID string) error {
|
||||
result, err := g.DB.Exec(
|
||||
`DELETE FROM groups WHERE id = $1 AND created_by = $2`,
|
||||
groupID, ownerID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if affected == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user