Authenticating gRPC services: metadata, mTLS, and interceptors
When you move from REST to gRPC, the familiar handles for authentication quietly disappear. There are no cookies. There's no Authorization header in the HTTP sense you reach for by habit, and definitely no browser session. Instead you have HTTP/2 streams, binary framing, and a concept called metadata that most teams either misuse or ignore until a security review flags it. The good news is that gRPC's structure—typed services, a uniform call pipeline, first-class streaming—actually makes centralized, consistent auth easier than in a sprawl of REST handlers, once you know where the seams are.
Two questions, two mechanisms
gRPC auth almost always splits into two distinct concerns that people conflate. Who is this caller? — the identity of the service or user on the other end. What is this request carrying? — the token or claim that authorizes this particular call. mTLS answers the first; metadata carries the second. You usually want both, and they protect against different things.
Carrying tokens in metadata
Metadata is gRPC's key-value side channel, conceptually similar to HTTP headers and in fact mapped onto HTTP/2 headers on the wire. It's where a per-call credential belongs. The convention is an authorization key with a bearer token, exactly mirroring REST so your token-issuing infrastructure doesn't need a gRPC-specific path.
// client: attach a token to every call via call credentials
import { credentials, Metadata } from "@grpc/grpc-js";
const callCreds = credentials.createFromMetadataGenerator((_params, cb) => {
const md = new Metadata();
md.set("authorization", `Bearer ${getAccessToken()}`);
cb(null, md);
});
// combine channel (TLS) creds with per-call (token) creds
const channelCreds = credentials.combineChannelCredentials(
credentials.createSsl(), // mTLS material goes here
callCreds
);Two details matter. First, metadata keys ending in -bin are treated as binary; everything else must be valid ASCII, so base64-encode anything that isn't. Second, use call credentials rather than manually stuffing metadata at each call site—the generator runs per call, so token refresh happens transparently and you never ship a stale token because one call site forgot to re-read it.
Call credentials require a secure channel. gRPC refuses to attach call credentials over a plaintext connection—by design, because sending a bearer token in cleartext metadata is exactly as bad as it sounds. If your token "isn't being sent," the first thing to check is whether the channel is actually TLS. This is a feature, not a bug; don't disable it to "make it work."
mTLS: authenticating the service itself
A bearer token proves the caller holds a credential. It doesn't prove the caller is the service you think it is—a leaked token works from anywhere. Mutual TLS closes that gap by having both ends present certificates. The server verifies the client's cert against a trusted CA, so a connection from an unrecognized workload is refused at the transport layer, before a single RPC method runs.
In a service mesh this is often handled for you—the sidecar terminates mTLS and injects the verified peer identity—but even then your application code should read that identity rather than trust the network blindly. The peer certificate's subject (or a SPIFFE identity embedded in it) tells you which service is actually calling, independent of whatever the token claims.
Defense in depth, not either/or
mTLS answers "is this a trusted workload on my network?" The token answers "is this request authorized to do this specific thing, on behalf of this specific user?" A compromised-but-still-trusted service could present a valid cert while making a request it has no business making; the token-level check catches that. Dropping either layer leaves a real gap.
Centralize with an interceptor
The feature that makes gRPC auth pleasant is the interceptor—middleware that wraps every incoming call. Instead of each method re-implementing token extraction and validation (and one of them getting it subtly wrong), you validate once, attach the resulting identity to the call context, and let handlers assume they're already authenticated.
// server: one interceptor validates every unary and streaming call
async function authInterceptor(call, methodDescriptor, next) {
const raw = call.metadata.get("authorization")[0] as string | undefined;
if (!raw?.startsWith("Bearer ")) {
throw { code: status.UNAUTHENTICATED, details: "missing token" };
}
let claims;
try {
claims = await verifyJwt(raw.slice(7)); // local verify, cached JWKS
} catch {
throw { code: status.UNAUTHENTICATED, details: "invalid token" };
}
// per-method authorization from the service definition
const required = requiredScope(methodDescriptor.path); // e.g. "/pkg.Svc/Delete"
if (required && !claims.scopes.includes(required)) {
throw { code: status.PERMISSION_DENIED, details: "insufficient scope" };
}
call.context = { ...call.context, identity: claims };
return next(call);
}Map the method path to a required scope in one table, and adding a new RPC forces a conscious decision about what authorizes it—rather than inheriting whatever the last method happened to check. Return the right status codes: UNAUTHENTICATED (16) when you can't establish who the caller is, PERMISSION_DENIED (7) when you know who they are but they're not allowed. Clients and dashboards distinguish these, and conflating them makes debugging miserable.
Streaming needs per-message thought. On a long-lived stream, the token validated at open can expire mid-stream. For short streams, validate at the start and accept the window. For long ones, either re-check expiry at message boundaries or tie the stream's lifetime to the token's—don't let a stream opened with a now-expired token keep flowing for hours because nobody re-checked.
Propagating identity between hops
One RPC often triggers another: the API gateway calls the orders service, which calls inventory. The user's identity has to survive each hop without inventory blindly trusting whatever orders claims. The clean pattern is token exchange—each service presents its own service identity (via mTLS) plus a downscoped token representing the original user, rather than forwarding the raw inbound token wholesale. That way inventory sees both "a trusted service called me" and "on behalf of this user, with these reduced scopes," and a compromise of one hop can't replay the user's full-powered token everywhere.
gRPC rewards centralization: one interceptor, one metadata convention, one mTLS trust domain, and your entire service graph authenticates consistently instead of each team reinventing it. That's far simpler when the thing issuing and verifying those tokens is infrastructure you run and can shape to your mesh. Owning your identity layer lets you align token scopes, mTLS trust, and per-method policy under one roof; Bastionary issues the short-lived, scoped tokens and publishes the JWKS these interceptors verify against locally.
Run it yourself
Bastionary is free, open-source software. Follow the quick start to have it running on your own server, or browse the releases on Git.