UNPKG

@apolitical/server

Version:

Node.js module to encapsulate Apolitical's express server setup

22 lines (16 loc) 659 B
'use strict'; const TIME_CONVERTION = 1000; // 1 second in milliseconds const EXPIRATION_LAPSE = 30 * 24 * 3600; // 30 days in seconds module.exports = ({ jsrsasign, config }) => { const { sign } = jsrsasign.jws.JWS; const { ALGORITHM, HEADER, DEFAULT_PAYLOAD } = config.JWT.ENCODE; function buildPayload(customPayload) { const exp = Math.floor(Date.now() / TIME_CONVERTION) + EXPIRATION_LAPSE; return Object.assign({}, DEFAULT_PAYLOAD, { exp }, customPayload); } function encode(secret, customPayload = {}) { const payload = buildPayload(customPayload); return sign(ALGORITHM, HEADER, payload, secret); } return encode; };