refactor admin api auth

This commit is contained in:
AlecM33
2022-12-10 23:27:23 -05:00
parent 9ef9141513
commit fe84db12fb
2 changed files with 25 additions and 27 deletions

View File

@@ -7,20 +7,8 @@ const gameManager = new (require('../modules/GameManager.js'))().getInstance();
const globals = require('../config/globals.js');
const cors = require('cors');
const KEY = process.env.NODE_ENV.trim() === 'development'
? globals.MOCK_AUTH
: process.env.ADMIN_KEY;
router.use(cors(globals.CORS));
router.use((req, res, next) => {
if (isAuthorized(req)) {
next();
} else {
res.status(401).send('You are not authorized to make this request.');
}
});
router.post('/sockets/broadcast', (req, res, next) => {
globals.CONTENT_TYPE_VALIDATOR(req, res, next);
});
@@ -40,16 +28,4 @@ router.get('/games/state', function (req, res) {
res.status(200).send(gamesArray);
});
/* validates Bearer Auth */
function isAuthorized (req) {
const header = req.headers.authorization;
if (header) {
const token = header.split(/\s+/).pop() || '';
const decodedToken = Buffer.from(token, 'base64').toString();
return decodedToken.trim() === KEY.trim();
}
return false;
}
module.exports = router;