feat: add tenant authorization middleware

This commit is contained in:
Philipp
2026-06-11 08:14:22 +02:00
parent b08b52dd1b
commit 452d4c5f71
10 changed files with 432 additions and 1 deletions
@@ -0,0 +1,33 @@
package authorization
import (
"encoding/json"
"net/http"
"proxui/backend/internal/membership"
"proxui/backend/internal/rbac"
)
type Middleware struct{}
func NewMiddleware() Middleware {
return Middleware{}
}
func (m Middleware) Require(action rbac.Action, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
membership, ok := membership.FromRequest(r)
if !ok || !rbac.Can(membership.Role, action) {
writeError(w, http.StatusForbidden, "forbidden")
return
}
next.ServeHTTP(w, r)
})
}
func writeError(w http.ResponseWriter, status int, message string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(map[string]string{"error": message})
}