Major changes
This commit is contained in:
@@ -1,30 +1,28 @@
|
||||
const { globalConfig } = require("../index.js")
|
||||
|
||||
/**
|
||||
* Checks if a token exists in the sessions file (authentication) and if it has the correct permissions (authorization)
|
||||
* @param {string} token Token as received by client
|
||||
* @param {string} scope Scope the token will need to have in order to succeed
|
||||
* @returns True for successful authentication and authorization, false if either fail
|
||||
*/
|
||||
async function checkToken(token,scope) {
|
||||
return await fetch(`${globalConfig.couchdbHost}/auth/sessions`).then(fetchRes => {
|
||||
|
||||
// CouchDB should only ever return 200/304 for success so this should work
|
||||
// https://docs.couchdb.org/en/stable/api/document/common.html#get--db-docid
|
||||
if (fetchRes.status !== 200 || fetchRes.status !== 304) {
|
||||
console.log(`ERROR: auth.js: Database request returned ${fetchRes.status}`)
|
||||
return false
|
||||
} else {
|
||||
return fetchRes.json().then(dbRes => {
|
||||
|
||||
return fetchRes.json().then(dbRes => {
|
||||
if (dbRes.sessions[token] == undefined) { // If the token is not on the sessions list then reject
|
||||
return false
|
||||
} else if (dbRes.sessions[token].scopes.includes(scope)) { // If the token is on the seesions list and includes the scope then accept
|
||||
return true
|
||||
} else { // Otherwise reject
|
||||
return false
|
||||
}
|
||||
|
||||
if (dbRes.sessions[token] == undefined) { // If the token is not on the sessions list then reject
|
||||
return false
|
||||
} else if (dbRes.sessions[token].scopes.includes(scope)) { // If the token is on the seesions list and includes the scope then accept
|
||||
return true
|
||||
} else { // Otherwise reject
|
||||
return false
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
}).catch(error => {
|
||||
console.log("ERROR: auth.js: " + error)
|
||||
console.log(`ERROR: auth.js: Fetch failed: ${error}`)
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
const { globalConfig } = require("../index.js")
|
||||
|
||||
async function checkAuthorization(documentToUse,keyToCheck) {
|
||||
return await fetch(`${globalConfig.couchdbHost}/apiauthkeys/${documentToUse}`).then(fetchRes => {
|
||||
|
||||
if (fetchRes.status === 404) { // If document doesnt exist fail gracefully
|
||||
|
||||
console.log("ERROR: Failed to check authorization: Requested document returned 404")
|
||||
return false
|
||||
|
||||
} else if (fetchRes.status === 401) { // If couchdb is reporting unauthorized fail gracefully
|
||||
|
||||
console.log("ERROR: Failed to check authorization: Database authorization is incorrect")
|
||||
return false
|
||||
|
||||
} else {
|
||||
return fetchRes.json().then(dbRes => { // Get response json and check it
|
||||
|
||||
if (dbRes["content"][keyToCheck.split("_")[0]] === keyToCheck.split("_")[1]) {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
}).catch(error => {
|
||||
console.log("ERROR: Failed to check authorization: " + error)
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {checkAuthorization}
|
||||
@@ -1,12 +1,16 @@
|
||||
const { globalConfig } = require("../index.js")
|
||||
|
||||
/**
|
||||
* Queries LastFM for user set in config file and returns formatted result
|
||||
* @returns {object} Object containing response in JSON and HTML (as string), WILL RETURN EMPTY OBJECT ON FAILURE!
|
||||
*/
|
||||
async function queryLastfm() {
|
||||
return await fetch(`https://ws.audioscrobbler.com/2.0/?format=json&method=user.getrecenttracks&limit=1&api_key=${globalConfig.nowplaying.lastfm.apiKey}&user=${globalConfig.nowplaying.lastfm.target}`).then(response => response.json()).then(response => {
|
||||
if (response["recenttracks"] == undefined) {
|
||||
return 1
|
||||
return {}
|
||||
} else {
|
||||
if (response.recenttracks.track[0]["@attr"] == undefined) {
|
||||
return 1
|
||||
return {}
|
||||
} else {
|
||||
return {
|
||||
"json": {
|
||||
@@ -20,6 +24,9 @@ async function queryLastfm() {
|
||||
}
|
||||
}
|
||||
}
|
||||
}).catch(fetchError => {
|
||||
console.log("libnowplaying.js: Fetch failed! "+fetchError)
|
||||
return {}
|
||||
})
|
||||
}
|
||||
|
||||
17
liberals/logging.js
Normal file
17
liberals/logging.js
Normal file
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* Logs various details about the request (IP, Token, Method, Path, etc) to the console for later review
|
||||
* @param {object} response Parent response object
|
||||
* @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
|
||||
*/
|
||||
function logRequest(response,request,code,extra) {
|
||||
if (extra) {
|
||||
actualExtra = "; Extra: "+extra
|
||||
} else {
|
||||
actualExtra = ""
|
||||
}
|
||||
console.log(`${request.get("cf-connecting-ip") ?? request.ip} (${request.get("Authorization")}) ${request.method} ${request.path} returned ${code}${actualExtra}`)
|
||||
}
|
||||
|
||||
module.exports = { logRequest }
|
||||
Reference in New Issue
Block a user