diff --git a/liberals/auth.js b/liberals/auth.js index eb60dc8..6348230 100644 --- a/liberals/auth.js +++ b/liberals/auth.js @@ -28,7 +28,15 @@ async function checkToken(token,scope) { */ async function checkTokenNew(token,scope) { - + return await db`select s.token, s.scopes, s.expires, u.username from sessions s join users u on s.owner = u.id where s.token = ${token}`.then(response => { + if (response.length === 0) { + return { result: false, owner: response[0]?.username} + } else if (response[0]?.scopes.split(",").includes(scope)) { + return { result: true, owner: response[0]?.username} + } else { + return { result: false, owner: response[0]?.username} + } + }) } -export {checkToken} \ No newline at end of file +export {checkToken, checkTokenNew} \ No newline at end of file diff --git a/liberals/logging.js b/liberals/logging.js index 3586855..ecdb8e2 100644 --- a/liberals/logging.js +++ b/liberals/logging.js @@ -4,9 +4,10 @@ * @param {object} request Parent request object * @param {number} code Status code to log, should be same as sent to client * @param {string} extra Optional extra details to add to log, ideal for caught errors + * @param {object} authresponse Optionally include result of auth response to include owner information for a token */ -function logRequest(response,request,code,extra) { - console.log(`${request.get("cf-connecting-ip") ?? request.ip} ${request.get("Authorization") ?? ""} ${request.method} ${request.path} returned ${code} ${extra ?? ""}`) +function logRequest(response,request,code,extra,authresponse) { + console.log(`${request.get("cf-connecting-ip") ?? request.ip} ${authresponse.owner ?? ""}/${request.get("Authorization") ?? ""} ${request.method} ${request.path} returned ${code} ${extra ?? ""}`) } export { logRequest } \ No newline at end of file diff --git a/routes/email.js b/routes/email.js index 4119776..3d916ca 100644 --- a/routes/email.js +++ b/routes/email.js @@ -1,5 +1,5 @@ import { app, globalConfig } from "../index.js" // Get globals from index -import { checkToken } from "../liberals/auth.js" +import { checkTokenNew } from "../liberals/auth.js" import { logRequest } from "../liberals/logging.js" import * as nodemailer from 'nodemailer' @@ -14,10 +14,10 @@ const transporter = nodemailer.createTransport({ }) app.post("/api/sendemail", (rreq,rres) => { - checkToken(rreq.get("Authorization"),"email").then(authRes => { - if (authRes === false) { + checkTokenNew(rreq.get("Authorization"),"email").then(authRes => { + if (authRes.result === false) { rres.sendStatus(401) - } else if (authRes === true) { + } else if (authRes.result === true) { if (rreq.body == undefined || rreq.body.recipient == undefined) { // 2024-05-11: Turbo bodge check to make sure request JSON is valid, probably wont work but whatever rres.sendStatus(400) } else { @@ -29,14 +29,14 @@ app.post("/api/sendemail", (rreq,rres) => { text: rreq.body.message ?? "Message Not Set" }).then(transportResponse => { if (transportResponse.response.slice(0,1) === "2") { - logRequest(rres,rreq,200,transportResponse.response) + logRequest(rres,rreq,200,transportResponse.response,authRes) rres.status(200).send(transportResponse.response) } else { - logRequest(rres,rreq,400,transportResponse.response) + logRequest(rres,rreq,400,transportResponse.response,authRes) rres.status(400).send(transportResponse.response) } }).catch(transportError => { - logRequest(rres,rreq,500,transportError) + logRequest(rres,rreq,500,transportError,authRes) rres.sendStatus(500) }) diff --git a/todo.md b/todo.md index 71f8b01..27281b2 100644 --- a/todo.md +++ b/todo.md @@ -2,4 +2,6 @@ - [ ] GET /api/login - OIDC login redirect to ECLS - [ ] GET /api/callback - Creates new token that is intended to be local to browser; e.g. can be used in turn to make longer lasting more specific tokens - [ ] POST /api/token - Allows owner to create a new token with customized scopes, comments & expiration date -- [ ] DELETE /api/token - Invalidate a token \ No newline at end of file +- [ ] DELETE /api/token - Invalidate a token +- [ ] liberals/libnowplaying - Implement queryCider() +- [ ] routes/nowplaying - Reimplement query order to Cider and then Jellyfin