Pulse/internal/cloudcp/cpsec/nonce.go
2026-03-18 16:06:30 +00:00

21 lines
610 B
Go

// Package cpsec provides shared security primitives for the Cloud CP server
// and its sub-packages.
package cpsec
import "context"
// nonceKey is the context key for the per-request CSP nonce.
type nonceKey struct{}
// WithNonce returns a copy of ctx carrying the given CSP nonce.
func WithNonce(ctx context.Context, nonce string) context.Context {
return context.WithValue(ctx, nonceKey{}, nonce)
}
// NonceFromContext returns the CSP nonce stored in ctx, or "" if absent.
func NonceFromContext(ctx context.Context) string {
if v, ok := ctx.Value(nonceKey{}).(string); ok {
return v
}
return ""
}