Protect your API
Your server accepts a request only if it carries a valid Novren ID access token for your app, not revoked. @novren/id-verifier does all of it.
You need: your app id and a service token in the server's environment (NOVREN_ID_SERVICE_TOKEN).
Set up the verifier once
import { createTokenVerifier, TokenRejected } from '@novren/id-verifier'
const verifier = createTokenVerifier({
idServerUrl: 'https://id.novren.net',
product: 'app-0123456789abcdef0123', // your app id, from the console
serviceToken: process.env.NOVREN_ID_SERVICE_TOKEN!, // never in the browser
})
await verifier.start() // first read of the revocations, then every 5 seconds| Option | Required | Default | Meaning |
|---|---|---|---|
idServerUrl | yes | https://id.novren.net. | |
product | yes | Your app id (app-…). Tokens with another aud are refused. | |
serviceToken | yes | Your service token: it reads the revocation feed. | |
pollMs | no | 5000 | How often the revocation feed is read. |
maxStalenessMs | no | 60000 | After this long without a successful read, every token is refused (unavailable). |
start() makes the first read at once, then repeats every pollMs. The timer doesn't keep Node alive (unref). Create one verifier per process, not one per request.
Verify every request
async function handle(request: Request): Promise<Response> {
const header = request.headers.get('authorization') ?? ''
const token = header.startsWith('Bearer ') ? header.slice(7) : undefined
try {
const claims = await verifier.verify(token)
const userId = claims.sub // the Novren ID account id
return Response.json(await loadNotes(userId))
} catch (error) {
if (error instanceof TokenRejected) {
// invalid / revoked: the client refreshes or signs in again.
// unavailable: we could not read the revocations for 60 s; fail closed.
const status = error.reason === 'unavailable' ? 503 : 401
return new Response(null, { status, headers: { 'www-authenticate': 'Bearer' } })
}
throw error
}
}verify(token) resolves to the token's claims or throws TokenRejected with a reason:
reason | Meaning | Answer |
|---|---|---|
invalid | missing token, bad signature, other app (aud), expired, wrong type | 401: the client refreshes or signs in |
revoked | the chain was closed, or the account revoked everything after the token was issued | 401: the client must sign in again |
unavailable | the revocation feed hasn't been read for 60 s | 503: temporary, retry later |
Use sub as the user id
claims.sub is the account id. Never trust a user id that the client sends in the body or the URL: take it from the verified token.
Express
The same with Express:
app.use(async (req, res, next) => {
const header = req.headers.authorization ?? ''
try {
const claims = await verifier.verify(header.startsWith('Bearer ') ? header.slice(7) : undefined)
res.locals.userId = claims.sub
next()
} catch (error) {
if (error instanceof TokenRejected) return res.sendStatus(error.reason === 'unavailable' ? 503 : 401)
next(error)
}
})After something sensitive
The feed is read every 5 seconds. If an action is sensitive (deleting data, changing an email) and you want the freshest revocations, call await verifier.sync() right before verify.
Edge cases
- Clock skew. Expiry is checked against your server's clock: keep it synchronised (NTP).
- Server starting while Novren ID is unreachable.
start()doesn't throw: the firstverifycalls answerunavailableuntil the feed is read. - Several instances. Each process has its own verifier and reads the feed on its own; there is nothing to share.
- Key rotation. Public keys are fetched from the JWKS and cached; a new
kidis fetched automatically.