feat(auth): X-Auth-Request-User context middleware

This commit is contained in:
Mathias Bergqvist
2026-05-04 21:05:16 +02:00
parent c6c328e517
commit 0b5daca198
2 changed files with 54 additions and 0 deletions

26
internal/auth/caller.go Normal file
View File

@@ -0,0 +1,26 @@
package auth
import (
"context"
"net/http"
)
type ctxKey struct{}
func CallerMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user := r.Header.Get("X-Auth-Request-User")
if user == "" {
user = r.Header.Get("X-Forwarded-User")
}
ctx := context.WithValue(r.Context(), ctxKey{}, user)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func Caller(ctx context.Context) string {
if v, ok := ctx.Value(ctxKey{}).(string); ok {
return v
}
return ""
}