Implements OAuth 2.0/2.1 authorization flows in Fastify applications — configures authorization code with PKCE, client credentials, device flow, refresh token rotation, JWT validation, and token introspection/revocation endpoints. Use when setting up authentication, authorization, login flows, access tokens, API security, or securing Fastify routes with OAuth; also applies when troubleshooting token validation errors, mismatched redirect URIs, CSRF issues, scope problems, or RFC 6749/6750/7636/8252/8628 compliance questions.
94
95%
Does it follow best practices?
Impact
93%
1.40xAverage score across 5 eval scenarios
Passed
No known issues
A fintech company runs several Fastify microservices that communicate via JWT bearer tokens issued by a central authorization server. Recently a security audit flagged that one of the services was insufficiently validating incoming JWTs — tokens from a decommissioned service and even expired tokens were still being accepted. The platform team needs a reusable Fastify hook that rigorously validates every incoming JWT before any handler runs.
The authorization server uses asymmetric key pairs and publishes its keys via a JWKS endpoint. Tokens include standard RFC 7519 claims: exp, iss, aud, and sub.
Produce a TypeScript implementation with the following files:
hooks/verifyToken.ts — a Fastify hook function that validates JWT bearer tokensroutes/api.ts — a sample protected route group that uses the hookThe hook should read expected values from environment variables:
EXPECTED_ISSUER — the expected iss claim valueEXPECTED_AUDIENCE — the expected aud claim valueThe route file should include at least one protected route (e.g. GET /me) that returns a field from the validated token payload.
No need to start a server or connect to a live authorization server. Produce only the source files.