Base Up And Down (auth, thème, dialogues) + moteur fleet (script distant POSIX, analyse pull + comparaison d'images, mise à jour d'une stack), interface de suivi. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
115 lines
3.5 KiB
Go
115 lines
3.5 KiB
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"github.com/vblogio/stackwatch/internal/auth"
|
|
)
|
|
|
|
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"`
|
|
}
|
|
|
|
// meResponse est lu avant la connexion (page de connexion) puis après (utilisateur, droits).
|
|
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"`
|
|
Setup bool `json:"setup,omitempty"`
|
|
Title string `json:"title"`
|
|
Version string `json:"version,omitempty"`
|
|
|
|
User *meUser `json:"user,omitempty"`
|
|
Admin bool `json:"admin"`
|
|
CanChangePW bool `json:"canChangePassword,omitempty"`
|
|
Logout string `json:"logout,omitempty"`
|
|
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")
|
|
m := meResponse{Mode: h.Mode, Local: h.Mode == "local" || h.Mode == "both", OIDC: h.Mode == "oidc" || h.Mode == "both",
|
|
OIDCName: h.OIDCName, Registration: "closed", Title: h.Title, Version: h.Version}
|
|
if m.Local && h.Auth != nil && h.Auth.Registration != nil {
|
|
m.Registration = h.Auth.Registration()
|
|
}
|
|
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: LocalUser, Name: "Utilisateur", Role: auth.RoleAdmin}
|
|
}
|
|
if m.User != nil {
|
|
m.Admin = h.isAdmin(r)
|
|
if m.Admin && h.Accounts != nil {
|
|
m.Pending = h.Accounts.PendingCount()
|
|
}
|
|
}
|
|
writeJSON(w, http.StatusOK, m)
|
|
}
|
|
|
|
// --- Comptes ----------------------------------------------------------------------
|
|
|
|
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)
|
|
}
|