Commit fb9f17d3 authored by Matthieu - Greep's avatar Matthieu - Greep
Browse files

removing tokenCheck correctly

parent c8385bb1
Loading
Loading
Loading
Loading
+1 −39
Original line number Original line Diff line number Diff line
@@ -26,44 +26,6 @@ connection.connect((err)=>{
    }
    }
})
})


/**
 * Get the authorization token from the request header
 * @param {import('express').Request} req
 * @param {import('express').Response} res
 * @param {import('mysql').Connection} sql
 * @param {Function} cb the callback function to call only if the token is valid. There are no parameters.
 */ 
function checkToken(req, res, sql, cb){
    var auth = req.get('Authorization')
    if (!auth){
        renderError(res, 403, 'No authorization token provided');
        return false;
    } else {
        if (auth.includes('Basic ')){
            var token = auth.replace('Basic ', '')
            sql.query("SELECT * FROM Auth WHERE token = ?", token, async (err, result)=>{
                if (err){
                    console.error(err)
                    renderError(res, 500, "Internal Server Error")
                } else {
                    if (result.length < 1){
                        renderError(res, 401, "Authorization Token not found.")
                    } else {
                        if (result[0].validate == 0){
                            renderError(res, 403, "Authorization Token not validated.")
                        } else {
                            console.log('Connexion from '+ result[0].description);
                            cb();
                        }
                    }
                }
            });
        } else {
            renderError(res, 403, "Token not found in Authorization header, please set a Token in Authorization header like this: 'Authorization: Basic [token]'.")
        }
    }
}

/**
/**
 * Render a JSON response for an error
 * Render a JSON response for an error
 * @param {import('express').Response} res
 * @param {import('express').Response} res
@@ -121,7 +83,7 @@ app.get('/', (req, res) => {
})
})


fs.readdirSync(path.join(__dirname, 'route')).filter(file => file.endsWith('.js')).forEach(function(file) {
fs.readdirSync(path.join(__dirname, 'route')).filter(file => file.endsWith('.js')).forEach(function(file) {
    require(path.join(__dirname, 'route', file))(app, connection, checkToken, renderError)
    require(path.join(__dirname, 'route', file))(app, connection, renderError)
});
});


// catch 404 and forward to error handler
// catch 404 and forward to error handler
+1 −1
Original line number Original line Diff line number Diff line
@@ -5,7 +5,7 @@ const path = require('path'),
 * @param {import('express').Express} app
 * @param {import('express').Express} app
 * @param {import('mysql').Connection} sql
 * @param {import('mysql').Connection} sql
 */
 */
module.exports = function(app, sql, token, errorHandler) {
module.exports = function(app, sql, errorHandler) {
    app.get('/'+scriptName, function(req, res) {
    app.get('/'+scriptName, function(req, res) {
        const mapId = req.query.map || req.body.map,
        const mapId = req.query.map || req.body.map,
            player = req.query.player || req.body.player;
            player = req.query.player || req.body.player;
+1 −1
Original line number Original line Diff line number Diff line
@@ -5,7 +5,7 @@ const path = require('path'),
 * @param {import('express').Express} app
 * @param {import('express').Express} app
 * @param {import('mysql').Connection} sql
 * @param {import('mysql').Connection} sql
 */
 */
module.exports = function(app, sql, token, errorHandler) {
module.exports = function(app, sql, errorHandler) {
    app.get('/'+scriptName, function(req, res) {
    app.get('/'+scriptName, function(req, res) {
        const mapId = req.query.map || req.body.map;
        const mapId = req.query.map || req.body.map;
        if (!mapId) {
        if (!mapId) {
+1 −1
Original line number Original line Diff line number Diff line
@@ -5,7 +5,7 @@ const path = require('path'),
 * @param {import('express').Express} app
 * @param {import('express').Express} app
 * @param {import('mysql').Connection} sql
 * @param {import('mysql').Connection} sql
 */
 */
module.exports = function(app, sql, token, errorHandler) {
module.exports = function(app, sql, errorHandler) {
    app.get('/'+scriptName, function(req, res) {
    app.get('/'+scriptName, function(req, res) {
        sql.query('SELECT player, COUNT(*) AS votes FROM votes GROUP BY player ORDER BY votes DESC', (err, sqlRes)=>{
        sql.query('SELECT player, COUNT(*) AS votes FROM votes GROUP BY player ORDER BY votes DESC', (err, sqlRes)=>{
            if (err) {
            if (err) {
+1 −1
Original line number Original line Diff line number Diff line
@@ -5,7 +5,7 @@ const path = require('path'),
 * @param {import('express').Express} app
 * @param {import('express').Express} app
 * @param {import('mysql').Connection} sql
 * @param {import('mysql').Connection} sql
 */
 */
module.exports = function(app, sql, token, errorHandler) {
module.exports = function(app, sql, errorHandler) {
    app.get('/'+scriptName, function(req, res) {
    app.get('/'+scriptName, function(req, res) {
        sql.query('SELECT map, COUNT(*) AS votes FROM votes GROUP BY map ORDER BY votes DESC', (err, sqlRes)=>{
        sql.query('SELECT map, COUNT(*) AS votes FROM votes GROUP BY map ORDER BY votes DESC', (err, sqlRes)=>{
            if (err) {
            if (err) {
Loading