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
This commit is contained in:
Jakub Zych
2026-10-23 16:21:37 +02:00
parent 0ec00f7f69
commit 24cf6cd931
16 changed files with 158 additions and 31 deletions

View File

@@ -22,7 +22,7 @@ Use cases:
## Features
- Four independent string values, each with a setter and a getter: actor (`towel.WithActor`, `towel.Actor`), organization (`towel.WithOrganization`, `towel.Organization`), collection (`towel.WithCollection`, `towel.Collection`) and locale (`towel.WithLocale`, `towel.Locale`).
- Five independent string values, each with a setter and a getter: actor (`towel.WithActor`, `towel.Actor`), organization (`towel.WithOrganization`, `towel.Organization`), collection (`towel.WithCollection`, `towel.Collection`), locale (`towel.WithLocale`, `towel.Locale`) and client address (`towel.WithClientIP`, `towel.ClientIP`).
- Unexported context key types, so no other package can read or overwrite the values by accident.
- Getters return `(value, ok)`: a value that was set to an empty string is distinguishable from one that was never set.
- Nil-safe: a setter given a nil context starts from `context.Background()`, and a getter given a nil context reports `false`.
@@ -66,6 +66,7 @@ func listPosts(w http.ResponseWriter, r *http.Request) {
| `towel.WithOrganization` / `towel.Organization` | Store and read the organization (tenant) the request belongs to. |
| `towel.WithCollection` / `towel.Collection` | Store and read the collection the request operates on. |
| `towel.WithLocale` / `towel.Locale` | Store and read the locale used for translations and localized responses. |
| `towel.WithClientIP` / `towel.ClientIP` | Store and read the request's client address for code below the HTTP layer, such as a token resolver logging a failed attempt. |
## Dependencies

View File

@@ -6,6 +6,7 @@ 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 {
@@ -61,3 +62,16 @@ func WithLocale(ctx context.Context, locale string) context.Context {
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{})
}