UNPKG

@mark01/express-utils

Version:

npm package that contains utilities for express.js

20 lines (19 loc) 774 B
import { verifyEdDSAJWT } from '../core/rfc7519'; import { AppError } from '../error'; import extractHeader from '../util/extract-header'; export const hasJWT = (header, publicKey, audience, issuer, onTokenValidated) => async (req, _, next) => { const token = extractHeader(req, header); if (!token) { next(new AppError('You do not possess an OTP session. Please verify your OTP by MFA.', 401)); return; } const decoded = await verifyEdDSAJWT(token, publicKey, audience, issuer); if (!decoded.payload.jti) { next(new AppError('The JTI of the token is invalid. Please verify your session again.', 401)); return; } if (onTokenValidated) await onTokenValidated(decoded); next(); }; export default hasJWT;