add owner handle

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-06-14 13:40:51 +02:00
commit f862abe5a4
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
12 changed files with 274 additions and 55 deletions

41
internal/auth/auth.go Normal file
View file

@ -0,0 +1,41 @@
package auth
import (
"context"
"fmt"
"net/http"
"strconv"
)
type contextKey struct{}
func WithOwner(ctx context.Context, ownerID int32) context.Context {
return context.WithValue(ctx, contextKey{}, ownerID)
}
func OwnerFromContext(ctx context.Context) (int32, bool) {
id, ok := ctx.Value(contextKey{}).(int32)
return id, ok
}
// Middleware extrait X-Owner-ID du header et l'injecte dans le contexte.
// Pour le dev, c'est l'ID numérique de l'owner. À remplacer par JWT en prod.
func Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
v := r.Header.Get("X-Owner-ID")
if v == "" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintln(w, `{"error":"missing X-Owner-ID header"}`)
return
}
id, err := strconv.Atoi(v)
if err != nil || id <= 0 {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintln(w, `{"error":"invalid X-Owner-ID"}`)
return
}
next.ServeHTTP(w, r.WithContext(WithOwner(r.Context(), int32(id))))
})
}