Launch offer for the first 500 accounts: $10K MTR free for 24 months + $100 credit →
    .io

    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

    Node
    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;
      }
    });

    Options

    OptionDefaultNotes
    projectId—Required. Checked against the token's aud
    environmentproductionSee above
    jwksUrlhttps://api.billdog.io/v1/auth-jwksOverride for a proxy or a self-hosted edge
    jwksCacheMs10 minutesHow long the key set is held
    clockToleranceSeconds60Skew between your clock and ours

    Entitlements, and the trap in reading them

    Code
    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 none

    Offline 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.

    Code
    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

    Next

    Was this page helpful?

    Or rate this page:

    We use cookies and process your data to provide our services. By continuing, you agree to our Terms and Privacy Policy.