Partage en lecture seule par lien signé

- Lien /s/<jeton> : jeton = id aléatoire 128 bits + HMAC-SHA256 tronqué par la
  clé du serveur (SHARE_SECRET, sinon DATA_DIR/.share-secret générée en 0600).
  Valide si la signature est correcte, le lien présent dans .shares.json et non
  expiré (1, 7, 30 jours ou sans expiration).
- API : GET/POST/DELETE /api/shares, POST /api/shares/revoke-all (renouvelle la
  clé), GET /api/public/{jeton} hors authentification (nom, titre, logo, scène).
- Auth Basic : /s/, /api/public/, /healthz et les fichiers statiques du build
  sont publics ; le reste de l'API et l'index restent protégés. Le serveur
  statique ne liste plus les répertoires. X-Robots-Tag: noindex sur les liens.
- Les liens suivent renommages et déplacements, disparaissent à la suppression.
- Interface : dialogue Partager (barre du haut, menu de l'explorateur), section
  Liens de partage dans Paramètres, vue publique Excalidraw en lecture seule
  avec téléchargement et bascule clair/sombre.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H6J7vKwXx7uopxKhWfFJpm
This commit is contained in:
claude BotandClaude Opus 5.5 committed 2026-10-07 18:10:09 +02:00
1 parent c1ea1e84a7
commit 50d40cd929
18 files changed
+1076 -12

No files matched your search

+91
View File
@@ -17,6 +17,11 @@
// GET /api/history/version?path=x&id=v contenu d'une version
// POST /api/history/snapshot {"path"} version du contenu courant
// POST /api/history/restore {"path","id"} restauration (ETag en en-tête)
// GET /api/shares[?path=x] liens de partage (d'un schéma ou tous)
// POST /api/shares {"path","days"} nouveau lien (days 0 : sans expiration)
// DELETE /api/shares?id=x révocation d'un lien
// POST /api/shares/revoke-all révocation de tous les liens
// GET /api/public/{jeton} schéma partagé (sans authentification)
// GET /api/library / PUT /api/library bibliothèque Excalidraw
// GET /api/settings / PUT {"title","channel"} paramètres (titre, logo, canal)
// GET /api/logo / PUT (image/png) / DELETE logo 64x64
@@ -24,11 +29,15 @@
package api
import (
"encoding/base64"
"encoding/json"
"errors"
"io"
"log/slog"
"net/http"
"path"
"strings"
"time"
"github.com/vblogio/excalidraw-go/internal/storage"
"github.com/vblogio/excalidraw-go/internal/update"
@@ -61,6 +70,11 @@ func (h *Handler) Register(mux *http.ServeMux) {
mux.HandleFunc("GET /api/history/version", h.readVersion)
mux.HandleFunc("POST /api/history/snapshot", h.snapshot)
mux.HandleFunc("POST /api/history/restore", h.restore)
mux.HandleFunc("GET /api/shares", h.shares)
mux.HandleFunc("POST /api/shares", h.createShare)
mux.HandleFunc("DELETE /api/shares", h.revokeShare)
mux.HandleFunc("POST /api/shares/revoke-all", h.revokeAllShares)
mux.HandleFunc("GET /api/public/{token}", h.public)
mux.HandleFunc("GET /api/library", h.readLibrary)
mux.HandleFunc("PUT /api/library", h.writeLibrary)
mux.HandleFunc("GET /api/settings", h.readSettings)
@@ -391,6 +405,83 @@ func (h *Handler) restore(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, entry)
}
func (h *Handler) shares(w http.ResponseWriter, r *http.Request) {
l, err := h.Store.Shares(r.URL.Query().Get("path"))
if err != nil {
fail(w, err)
return
}
w.Header().Set("Cache-Control", "no-store")
writeJSON(w, http.StatusOK, l)
}
type shareReq struct {
Path string `json:"path"`
Days int `json:"days"`
}
func (h *Handler) createShare(w http.ResponseWriter, r *http.Request) {
var req shareReq
if !h.decode(w, r, &req) {
return
}
if req.Days < 0 || req.Days > 3650 {
writeErr(w, http.StatusBadRequest, "durée de 0 (sans expiration) à 3650 jours")
return
}
sh, err := h.Store.CreateShare(req.Path, time.Duration(req.Days)*24*time.Hour)
if err != nil {
fail(w, err)
return
}
writeJSON(w, http.StatusCreated, sh)
}
func (h *Handler) revokeShare(w http.ResponseWriter, r *http.Request) {
if err := h.Store.RevokeShare(r.URL.Query().Get("id")); err != nil {
fail(w, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
func (h *Handler) revokeAllShares(w http.ResponseWriter, r *http.Request) {
if err := h.Store.RevokeAllShares(); err != nil {
fail(w, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
// public sert un schéma partagé, hors authentification (voir cmd/server).
// Seuls le nom du schéma, son contenu et l'apparence (titre, logo) sont exposés.
func (h *Handler) public(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store")
w.Header().Set("X-Robots-Tag", "noindex, nofollow")
sh, data, err := h.Store.ReadShared(r.PathValue("token"))
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
writeErr(w, http.StatusNotFound, "lien invalide, expiré ou révoqué")
} else {
fail(w, err)
}
return
}
out := struct {
Name string `json:"name"`
Title string `json:"title"`
Logo string `json:"logo,omitempty"`
Scene json.RawMessage `json:"scene"`
}{Name: strings.TrimSuffix(path.Base(sh.Path), storage.Ext), Scene: data}
if st, err := h.Store.ReadSettings(); err == nil {
out.Title = st.Title
}
if logo, err := h.Store.ReadLogo(); err == nil {
out.Logo = "data:image/png;base64," + base64.StdEncoding.EncodeToString(logo)
}
writeJSON(w, http.StatusOK, out)
}
func (h *Handler) readLibrary(w http.ResponseWriter, r *http.Request) {
data, err := h.Store.ReadLibrary()
if err != nil {