moved some functions out of server
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
const crypto = require('crypto');
|
||||
|
||||
/**
|
||||
* hashes with the secure hashing algorithm 256
|
||||
* @param {string} str string to hash
|
||||
* @param {any} salt salt to apply to string
|
||||
* @param {number} num amount of times to hash, defaults to 1
|
||||
* @returns {string} base64 digested hash
|
||||
*/
|
||||
function SHA256(str,salt,num) {
|
||||
if(!num && num!==0)num=1;
|
||||
if(!str)return;
|
||||
let ret = str;
|
||||
for (let i = 0; i < num; i++) {
|
||||
ret = crypto
|
||||
.createHash("sha256")
|
||||
.update(ret+salt)
|
||||
.digest("base64");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
SHA256: SHA256
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
const signature = require('cookie-signature')
|
||||
const fs = require('fs');
|
||||
const cookiesecret = fs.readFileSync("cookiesecret.txt").toString()
|
||||
/**
|
||||
* usignes a string
|
||||
* @param {string} text text to unsign
|
||||
* @param {request} req request object, used for getting the ip for unsigning
|
||||
* @param {response} res response object
|
||||
* @return {string/boolean} unsigned text, or if unsigning was unsuccessful, false
|
||||
*/
|
||||
function unsign(text,req,res) {
|
||||
let ip = req.socket.remoteAddress
|
||||
let unsigned = signature.unsign(text,cookiesecret+ip)
|
||||
if(!unsigned) {
|
||||
return false
|
||||
}
|
||||
return unsigned
|
||||
}
|
||||
|
||||
/**
|
||||
* unsignes the auth cookie of a request, also sends json response if auth cookie was invalid
|
||||
* @param {request} req request object
|
||||
* @param {response} res response object
|
||||
* @return {string/boolean} unsigned cookie, or if unsigning was unsuccessful, false
|
||||
*/
|
||||
function getunsigned(req,res) {
|
||||
let cookie = req.cookies.AUTH_COOKIE
|
||||
if(!cookie){
|
||||
res.status(400)
|
||||
res.json({"error":"you are not logged in! (no cookie)"})
|
||||
return
|
||||
}
|
||||
let unsigned = unsign(cookie,req,res)
|
||||
if(!unsigned){
|
||||
try {
|
||||
res.status(400)
|
||||
res.json({"error":"Bad auth cookie set"})
|
||||
} catch (ignored) {} //sometimes it errors, gotta debug soon
|
||||
return false
|
||||
}
|
||||
return decodeURIComponent(unsigned)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
unsign: unsign,
|
||||
getunsigned: getunsigned
|
||||
}
|
||||
Reference in New Issue
Block a user