From fe13b12f4387035b952da68bf2115adb096a8531 Mon Sep 17 00:00:00 2001 From: Jocelyn Le Sage Date: Mon, 8 Feb 2021 16:46:35 -0500 Subject: [PATCH] Removed the need to restart Nginx Proxy Manager after generating JWT key pair. --- backend/models/token.js | 17 ++++++++++++++--- backend/setup.js | 5 ++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/backend/models/token.js b/backend/models/token.js index 373f8e5..4e1b182 100644 --- a/backend/models/token.js +++ b/backend/models/token.js @@ -4,15 +4,23 @@ */ const _ = require('lodash'); -const config = require('config'); const jwt = require('jsonwebtoken'); const crypto = require('crypto'); const error = require('../lib/error'); const ALGO = 'RS256'; +let public_key = null; +let private_key = null; + +function checkJWTKeyPair() { + if (!public_key || !private_key) { + let config = require('config'); + public_key = config.get('jwt.pub'); + private_key = config.get('jwt.key'); + } +} + module.exports = function () { - const public_key = config.get('jwt.pub'); - const private_key = config.get('jwt.key'); let token_data = {}; @@ -32,6 +40,8 @@ module.exports = function () { .toString('base64') .substr(-8); + checkJWTKeyPair(); + return new Promise((resolve, reject) => { jwt.sign(payload, private_key, options, (err, token) => { if (err) { @@ -53,6 +63,7 @@ module.exports = function () { */ load: function (token) { return new Promise((resolve, reject) => { + checkJWTKeyPair(); try { if (!token || token === null || token === 'null') { reject(new error.AuthError('Empty token')); diff --git a/backend/setup.js b/backend/setup.js index d58a160..9a59f9a 100644 --- a/backend/setup.js +++ b/backend/setup.js @@ -51,9 +51,8 @@ const setupJwt = () => { reject(err); } else { logger.info('Wrote JWT key pair to config file: ' + filename); - - logger.warn('Restarting interface to apply new configuration'); - process.exit(0); + delete require.cache[require.resolve('config')]; + resolve(); } }); } else {