rewrite to a module
This commit is contained in:
+34
-33
@@ -1,7 +1,7 @@
|
||||
const signature = require('cookie-signature')
|
||||
const fs = require('fs');
|
||||
const cookiesecret = fs.readFileSync("cookiesecret.txt").toString()
|
||||
const getIP = require("./getip.js")
|
||||
import * as signature from "cookie-signature";
|
||||
import fs from "fs";
|
||||
import getIP from "./getip.js";
|
||||
const cookiesecret = fs.readFileSync("cookiesecret.txt").toString();
|
||||
/**
|
||||
* usignes a string
|
||||
* @param {string} text text to unsign
|
||||
@@ -9,40 +9,41 @@ const getIP = require("./getip.js")
|
||||
* @param {response} res response object
|
||||
* @return {string/boolean} unsigned text, or if unsigning was unsuccessful, false
|
||||
*/
|
||||
function unsign(text,req,res) {
|
||||
let ip = getIP(req)
|
||||
let unsigned = signature.unsign(text,cookiesecret+ip)
|
||||
if(!unsigned) {
|
||||
return false
|
||||
}
|
||||
return unsigned
|
||||
function unsign(text, req, res) {
|
||||
let ip = getIP(req);
|
||||
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
|
||||
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);
|
||||
}
|
||||
export { unsign };
|
||||
export { getunsigned };
|
||||
export default {
|
||||
unsign: unsign,
|
||||
getunsigned: getunsigned
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user