package middleware import ( "encoding/json" "net/http" ) func Auth(expectedClientID, expectedAPIKey string, next http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { clientID := r.Header.Get("Client-Id") apiKey := r.Header.Get("Api-Key") if clientID == "" || apiKey == "" { writeError(w, 401, "missing credentials: Client-Id and Api-Key headers are required") return } if expectedClientID != "" && clientID != expectedClientID { writeError(w, 403, "invalid client id") return } if expectedAPIKey != "" && apiKey != expectedAPIKey { writeError(w, 403, "invalid api key") return } next(w, r) } } func writeError(w http.ResponseWriter, code int, msg string) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(code) json.NewEncoder(w).Encode(map[string]any{ "code": code, "message": msg, }) }