add a simple auth function

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-04-25 20:11:46 +02:00
commit 87fe581353
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
10 changed files with 206 additions and 8 deletions

View file

@ -26,6 +26,10 @@ func (h *SnapshotHandler) Create(w http.ResponseWriter, r *http.Request) {
Error(w, http.StatusBadRequest, "invalid id")
return
}
if err := Authorize(r.Context(), ActionSnapshotCreate, repoID); err != nil {
Error(w, http.StatusForbidden, "forbidden")
return
}
var req CreateSnapshotRequest
json.NewDecoder(r.Body).Decode(&req)
if req.Label == "" {
@ -49,6 +53,10 @@ func (h *SnapshotHandler) List(w http.ResponseWriter, r *http.Request) {
Error(w, http.StatusBadRequest, "invalid id")
return
}
if err := Authorize(r.Context(), ActionSnapshotRead, repoID); err != nil {
Error(w, http.StatusForbidden, "forbidden")
return
}
snaps, err := h.svc.List(r.Context(), repoID)
if errors.Is(err, store.ErrNotFound) {
Error(w, http.StatusNotFound, "repo not found")
@ -75,6 +83,10 @@ func (h *SnapshotHandler) Get(w http.ResponseWriter, r *http.Request) {
Error(w, http.StatusBadRequest, "invalid snap_id")
return
}
if err := Authorize(r.Context(), ActionSnapshotRead, repoID); err != nil {
Error(w, http.StatusForbidden, "forbidden")
return
}
snap, pkgs, err := h.svc.Get(r.Context(), repoID, snapID)
if errors.Is(err, store.ErrNotFound) {
Error(w, http.StatusNotFound, "snapshot not found")
@ -104,6 +116,10 @@ func (h *SnapshotHandler) Delete(w http.ResponseWriter, r *http.Request) {
Error(w, http.StatusBadRequest, "invalid snap_id")
return
}
if err := Authorize(r.Context(), ActionSnapshotDelete, repoID); err != nil {
Error(w, http.StatusForbidden, "forbidden")
return
}
if err := h.svc.Delete(r.Context(), repoID, snapID); errors.Is(err, store.ErrNotFound) {
Error(w, http.StatusNotFound, "snapshot not found")
return
@ -120,6 +136,10 @@ func (h *SnapshotHandler) Diff(w http.ResponseWriter, r *http.Request) {
Error(w, http.StatusBadRequest, "invalid id")
return
}
if err := Authorize(r.Context(), ActionSnapshotRead, repoID); err != nil {
Error(w, http.StatusForbidden, "forbidden")
return
}
fromID, err := strconv.ParseInt(r.URL.Query().Get("from"), 10, 64)
if err != nil {
Error(w, http.StatusBadRequest, "invalid from param")
@ -163,6 +183,10 @@ func (h *SnapshotHandler) Rollback(w http.ResponseWriter, r *http.Request) {
Error(w, http.StatusBadRequest, "invalid snap_id")
return
}
if err := Authorize(r.Context(), ActionSnapshotRollback, repoID); err != nil {
Error(w, http.StatusForbidden, "forbidden")
return
}
if err := h.svc.Rollback(r.Context(), repoID, snapID); errors.Is(err, store.ErrNotFound) {
Error(w, http.StatusNotFound, "snapshot not found")
return