caelum0x ed0dbe14b5 feat(gateway,cli,tui,agent): enforce tenant-scoped auth by default
Close THREAT-MODEL.md's top-named gap end-to-end: services/cli,
services/tui and services/agent each mint an operator-scoped
credential (new internal/authclient in all three, mirroring gateway's
existing authclient) at startup and attach it as x-api-key/
authorization metadata on every gateway call, so
RATION_GATEWAY_REQUIRE_TENANT_AUTH now defaults to true instead of
false.

Also close the two named sub-gaps this left open:
- GetCard/AdjustCardLimit carry only card_id, not tenant_id, so
  tenantauth and ratelimit previously passed them through unscoped.
  Add internal/gateway/cardtenant, a cached card_id -> tenant_id
  resolver backed by card-issuance, and use it in both interceptors.
- StreamActivity accepted an empty tenant_id (meaning "all tenants")
  from any caller, authenticated or not. Add
  tenantauth.StreamServerInterceptor: a non-operator credential may
  only subscribe to its own tenant_id; only an operator credential may
  request the all-tenants view.
2026-10-23 18:09:11 +03:00

35 lines
1.5 KiB
Go

package authclient
import (
"context"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
// bearerHeader must match services/gateway/internal/tenantauth's
// authorizationHeader constant exactly — these two sides of the same
// credential exchange are defined independently (different Go modules)
// but must agree on the wire.
const bearerHeader = "authorization"
// UnaryClientInterceptor attaches "authorization: Bearer <token>" to
// every outgoing unary RPC's context, so a caller only has to mint a
// credential once (see Client.MintOperatorJWT) and dial with this
// interceptor, rather than remembering to attach it at every call site.
func UnaryClientInterceptor(token string) grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
ctx = metadata.AppendToOutgoingContext(ctx, bearerHeader, "Bearer "+token)
return invoker(ctx, method, req, reply, cc, opts...)
}
}
// StreamClientInterceptor is UnaryClientInterceptor's counterpart for
// streaming RPCs (e.g. StreamActivity).
func StreamClientInterceptor(token string) grpc.StreamClientInterceptor {
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
ctx = metadata.AppendToOutgoingContext(ctx, bearerHeader, "Bearer "+token)
return streamer(ctx, desc, cc, method, opts...)
}
}