Auth providers
Any OpenID Connect provider (JWT)
josejwtVerifycreateRemoteJWKSet
Verify access tokens from Auth0, Keycloak, Cognito, Entra ID and others with jose.
Most identity providers issue JWTs signed with keys published at a JWKS URL. jose verifies them without a provider SDK. This works for Auth0, Keycloak, Amazon Cognito, Microsoft Entra ID, Zitadel and others.
server/auth/verify-user.ts
import { createError, getCookie, getHeader } from 'h3'
import { createRemoteJWKSet, errors, jwtVerify } from 'jose'
import type { VerifyUser } from '@kurashina-design/nuxt-bff-guard'
const issuer = process.env.OIDC_ISSUER! // e.g. https://example.eu.auth0.com/
const audience = process.env.OIDC_AUDIENCE! // your API identifier
// Created once: keys are cached and refreshed when an unknown key id appears.
const jwks = createRemoteJWKSet(new URL('.well-known/jwks.json', issuer))
export default (async (event) => {
const bearer = /^Bearer[ \t]+(\S+)$/i.exec(getHeader(event, 'authorization') ?? '')?.[1]
const token = bearer ?? getCookie(event, 'access_token')
if (!token) throw createError({ statusCode: 401 })
try {
const { payload } = await jwtVerify(token, jwks, { issuer, audience })
event.context.user = { id: payload.sub!, scope: String(payload.scope ?? '') }
}
catch (error) {
// The JWKS endpoint did not answer in time → 503.
if (error instanceof errors.JWKSTimeout) throw error
// Expired, wrong audience, bad signature, unknown key → 401.
if (error instanceof errors.JOSEError) throw createError({ statusCode: 401 })
throw error // fetch failures and anything unexpected → 503
}
return { viaBearer: Boolean(bearer) }
}) satisfies VerifyUser
Check the JWKS path in your provider's discovery document (/.well-known/openid-configuration); Keycloak, for example, uses protocol/openid-connect/certs.
Scopes as an app-wide rule
const scopes = String(payload.scope ?? '').split(' ')
if (!scopes.includes('app:use')) throw createError({ statusCode: 403 })