- Hors liste blanche, reniflage du contenu (UTF-8 valide, sans NUL, peu de caractères de contrôle) : .pem, .csr, fichiers sans extension... servis en text/plain avec les mêmes protections - Langage deviné côté client (highlight.js), sauf blocs PEM/PGP - Paramètres > Aperçu : préférences d'affichage par navigateur (coloration, numéros de ligne, retour à la ligne, Markdown rendu/source, CSV) ; réglages administrateur (détection, plafond 1-50 Mo, extensions texte, extensions exclues, aperçu dans les partages) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
239 lines
6.7 KiB
Go
239 lines
6.7 KiB
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"github.com/vblogio/upandown/internal/auth"
|
|
"github.com/vblogio/upandown/internal/storage"
|
|
)
|
|
|
|
type meUser struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
Email string `json:"email,omitempty"`
|
|
Role string `json:"role"`
|
|
Provider string `json:"provider,omitempty"`
|
|
}
|
|
|
|
type usage struct {
|
|
Used int64 `json:"used"`
|
|
Quota int64 `json:"quota"` // 0 : illimité
|
|
}
|
|
|
|
// meResponse est lu avant la connexion (page de connexion : fournisseurs,
|
|
// inscription, titre) puis après (utilisateur, droits, occupation).
|
|
type meResponse struct {
|
|
Mode string `json:"mode"` // none, local, oidc ou both
|
|
Local bool `json:"local"`
|
|
OIDC bool `json:"oidc"`
|
|
OIDCName string `json:"oidcName,omitempty"`
|
|
Registration string `json:"registration"` // closed, approval ou open (comptes locaux)
|
|
Setup bool `json:"setup,omitempty"`
|
|
Title string `json:"title"`
|
|
Logo string `json:"logo,omitempty"`
|
|
Version string `json:"version,omitempty"`
|
|
|
|
User *meUser `json:"user,omitempty"`
|
|
Admin bool `json:"admin"`
|
|
CanChangePW bool `json:"canChangePassword,omitempty"`
|
|
Logout string `json:"logout,omitempty"`
|
|
Usage *usage `json:"usage,omitempty"`
|
|
CommonReadonly bool `json:"commonReadonly"`
|
|
Pending int `json:"pending,omitempty"` // administrateur : inscriptions à valider
|
|
}
|
|
|
|
func (h *Handler) me(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
st, err := h.Store.ReadSettings()
|
|
if err != nil {
|
|
fail(w, err)
|
|
return
|
|
}
|
|
m := meResponse{Mode: h.Mode, Local: h.Mode == "local" || h.Mode == "both", OIDC: h.Mode == "oidc" || h.Mode == "both",
|
|
OIDCName: h.OIDCName, Registration: st.Registration, Title: st.Title, Logo: st.Logo, Version: h.Version,
|
|
CommonReadonly: st.CommonReadonly}
|
|
if !m.Local {
|
|
m.Registration = storage.RegClosed
|
|
}
|
|
if h.Accounts != nil {
|
|
m.Setup = m.Local && h.Accounts.NeedsSetup()
|
|
}
|
|
u := auth.UserFrom(r.Context())
|
|
switch {
|
|
case u != nil:
|
|
m.User = &meUser{ID: u.ID, Name: u.Name, Email: u.Email, Role: u.Role, Provider: u.Provider}
|
|
m.Logout = "/auth/logout"
|
|
m.CanChangePW = u.Provider == auth.ProviderLocal
|
|
case h.Mode == "none":
|
|
m.User = &meUser{ID: storage.LocalUser, Name: "Utilisateur", Role: auth.RoleAdmin}
|
|
}
|
|
if m.User != nil {
|
|
m.Admin = h.isAdmin(r)
|
|
if sp, err := h.Spaces.For(userID(r), m.Admin); err == nil {
|
|
used, quota := sp.Usage()
|
|
m.Usage = &usage{Used: used, Quota: quota}
|
|
}
|
|
if m.Admin && h.Accounts != nil {
|
|
m.Pending = h.Accounts.PendingCount()
|
|
}
|
|
}
|
|
writeJSON(w, http.StatusOK, m)
|
|
}
|
|
|
|
// --- Paramètres et logo ---------------------------------------------------------------
|
|
|
|
func (h *Handler) readSettings(w http.ResponseWriter, r *http.Request) {
|
|
st, err := h.Store.ReadSettings()
|
|
if err != nil {
|
|
fail(w, err)
|
|
return
|
|
}
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
writeJSON(w, http.StatusOK, st)
|
|
}
|
|
|
|
// settingsReq : seuls les champs présents sont modifiés.
|
|
type settingsReq struct {
|
|
Title *string `json:"title"`
|
|
TrashDays *int `json:"trashDays"`
|
|
Registration *string `json:"registration"`
|
|
QuotaMB *int64 `json:"quotaMB"`
|
|
CommonReadonly *bool `json:"commonReadonly"`
|
|
|
|
Preview *storage.PreviewPatch `json:"preview"`
|
|
}
|
|
|
|
func (h *Handler) writeSettings(w http.ResponseWriter, r *http.Request) {
|
|
if !h.admin(w, r) {
|
|
return
|
|
}
|
|
var req settingsReq
|
|
if !h.decode(w, r, &req) {
|
|
return
|
|
}
|
|
st, err := h.Store.ReadSettings()
|
|
if err == nil && req.Title != nil {
|
|
st, err = h.Store.WriteTitle(*req.Title)
|
|
}
|
|
if err == nil && req.Registration != nil {
|
|
st, err = h.Store.WriteRegistration(*req.Registration)
|
|
}
|
|
if err == nil && req.QuotaMB != nil {
|
|
st, err = h.Store.WriteQuota(*req.QuotaMB)
|
|
}
|
|
if err == nil && req.CommonReadonly != nil {
|
|
st, err = h.Store.WriteCommonReadonly(*req.CommonReadonly)
|
|
}
|
|
if err == nil && req.Preview != nil {
|
|
st, err = h.Store.WritePreview(*req.Preview)
|
|
}
|
|
if err == nil && req.TrashDays != nil {
|
|
if st, err = h.Store.WriteTrashDays(*req.TrashDays); err == nil {
|
|
if perr := h.Spaces.PurgeTrash(); perr != nil {
|
|
slog.Warn("poubelle : purge incomplète", "err", perr)
|
|
}
|
|
}
|
|
}
|
|
if err != nil {
|
|
fail(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, st)
|
|
}
|
|
|
|
func (h *Handler) readLogo(w http.ResponseWriter, r *http.Request) {
|
|
data, err := h.Store.ReadLogo()
|
|
if err != nil {
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
fail(w, err)
|
|
return
|
|
}
|
|
// L'URL porte la version du logo (?v=...) : un nouveau logo change l'URL.
|
|
w.Header().Set("Cache-Control", "private, max-age=86400")
|
|
w.Header().Set("Content-Type", "image/png")
|
|
_, _ = w.Write(data)
|
|
}
|
|
|
|
func (h *Handler) writeLogo(w http.ResponseWriter, r *http.Request) {
|
|
if !h.admin(w, r) {
|
|
return
|
|
}
|
|
data, err := io.ReadAll(http.MaxBytesReader(w, r.Body, storage.MaxLogoUpload))
|
|
if err != nil {
|
|
fail(w, err)
|
|
return
|
|
}
|
|
st, err := h.Store.WriteLogo(data)
|
|
if err != nil {
|
|
fail(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, st)
|
|
}
|
|
|
|
func (h *Handler) deleteLogo(w http.ResponseWriter, r *http.Request) {
|
|
if !h.admin(w, r) {
|
|
return
|
|
}
|
|
st, err := h.Store.DeleteLogo()
|
|
if err != nil {
|
|
fail(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, st)
|
|
}
|
|
|
|
// --- Compte ------------------------------------------------------------------------------
|
|
|
|
func accountErr(w http.ResponseWriter, err error) {
|
|
switch {
|
|
case errors.Is(err, auth.ErrNoAccount):
|
|
writeErr(w, http.StatusNotFound, err.Error())
|
|
case errors.Is(err, auth.ErrBadCredentials):
|
|
writeErr(w, http.StatusForbidden, "mot de passe actuel incorrect")
|
|
case errors.Is(err, auth.ErrInvalidAccount), errors.Is(err, auth.ErrLastAdmin), errors.Is(err, auth.ErrAccountExists):
|
|
writeErr(w, http.StatusBadRequest, err.Error())
|
|
default:
|
|
slog.Error("compte", "err", err)
|
|
writeErr(w, http.StatusInternalServerError, "erreur interne")
|
|
}
|
|
}
|
|
|
|
func (h *Handler) changePassword(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
Current string `json:"current"`
|
|
New string `json:"new"`
|
|
}
|
|
if !h.decode(w, r, &req) {
|
|
return
|
|
}
|
|
if err := h.Accounts.ChangePassword(userID(r), req.Current, req.New); err != nil {
|
|
accountErr(w, err)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (h *Handler) patchAccount(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
Name *string `json:"name"`
|
|
Email *string `json:"email"`
|
|
}
|
|
if !h.decode(w, r, &req) {
|
|
return
|
|
}
|
|
acc, ok := h.Accounts.Get(userID(r))
|
|
if !ok || acc.Provider != auth.ProviderLocal {
|
|
writeErr(w, http.StatusBadRequest, "profil géré par le fournisseur d'identité")
|
|
return
|
|
}
|
|
if _, err := h.Accounts.Update(acc.ID, auth.Patch{Name: req.Name, Email: req.Email}); err != nil {
|
|
accountErr(w, err)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|