Files
upstream-centrifugo/internal/middleware/user_header_auth.go
upstream-mirror b352a9a30d
Some checks failed
test / Lint (push) Failing after 3s
test / Test with Go 1.26.8 (push) Failing after 2s
snapshot of https://github.com/centrifugal/centrifugo.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:17:21 +05:00

26 lines
669 B
Go

package middleware
import (
"net/http"
"github.com/centrifugal/centrifuge"
)
// UserHeaderAuth is a middleware that extracts the value of user ID from the specific header
// and sets connection credentials.
func UserHeaderAuth(userHeaderName string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
userID := r.Header.Get(userHeaderName)
if userID != "" {
ctx := centrifuge.SetCredentials(r.Context(), &centrifuge.Credentials{
UserID: userID,
})
r = r.WithContext(ctx)
}
// Call the next handler
next.ServeHTTP(w, r)
})
}
}