REST Service for POPCORN - ILIAS
alex
2025-05-15 13c9ae4171a0696df58b153b6ee99c088c27cefb
adding auth token
2 files modified
20 ■■■■■ changed files
app.js 19 ●●●●● patch | view | raw | blame | history
settings.js 1 ●●●● patch | view | raw | blame | history
app.js
@@ -2,8 +2,21 @@
   logger: true
})
const db = require("./lib/db")
const settings = require("./settings")
/////////////////////////////////////////////////////////////////////////
// AUTH
fastify.addHook("onRequest", async (req, res) => {
   const token = req.query.token
   if (token !== settings.authtoken) {
      console.error("# AUTH ERROR #", token)
      await promiseDelay(500) // delay response to avoid denial of service attacks
      res.code(403)
      return res.send({status: "error", error: "access denied"})
   }
   else {}
})
fastify
   .get('/api/users', async function (req, res) {
@@ -46,3 +59,9 @@
   }
   // Server is now listening on ${address}
})
/////////////////////////////////////////////////////////////////////////
async function promiseDelay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms))
}
settings.js
@@ -1,4 +1,5 @@
module.exports = {
   authtoken: "jiuGfr432898D90290kjfsldkfn3hh8F",
   db: {
      host: "localhost",
      port: 33009,