Verifying Tokens on Your Server
No network call on your request path
Verification is a local signature check against a key set cached for ten minutes. An outage on our side cannot take your authorization down with it, and a hot route pays no network cost. This is the half of BillDog Auth that runs on your infrastructure.
The middleware
import { verifyToken, hasEntitlement, BillDogAuthError } from 'billdogeng-node';
app.use(async (req, res, next) => {
const header = req.headers.authorization ?? '';
if (!header.startsWith('Bearer ')) return res.status(401).end();
try {
const claims = await verifyToken(header.slice(7), {
projectId: process.env.BILLDOG_PROJECT_ID!,
environment: 'production',
});
req.userId = claims.sub; // the person who signed in
req.customerId = claims.cus; // the record they own
req.orgId = claims.o?.id; // active organization, if any
if (!hasEntitlement(claims, 'pro')) return res.status(403).end();
next();
} catch (e) {
// Expired, wrong signature, wrong audience, wrong environment — all 401.
if (e instanceof BillDogAuthError) return res.status(401).end();
throw e;
}
});environment is a security control, not a convenience
production — the strict answer — and you should set it explicitly anyway. Sandbox and production identities are different people, and a backend serving real users must refuse a sandbox token even though it is genuinely signed by us. Signing keys are environment-scoped and each environment has its own JWKS, so this is enforced at the key level rather than trusted from a claim.Options
| Option | Default | Notes |
|---|---|---|
projectId | — | Required. Checked against the token's aud |
environment | production | See above |
jwksUrl | https://api.billdog.io/v1/auth-jwks | Override for a proxy or a self-hosted edge |
jwksCacheMs | 10 minutes | How long the key set is held |
clockToleranceSeconds | 60 | Skew between your clock and ours |
Entitlements, and the trap in reading them
Never treat ent as complete without checking
ent_truncated is true and the list is partial — reading it as the whole set turns a paying customer into a free one. Truncation is never silent, but hasEntitlement() only reads the list: when it returns false, check entitlementsComplete() and, if the claim was cut, fall back to introspect(). If you parse the token yourself, check the flag the same way.hasEntitlement(claims, 'pro'); // reads the claim list only
entitlementsComplete(claims); // false when the claim was cut
factorAge(claims, 'first'); // minutes since the first factor, or null
factorAge(claims, 'second'); // minutes since the second factor; null if noneOffline verification's one limit
A verified token proves it was valid when issued, not that the session is live right now. Because the token lasts 90 seconds, that is exactly how long a revoked session can still pass verification. Choosing offline verification is choosing a bounded staleness — that is the trade, and for almost every route it is the right one.
Where you need certainty inside that window — a "sign out everywhere" that must bite immediately, or a genuinely destructive action — call introspect(). It costs a round trip and answers whether the session is live now. It also re-resolves entitlements through the organization context, so it can never disagree downward with the token it just validated.
const { active, claims } = await introspect(token, {
apiKey: process.env.BILLDOG_SECRET_KEY!,
});Key rotation
Rotation never invalidates a live token: a retired key keeps verifying until its own retirement time passes, so tokens minted just before a rotation still work. Your cache picks up new keys within ten minutes; clearJwksCache() forces it, which is useful in tests and after an emergency rotation.
On other server languages
Node, Python and Go verify offline
billdogeng-node (verifyToken), the Python package billdogeng (verify_token) and the Go module github.com/billdogai/billdogeng-go (auth.VerifyToken). From Ruby, PHP, Java, Rust, .NET or Elixir, either verify in a small Node, Python or Go service, or call the introspection endpoint over HTTP — a round trip per check, in exchange for knowing the session is live now. A generic JWT library can also verify our Ed25519 signatures against the JWKS, but you take on the audience, environment and truncation checks yourself.Next
- Sessions and tokens — the full claim set.
- Auth webhooks
- Security
Was this page helpful?
Or rate this page: