Client-Side Auth Interceptors
Server-side interceptors (createGatewayAuthInterceptor, createJwtAuthInterceptor, etc.) validate incoming requests. Client-side interceptors do the opposite -- they attach credentials to outgoing requests.
Connectum provides two client interceptor factories for common auth patterns:
| Factory | Purpose | Header set |
|---|---|---|
createClientBearerInterceptor | Attach a Bearer token | Authorization: Bearer <token> |
createClientGatewayInterceptor | Service-to-service trust | x-gateway-secret, x-auth-subject, x-auth-roles |
Bearer Token
Use createClientBearerInterceptor when your service calls another service that expects a Bearer token (JWT, opaque token, API key in Bearer format).
Static token
import { createClientBearerInterceptor } from '@connectum/auth';
import { createConnectTransport } from '@connectrpc/connect-node';
const transport = createConnectTransport({
baseUrl: 'http://internal-api:5000',
httpVersion: '1.1',
interceptors: [
createClientBearerInterceptor({ token: process.env.API_TOKEN! }),
],
});Async token factory (refresh flow)
When the token may expire, pass an async function. It is called before every request:
import { createClientBearerInterceptor } from '@connectum/auth';
const bearerAuth = createClientBearerInterceptor({
token: async () => {
const { accessToken } = await refreshTokenIfNeeded();
return accessToken;
},
});A static token must be a non-empty string: createClientBearerInterceptor throws a TypeError when it is created with anything else, which is what an unset environment variable gives (process.env.API_TOKEN is undefined). A factory that resolves to anything but a non-empty string, for example undefined or null after a failed refresh, fails that call on the client with Code.Unauthenticated and the message @connectum/auth: createClientBearerInterceptor token factory returned ...; expected a non-empty string. No request is sent, so the server never sees Authorization: Bearer undefined. A factory that throws keeps its own error. See Auth authorization and JWT validation when upgrading.
token accepts either a static credential or an async factory. See ClientBearerInterceptorOptions for the exact contract.
Gateway (Service-to-Service)
Use createClientGatewayInterceptor when calling a service behind a gateway that uses createGatewayAuthInterceptor on the server side. The client interceptor sets the trust headers that the server interceptor reads.
import { createClientGatewayInterceptor } from '@connectum/auth';
import { createConnectTransport } from '@connectrpc/connect-node';
const transport = createConnectTransport({
baseUrl: 'http://order-service:5001',
httpVersion: '1.1',
interceptors: [
createClientGatewayInterceptor({
secret: process.env.GATEWAY_SECRET!,
subject: 'task-coordinator',
roles: ['service', 'order-writer'],
}),
],
});Creating the interceptor throws if the secret is empty, contains NUL, CR, LF or a character above U+00FF, or starts or ends with a space or tab (the platform removes that whitespace from a header value, so the secret would never match), if the subject is empty or contains NUL, CR, LF or a lone surrogate, or if roles is not an array of strings without holes that fits the roles header (every character above U+00FF counts as six there). The roles are checked again on each call, so a change made after creation is refused with InvalidArgument. A subject over 512 characters is refused at creation as well. Provide a secret that matches the server trust source and a stable service subject; roles are optional. secret and subject must be non-empty strings: an unset environment variable throws a TypeError when the interceptor is created, where it used to send x-gateway-secret: undefined. See ClientGatewayInterceptorOptions for exact fields.
The plaintext URLs above are for a trusted local development environment. Use TLS for hops carrying bearer tokens or gateway secrets in production; the transport matrix explains which httpVersion to choose for the receiving server.
Header mapping
The client interceptor sets these headers:
| Header | Source | Server reads via |
|---|---|---|
x-gateway-secret | options.secret | trustSource.header |
x-auth-subject | options.subject | headerMapping.subject |
x-auth-roles | JSON.stringify(options.roles) | headerMapping.roles |
When roles is omitted or empty, the x-auth-roles header is not set.
headerMapping must match the client headers
createClientGatewayInterceptor always sends the fixed header names x-auth-subject and x-auth-roles. The server's createGatewayAuthInterceptor reads whatever names its headerMapping declares. The headerMapping examples in the Gateway Authentication guide use x-user-id / x-user-roles, which do not match these client headers. To pair the two interceptors out of the box, configure the server with a matching mapping:
const gatewayAuth = createGatewayAuthInterceptor({
headerMapping: {
subject: 'x-auth-subject',
roles: 'x-auth-roles',
},
trustSource: {
header: 'x-gateway-secret',
expectedValues: [process.env.GATEWAY_SECRET!],
},
});Combining with other interceptors
Client auth interceptors compose naturally with other interceptors:
import { createClientGatewayInterceptor } from '@connectum/auth';
import { createOtelClientInterceptor } from '@connectum/otel';
import { createConnectTransport } from '@connectrpc/connect-node';
const transport = createConnectTransport({
baseUrl: 'http://order-service:5001',
httpVersion: '1.1',
interceptors: [
createOtelClientInterceptor({ // tracing
serverAddress: 'order-service',
serverPort: 5001,
}),
createClientGatewayInterceptor({ // auth
secret: process.env.GATEWAY_SECRET!,
subject: 'api-gateway',
roles: ['gateway'],
}),
],
});Server ↔ Client pairing
| Client interceptor | Server interceptor | Trust mechanism |
|---|---|---|
createClientBearerInterceptor | createJwtAuthInterceptor | JWT signature verification |
createClientBearerInterceptor | createSessionAuthInterceptor | Session token lookup |
createClientGatewayInterceptor | createGatewayAuthInterceptor | Shared secret header |
Verify
Observe outgoing headers in a test transport and confirm the bearer or gateway headers shown above. Call the paired server with a valid credential, then a wrong one: the server should admit the valid credential and return Code.Unauthenticated for the wrong one. The client interceptor only attaches credentials; it does not validate them or authorize the method.