package datasource import "context" // userKey is the unexported context key under which the end-user identity is // stored. Using a private type prevents collisions with other packages. type userKey struct{} // WithUser returns a copy of ctx carrying the end-user identity associated with // the request (e.g. the authenticated web client). Data sources may use this to // attribute operations to the actual user rather than the server process — for // example EPICS Channel Access access-security rules match on the client // username. An empty user is treated as "no client identity". func WithUser(ctx context.Context, user string) context.Context { if user == "" { return ctx } return context.WithValue(ctx, userKey{}, user) } // UserFrom returns the end-user identity stored in ctx by WithUser. The boolean // is false when no (non-empty) identity is present, in which case callers // should fall back to the server's own identity. func UserFrom(ctx context.Context) (string, bool) { u, ok := ctx.Value(userKey{}).(string) return u, ok && u != "" }