From 2e527a853979d0ba20b4e67008a42694e9c4e0c7 Mon Sep 17 00:00:00 2001 From: Enstrayed <48845980+Enstrayed@users.noreply.github.com> Date: Fri, 23 Aug 2024 19:54:54 -0700 Subject: [PATCH] tweak logging & add token deletion --- liberals/logging.js | 11 ++++++++++- routes/auth.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 routes/auth.js diff --git a/liberals/logging.js b/liberals/logging.js index 1650318..ded7685 100644 --- a/liberals/logging.js +++ b/liberals/logging.js @@ -11,7 +11,16 @@ function logRequest(response,request,code,extra) { } else { actualExtra = "" } - console.log(`${request.get("cf-connecting-ip") ?? request.ip} (${request.get("Authorization")}) ${request.method} ${request.path} returned ${code}${actualExtra}`) + if (request.get("Authorization")) { + actualAuth = `(${request.get("Authorization")})` + } else { + actualAuth = "" + } + // Client IP if connecting over Cloudflare, else IP as received by Express + // | / Token used (if provided) + // | / | Request Method Request Path Status code returned to client provided by function call + // V V V V V V Extra information if provided by function call + console.log(`${request.get("cf-connecting-ip") ?? request.ip}${actualAuth}${request.method} ${request.path} returned ${code}${actualExtra}`) } module.exports = { logRequest } \ No newline at end of file diff --git a/routes/auth.js b/routes/auth.js new file mode 100644 index 0000000..7be2973 --- /dev/null +++ b/routes/auth.js @@ -0,0 +1,34 @@ +const { globalConfig, app } = require("../index.js") +const { logRequest } = require("../liberals/logging.js") + +app.delete("/api/token", (rreq,rres) => { + fetch(`${globalConfig.couchdbHost}/auth/sessions`).then(res => res.json()).then(fetchRes => { + if (fetchRes.sessions[rreq.get("Authorization")]) { + delete fetchRes.sessions[rreq.get("Authorization")] + fetch(`${globalConfig.couchdbHost}/auth/sessions`, { + method: "PUT", + headers: { + "If-Match": fetchRes._rev + }, + body: JSON.stringify({ + sessions: fetchRes.sessions + }) + + }).then(res => { + if (res.status == 201) { + rres.sendStatus(200) + } else { + logRequest(rres,rreq,500,`Token invalidation may have failed: ${res.status} ${res.statusText}`) + rres.sendStatus(500) + } + }).catch(fetchError => { + logRequest(rres,rreq,500,fetchError) + rres.sendStatus(500) + }) + } else { + rres.sendStatus(400) + } + }) +}) + +module.exports = {app} \ No newline at end of file