AuthN/AuthZ
Bearer JWTs in call metadata, a server interceptor that checks signature and expiry, and per-method authorization by scope.
Learning objectives
By the end of this chapter you can:
- Write a server interceptor that verifies a JWT’s HMAC signature and expiry and returns
UNAUTHENTICATEDon any failure. - Separate authentication (
UNAUTHENTICATED) from per-method authorization (PERMISSION_DENIED) with a method-to-scope map. - Compare signatures in constant time with
MessageDigest.isEqual. - Explain why mTLS (connection identity) and JWTs (request identity) complement each other.
- Mint valid, expired, bad-signature and wrong-scope tokens to exercise every interceptor branch.
Why this matters
A presence check on the authorization header accepts Bearer demo-token exactly as it accepts a forged string, because presence is the whole test. A real check decodes the token, verifies a signature, compares an expiry to the clock and reads a scope to decide not only whether the caller is who they claim, but what they may call. RoutingService.GetQuote is unchanged: no proto field moves, because authentication and authorization live in metadata and in the interceptor in front of the handler.
A JWT in metadata
A JSON Web Token is three base64url segments joined by dots: a header naming the algorithm ({"alg":"HS256"}), a payload of claims (sub, scope, iat, exp) and a signature over the first two segments that nobody can forge without the key. A JWT is a string, so it rides in call metadata as the value of the authorization key, conventionally prefixed Bearer . gRPC treats it as opaque; all the work is on the receiving end.
Two properties of metadata matter here:
- Headers arrive first. Metadata travels in the HTTP/2 HEADERS frame, ahead of the DATA frame with the request message, so a token can be rejected before any work is spent deserializing the body.
- Metadata is per call. A reused connection can carry a fresh token on one call and an expired one on the next, and the server cannot tell from the connection alone. Every call proves its own credential.
Authentication and authorization
The interceptor makes two decisions, in a fixed order:
| Question | Failure | Status |
|---|---|---|
| Is this caller who they claim to be? | Missing token, bad signature, expired token | UNAUTHENTICATED |
| Is this caller allowed to do this? | Valid token without the method’s scope | PERMISSION_DENIED |
Authorization is asked only of a token that already authenticated. Collapsing the two into one status hides which happened, and they need different responses: a client re-authenticates for the first and must obtain a different grant for the second. Reflection is exempt from both checks.
Per-method scopes
Authorization is a plain map from method name to the scope it requires. The dict fits because lookup is an exact match on a small fixed set:
METHOD_SCOPES: dict[str, str] = {
"shipping.routing.v1.RoutingService/GetQuote": "quote:read",
"shipping.routing.v1.RoutingService/ListRoutes": "routes:admin",
}
The leading / of the wire-format method string is stripped before lookup, because the Java side’s getFullMethodName() has none. A demo token carrying quote:read gets OK from GetQuote and PERMISSION_DENIED from ListRoutes, however well signed and unexpired it is. A method with no entry (FindOptimalRoute, NegotiateRoute, RecomputeRoutes) is authenticated-only, not open. A service that means to restrict one adds it to the map.
The PERMISSION_DENIED for ListRoutes fires before the handler runs. That handler is not implemented and would answer UNIMPLEMENTED, so the denial is the authorization decision itself, independent of what sits behind it.
The shared secret is a demo prop
Both stacks verify against one hardcoded string, ch20-demo-shared-hmac-secret-DO-NOT-USE-IN-PRODUCTION. That suits a self-contained example with no identity provider and is wrong for anything that ships: a secret in a repository is a secret every contributor and clone has seen. Production verifies an asymmetric signature (RS256 or ES256) against the provider's public keys from a JWKS endpoint, so the verifier holds no key that could forge a token. In Quarkus that is quarkus-oidc, not a hand-rolled Mac.
Verifying the signature
Both stacks share one claim shape and one scope map, and verify with different mechanisms. The contract, what a valid token looks like, is independent of how a stack checks it.
Python. verify_token calls PyJWT’s jwt.decode(token, SECRET, algorithms=["HS256"]) and collapses PyJWT’s InvalidTokenError hierarchy (ExpiredSignatureError, InvalidSignatureError, DecodeError) into one TokenError. Every one of those maps to UNAUTHENTICATED, so the interceptor has nothing to branch on. A header that is present but malformed is treated like an absent one, since neither is a usable credential. The scope claim is split on whitespace into a set, the OAuth2 convention for a space-delimited scope string.
Quarkus. JwtVerifier is written by hand to show what a signature check does. It splits the token into exactly three segments, recomputes HmacSHA256 over header.payload with the JDK’s javax.crypto.Mac, and compares:
if (!MessageDigest.isEqual(expectedSignature, actualSignature)) {
throw new TokenError("signature verification failed");
}
MessageDigest.isEqual runs in time independent of where the first mismatch occurs. A short-circuiting comparison leaks how many leading bytes were correct, and an attacker who can time rejections recovers a valid signature one byte at a time. That would turn the MAC into a timing oracle. Only after the signature passes does the verifier decode the payload with Jackson and check exp against the clock. A malformed token, a bad signature and an expired exp throw the same TokenError.
Both interceptors exempt reflection first. The Python interceptor resolves the real handler with continuation(handler_call_details) without invoking it, then either returns it or substitutes a handler that aborts with the right status. The substitute matches the real handler’s unary or streaming shape, so the same interceptor protects NegotiateRoute’s bidirectional stream and GetQuote’s unary call. The Quarkus AuthzInterceptor is an @ApplicationScoped ServerInterceptor registered with @GlobalInterceptor. A rejection calls call.close(status, new Metadata()) and never calls next.startCall, so RoutingServiceImpl never runs. An accepted call is wrapped in a ForwardingServerCall.SimpleForwardingServerCall that logs the status the handler closes with.
Tokens come from a minter bundled in the Python image, app.mint_token <case>, which prints one line: the encoded token. Claims are built fresh on each run, with exp five minutes out. expired sets exp to five minutes ago, wrong-scope carries shipments:write, which satisfies neither scope, and bad-sig signs with a different secret. Shipping the signer in the server’s image keeps the claim shape it produces from drifting away from what the interceptor expects.
Decision
Check signature, expiry and scope in an interceptor, not in each handler: the check is identical for every method, so it belongs in the chain, and the method-to-scope map keeps the whole policy in one place. Use a JWT verified against the provider's JWKS when identity must travel with the call, through a gateway or across hops that share no TLS session. Use mTLS when identity is a property of the connection, as with sidecar-to-sidecar identity in a mesh. Systems commonly use both: mTLS secures each hop and the JWT survives across hops. Wherever a shared HS256 secret appears in a real service, replace it with an asymmetric algorithm verified against the provider's JWKS.
🤖 Explore with your agent
“In
examples/20-authn-authz, require a newroutes:readscope onFindOptimalRouteinMETHOD_SCOPES(python/app/auth.py) and in the QuarkusAuthzInterceptor, and add aroutes-readercase toapp/mint_token.pywhose space-delimitedscopeclaim isquote:read routes:read. Keep authentication failures asUNAUTHENTICATEDand a missing scope asPERMISSION_DENIED, and do not weaken theMessageDigest.isEqualcomparison.”Check: call
FindOptimalRoutewithgrpcurl -H "authorization: Bearer $TOKEN"on ports 9200 and 9201. Thevalidtoken (onlyquote:read) must returnPermissionDenied; theroutes-readertoken must pass the interceptor and reach the unimplemented handler (Unimplemented).
Build, run, observe
cd examples/20-authn-authz
./verify.sh
The script starts both services with docker compose, waits for reflection, mints the four token cases at runtime and drives localhost:9200 (Quarkus) and localhost:9201 (Python). Reflection answers with no token, a valid token returns the GetQuote quote, a missing, bad-sig or expired token gets UNAUTHENTICATED, and a wrong-scope token calling ListRoutes gets PERMISSION_DENIED. It prints PASS when both stacks agree.
By hand, a call with no token fails with Code: Unauthenticated, and a valid token calling ListRoutes fails with Code: PermissionDenied and the message token lacks required scope 'routes:admin':
TOKEN="$(docker compose -p ch20-auth run --rm --entrypoint python python -m app.mint_token valid)"
grpcurl -plaintext -H "authorization: Bearer $TOKEN" -d '{}' \
localhost:9200 shipping.routing.v1.RoutingService/ListRoutes
A hand-rolled HMAC verifier deserves a check against an independent implementation. Recompute the signature with openssl, which knows nothing about this code:
HEADER_PAYLOAD="$(cut -d. -f1-2 <<<"$TOKEN")"
echo -n "$HEADER_PAYLOAD" | openssl dgst -sha256 -hmac "ch20-demo-shared-hmac-secret-DO-NOT-USE-IN-PRODUCTION" -binary \
| base64 | tr '+/' '-_' | tr -d '='
The output equals the token’s third segment (cut -d. -f3 <<<"$TOKEN").
Experiments:
- Drop the
Bearerprefix. Send-H "authorization: $TOKEN": a malformed header counts as absent, and the call fails withUnauthenticatedandmissing 'authorization: Bearer <jwt>' metadata. - Swap tokens. Mint
bad-sigorexpiredand callGetQuote: both fail withUnauthenticatedand aninvalid token:message, neverPermissionDenied, because authentication runs first.
What you learned
- Authentication and authorization are separate decisions with separate statuses, and merging them discards information that retry logic and log triage need.
- An interceptor is where both belong, because the check is the same for every method and nobody has to remember it for the next RPC.
- Authorization attaches to the method: one valid token gets
OKon one call andPERMISSION_DENIEDon the next, over the same connection. - Constant-time comparison is required in a signature check.
Observability reuses the same interceptor seam to put rejected and authorized calls on a trace.
For deeper coverage
- grpc.io: Authentication guide: gRPC’s token-based authentication APIs, the category the bearer-token check belongs to.
- Quarkus, OpenID Connect (
quarkus-oidc) guide: the production replacement for the hand-rolled HMAC check, verifying against a provider’s JWKS endpoint. - Cloud Native Design Patterns, “Security” chapter: the wider authn/authz pattern catalog.
- Liz Rice, Container Security, 2nd ed., O’Reilly, 2025: secret management and service identity.