Files
summercms/modules/towel/context.go
Jakub Zych 24cf6cd931 fix(pier): end only the revoked token's or client's MCP sessions and log the client address (B-WR-04, B-IN-04)
- pact.MCPSubjectChanged and pier.Event gain TokenID and ClientID; a
  narrowed revoked event closes only matching sessions and stateless servers
- cabana fires the admin token revoke with the token row id
- towel.WithClientIP/ClientIP; pier puts the address (surf applies
  http.trusted_proxies) on the resolve context and ResolveBackendToken logs it
2026-10-23 16:21:37 +02:00

78 lines
2.2 KiB
Go

package towel
import "context"
type actorKey struct{}
type organizationKey struct{}
type collectionKey struct{}
type localeKey struct{}
type clientIPKey struct{}
func withValue(ctx context.Context, key, value any) context.Context {
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(ctx, key, value)
}
func stringValue(ctx context.Context, key any) (string, bool) {
if ctx == nil {
return "", false
}
v, ok := ctx.Value(key).(string)
return v, ok
}
// WithActor stores the request actor on ctx.
func WithActor(ctx context.Context, actor string) context.Context {
return withValue(ctx, actorKey{}, actor)
}
// Actor returns the request actor from ctx.
func Actor(ctx context.Context) (string, bool) {
return stringValue(ctx, actorKey{})
}
// WithOrganization stores the request organization on ctx.
func WithOrganization(ctx context.Context, org string) context.Context {
return withValue(ctx, organizationKey{}, org)
}
// Organization returns the request organization from ctx.
func Organization(ctx context.Context) (string, bool) {
return stringValue(ctx, organizationKey{})
}
// WithCollection stores the request collection on ctx.
func WithCollection(ctx context.Context, collection string) context.Context {
return withValue(ctx, collectionKey{}, collection)
}
// Collection returns the request collection from ctx.
func Collection(ctx context.Context) (string, bool) {
return stringValue(ctx, collectionKey{})
}
// WithLocale stores the request locale on ctx.
func WithLocale(ctx context.Context, locale string) context.Context {
return withValue(ctx, localeKey{}, locale)
}
// Locale returns the request locale from ctx.
func Locale(ctx context.Context) (string, bool) {
return stringValue(ctx, localeKey{})
}
// WithClientIP stores the request's client address on ctx, for code that
// runs below the HTTP layer (for example a token resolver logging a failed
// attempt). The caller decides how the address was derived (trusted proxy
// headers or the socket peer).
func WithClientIP(ctx context.Context, ip string) context.Context {
return withValue(ctx, clientIPKey{}, ip)
}
// ClientIP returns the client address stored by WithClientIP.
func ClientIP(ctx context.Context) (string, bool) {
return stringValue(ctx, clientIPKey{})
}