Browse Source

feat: added option to change name of SID cookie

Kristian Vos 5 years ago
parent
commit
cb8f743b84

+ 2 - 1
backend/config/template.json

@@ -55,6 +55,7 @@
 	},
   	"cookie": {
 	  	"domain": "localhost",
-	  	"secure": false
+		"secure": false,
+		"SIDname": "SID"  
 	}
 }

+ 3 - 3
backend/logic/app.js

@@ -24,7 +24,7 @@ module.exports = class extends coreClass {
 			this.utils = this.moduleManager.modules["utils"];
 
 			let app = this.app = express();
-
+			const SIDname = config.get("cookie.SIDname");
 			this.server = app.listen(config.get('serverPort'));
 
 			app.use(cookieParser());
@@ -64,7 +64,7 @@ module.exports = class extends coreClass {
 					`client_id=${config.get('apis.github.client')}`,
 					`redirect_uri=${config.get('serverDomain')}/auth/github/authorize/callback`,
 					`scope=user:email`,
-					`state=${req.cookies.SID}`
+					`state=${req.cookies[SIDname]}`
 				].join('&');
 				res.redirect(`https://github.com/login/oauth/authorize?${params}`);
 			});
@@ -188,7 +188,7 @@ module.exports = class extends coreClass {
 						if (err) return redirectOnErr(res, err.message);
 						let date = new Date();
 						date.setTime(new Date().getTime() + (2 * 365 * 24 * 60 * 60 * 1000));
-						res.cookie('SID', sessionId, {
+						res.cookie(SIDname, sessionId, {
 							expires: date,
 							secure: config.get("cookie.secure"),
 							path: "/",

+ 5 - 2
backend/logic/io.js

@@ -6,12 +6,13 @@ const coreClass = require("../core");
 
 const socketio = require("socket.io");
 const async = require("async");
+const config = require("config");
 
 module.exports = class extends coreClass {
 	constructor(name, moduleManager) {
 		super(name, moduleManager);
 
-		this.dependsOn = ["app", "db", "cache"];
+		this.dependsOn = ["app", "db", "cache", "utils"];
 	}
 
 	initialize() {
@@ -27,6 +28,8 @@ module.exports = class extends coreClass {
 			
 			const actions = require('../logic/actions');
 
+			const SIDname = config.get("cookie.SIDname");
+
 			// TODO: Check every 30s/60s, for all sockets, if they are still allowed to be in the rooms they are in, and on socket at all (permission changing/banning)
 			this.io = socketio(app.server);
 
@@ -42,7 +45,7 @@ module.exports = class extends coreClass {
 						utils.parseCookies(
 							socket.request.headers.cookie
 						).then(res => {
-							SID = res.SID;
+							SID = res[SIDname];
 							next(null);
 						});
 					},

+ 5 - 4
frontend/api/auth.js

@@ -62,7 +62,7 @@ export default {
 							let domain = "";
 							if (cookie.domain !== "localhost")
 								domain = ` domain=${cookie.domain};`;
-							document.cookie = `SID=${
+							document.cookie = `${cookie.SIDname}=${
 								res.SID
 							}; expires=${date.toGMTString()}; ${domain}${secure}path=/`;
 							return resolve({ status: "success" });
@@ -79,9 +79,10 @@ export default {
 			io.getSocket(socket => {
 				socket.emit("users.logout", result => {
 					if (result.status === "success") {
-						document.cookie =
-							"SID=;expires=Thu, 01 Jan 1970 00:00:01 GMT;";
-						return window.location.reload();
+						return lofig.get("cookie", cookie => {
+							document.cookie = `${cookie.SIDname}=;expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
+							return window.location.reload();
+						});
 					}
 					Toast.methods.addToast(result.message, 4000);
 					return reject(new Error(result.message));

+ 2 - 1
frontend/dist/config/template.json

@@ -7,7 +7,8 @@
 	"frontendPort": "81",
   	"cookie": {
 		"domain": "localhost",
-		"secure": false
+		"secure": false,
+		"SIDname": "SID"
 	},
 	"siteSettings": {
 		"logo": "/assets/wordmark.png",