mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 16:13:12 +09:00
Remove config dependency in lib/authenticate
Restructure lib/authenticate to set the authentication secret at a later point in time via setAuthSecret. This breaks playerManager.
This commit is contained in:
1 parent
8c1c8d07a9
commit
af2471b427
5 files changed
+55
-27
No files matched your search
@@ -8,3 +8,5 @@ Version 2.0.0
|
|||||||
Breaking Changes
|
Breaking Changes
|
||||||
----------------
|
----------------
|
||||||
- Node.js versions below 10 are no longer supported.
|
- Node.js versions below 10 are no longer supported.
|
||||||
|
- lib/authenticate no longer requires config to be passed to it. Breaks
|
||||||
|
playerManager.
|
||||||
+43
-23
@@ -1,28 +1,48 @@
|
|||||||
const jwt = require('jsonwebtoken');
|
const jwt = require('jsonwebtoken');
|
||||||
|
|
||||||
// const config = require("./../config");
|
let authSecret = null;
|
||||||
|
function setAuthSecret(secret) {
|
||||||
|
authSecret = secret;
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = function(config){
|
function middleware(req, res, next) {
|
||||||
return {
|
// This should not happen
|
||||||
middleware: function (req, res, next) {
|
if (!authSecret) {
|
||||||
var token = req.headers['x-access-token'];
|
return res.status(500).send({ auth: false, message: 'Secret not set', endpoint: req.route.path });
|
||||||
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"});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
};
|
};
|
||||||
@@ -36,7 +36,7 @@ const generateSSLcert = require("lib/generateSSLcert");
|
|||||||
const getFactorioLocale = require("lib/getFactorioLocale");
|
const getFactorioLocale = require("lib/getFactorioLocale");
|
||||||
|
|
||||||
// homemade express middleware for token auth
|
// homemade express middleware for token auth
|
||||||
const authenticate = require("lib/authenticate")(config);
|
const authenticate = require("lib/authenticate");
|
||||||
|
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
const compression = require('compression');
|
const compression = require('compression');
|
||||||
@@ -921,6 +921,8 @@ async function startServer() {
|
|||||||
// mod downloads
|
// mod downloads
|
||||||
app.use(express.static(masterModFolder));
|
app.use(express.static(masterModFolder));
|
||||||
|
|
||||||
|
authenticate.setAuthSecret(config.masterAuthSecret);
|
||||||
|
|
||||||
// Make sure we're actually going to listen on a port
|
// Make sure we're actually going to listen on a port
|
||||||
let httpPort = args.masterPort || config.masterPort;
|
let httpPort = args.masterPort || config.masterPort;
|
||||||
let httpsPort = args.sslPort || config.sslPort;
|
let httpsPort = args.sslPort || config.sslPort;
|
||||||
|
|||||||
+6
-2
@@ -1,11 +1,15 @@
|
|||||||
const app = require('./master.js');
|
|
||||||
const config = require('./config.json');
|
|
||||||
const assert = require('assert').strict;
|
const assert = require('assert').strict;
|
||||||
const request = require('supertest');
|
const request = require('supertest');
|
||||||
const validateHTML = require('html5-validator');
|
const validateHTML = require('html5-validator');
|
||||||
const parallel = require('mocha.parallel');
|
const parallel = require('mocha.parallel');
|
||||||
const jwt = require("jsonwebtoken");
|
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 =
|
const token =
|
||||||
jwt.sign( { id: "api" }, config.masterAuthSecret, { expiresIn: 600 });
|
jwt.sign( { id: "api" }, config.masterAuthSecret, { expiresIn: 600 });
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ const path = require("path");
|
|||||||
|
|
||||||
class masterPlugin {
|
class masterPlugin {
|
||||||
constructor({config, socketio, express, Prometheus}){
|
constructor({config, socketio, express, Prometheus}){
|
||||||
const authenticate = require("lib/authenticate")(config);
|
const authenticate = require("lib/authenticate");
|
||||||
|
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.io = socketio;
|
this.io = socketio;
|
||||||
|
|||||||
Reference in new issue
Block a user