diff --git a/CHANGELOG.md b/CHANGELOG.md index 148b985d..0800b8a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,3 +8,5 @@ Version 2.0.0 Breaking Changes ---------------- - Node.js versions below 10 are no longer supported. +- lib/authenticate no longer requires config to be passed to it. Breaks + playerManager. diff --git a/lib/authenticate.js b/lib/authenticate.js index 9365a107..c1a02753 100644 --- a/lib/authenticate.js +++ b/lib/authenticate.js @@ -1,28 +1,48 @@ const jwt = require('jsonwebtoken'); -// const config = require("./../config"); +let authSecret = null; +function setAuthSecret(secret) { + authSecret = secret; +} -module.exports = function(config){ - return { - middleware: function (req, res, next) { - var token = req.headers['x-access-token']; - if(!token) return res.status(401).send({ auth: false, message: 'No token provided.', endpoint: req.route.path}); - - jwt.verify(token, config.masterAuthSecret, function(err, decoded) { - if(err) return res.status(500).send({ auth: false, message: 'Failed to authenticate token.', endpoint: req.route.path}); - next(); - }); - }, - check: async function(token){ - return new Promise((resolve, reject) => { - jwt.verify(token, config.masterAuthSecret, function(err, decoded) { - if(err){ - resolve({ok:false, msg:err}) - } else { - resolve({ok:true, msg:"Successfully authenticated"}); - } - }); - }); - } +function middleware(req, res, next) { + // This should not happen + if (!authSecret) { + return res.status(500).send({ auth: false, message: 'Secret not set', endpoint: req.route.path }); } + + var token = req.headers['x-access-token']; + if (!token) { + return res.status(401).send({ auth: false, message: 'No token provided.', endpoint: req.route.path }); + } + + jwt.verify(token, authSecret, function(err, decoded) { + if (err) { + return res.status(401).send({ auth: false, message: 'Failed to authenticate token.', endpoint: req.route.path }); + } + next(); + }); +} + +async function check(token) { + return new Promise((resolve, reject) => { + // This should not happen + if (!authSecret) { + resolve({ ok: false, msg: 'Secret not set' }); + } + + jwt.verify(token, authSecret, function(err, decoded) { + if (err) { + resolve({ok:false, msg:err}) + } else { + resolve({ok:true, msg:"Successfully authenticated"}); + } + }); + }); +} + +module.exports = { + setAuthSecret, + middleware, + check, }; diff --git a/master.js b/master.js index dc68b2a4..23a01acc 100644 --- a/master.js +++ b/master.js @@ -36,7 +36,7 @@ const generateSSLcert = require("lib/generateSSLcert"); const getFactorioLocale = require("lib/getFactorioLocale"); // homemade express middleware for token auth -const authenticate = require("lib/authenticate")(config); +const authenticate = require("lib/authenticate"); const express = require("express"); const compression = require('compression'); @@ -921,6 +921,8 @@ async function startServer() { // mod downloads app.use(express.static(masterModFolder)); + authenticate.setAuthSecret(config.masterAuthSecret); + // Make sure we're actually going to listen on a port let httpPort = args.masterPort || config.masterPort; let httpsPort = args.sslPort || config.sslPort; diff --git a/master.spec.js b/master.spec.js index 52620873..61cbf3cc 100644 --- a/master.spec.js +++ b/master.spec.js @@ -1,11 +1,15 @@ -const app = require('./master.js'); -const config = require('./config.json'); const assert = require('assert').strict; const request = require('supertest'); const validateHTML = require('html5-validator'); const parallel = require('mocha.parallel'); const jwt = require("jsonwebtoken"); +const app = require('./master.js'); +const config = require('./config.json'); +const authenticate = require('lib/authenticate'); + + +authenticate.setAuthSecret(config.masterAuthSecret); const token = jwt.sign( { id: "api" }, config.masterAuthSecret, { expiresIn: 600 }); diff --git a/sharedPlugins/statisticsExporter/masterPlugin.js b/sharedPlugins/statisticsExporter/masterPlugin.js index eb6fe31b..d65a8e20 100644 --- a/sharedPlugins/statisticsExporter/masterPlugin.js +++ b/sharedPlugins/statisticsExporter/masterPlugin.js @@ -3,7 +3,7 @@ const path = require("path"); class masterPlugin { constructor({config, socketio, express, Prometheus}){ - const authenticate = require("lib/authenticate")(config); + const authenticate = require("lib/authenticate"); this.config = config; this.io = socketio;