Skip to content

Commit

Permalink
fix session token/cookie handling for arbitrary server IPs; fix realt…
Browse files Browse the repository at this point in the history
…ime update distribution via websockets
  • Loading branch information
mntmn committed May 1, 2018
1 parent f752ec4 commit c19f00b
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 16,012 deletions.
3 changes: 2 additions & 1 deletion middlewares/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ module.exports = (req, res, next) => {
else db.User.findOne({where: {_id: session.user_id}})
.then(user => {
if (!user) {
res.clearCookie('sdsession');
var domain = (process.env.NODE_ENV == "production") ? new URL(config.get('endpoint')).hostname : req.headers.hostname;
res.clearCookie('sdsession', { domain: domain });

if (req.accepts("text/html")) {
res.send("Please clear your cookies and try again.");
Expand Down
15,931 changes: 0 additions & 15,931 deletions public/javascripts/spacedeck.js

This file was deleted.

31 changes: 0 additions & 31 deletions public/javascripts/spacedeck_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,6 @@ var SpacedeckRoutes = {
// #hash
if (event.currentTarget.hash && event.currentTarget.hash.length>1) return;

console.log("clicked", event.currentTarget.pathname);

// external link?
if (event.currentTarget.host != location.host) return;

Expand All @@ -269,35 +267,6 @@ var SpacedeckRoutes = {
event.preventDefault();
}.bind(this));

if (location.host!=ENV.webHost) {
if (!subdomainTeam) {
location.href = ENV.webEndpoint;
return;
} else {
if(subdomainTeam.subdomain) {
var realHost = (subdomainTeam.subdomain + "." + ENV.webHost);
if (location.host != realHost) {
location.href = realHost;
return;
}
} else {
location.href = ENV.webEndpoint;
return;
}
}
}

if (this.logged_in) {
if (this.user.team) {
if (this.user.team.subdomain && this.user.team.subdomain.length > 0) {
var realHost = (this.user.team.subdomain + "." + ENV.webHost);
if (location.host != realHost) {
location.href = location.protocol + "//" + realHost + location.pathname;
return;
}
}
}
}
this.internal_route(location.pathname);
},

Expand Down
4 changes: 0 additions & 4 deletions public/javascripts/spacedeck_users.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ SpacedeckUsers = {
},

finalize_login: function(session_token, on_success) {
if(!window.socket_auth || window.socket_auth == '' || window.socket_auth == 'null') {
window.socket_auth = session_token;
}

this.load_user(function(user) {
if (this.invitation_token) {
accept_invitation(this.invitation_token, function(memberships){
Expand Down
8 changes: 5 additions & 3 deletions public/javascripts/spacedeck_websockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ SpacedeckWebsockets = {
else if (msg.action == "delete" && msg.object) {
if (this.active_space) {
var o = msg.object;
if(o._id){
if (o._id){
var existing_artifact = this.find_artifact_by_id(o._id);
if (existing_artifact) {
var idx = this.active_space_artifacts.indexOf(existing_artifact);
this.active_space_artifacts.splice(idx, 1);
} else console.log("existing artifact to delete not found");
}else console.error("object without _id");
} else console.error("object without _id");
}
}
}
Expand Down Expand Up @@ -101,11 +101,13 @@ SpacedeckWebsockets = {
}

if (this.websocket && this.websocket.readyState==1) {
var token = "";
if (this.user) token = this.user.token;
var auth_params = {
action: "auth",
editor_auth: space_auth,
editor_name: this.guest_nickname,
auth_token: window.socket_auth,
auth_token: token,
space_id: space._id
};
console.log("[websocket] auth space");
Expand Down
24 changes: 9 additions & 15 deletions routes/api/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,11 @@ router.post('/', function(req, res) {
db.User.findOne({where: {email: email}})
.error(err => {
res.sendStatus(404);
//res.status(400).json({"error":"session.users"});
})
.then(user => {
console.log("!!! user: ",user.password_hash);

if (bcrypt.compareSync(password, user.password_hash)) {
crypto.randomBytes(48, function(ex, buf) {
var token = buf.toString('hex');
console.log("!!! token: ",token);

var session = {
user_id: user._id,
Expand All @@ -47,7 +43,7 @@ router.post('/', function(req, res) {
res.sendStatus(500);
})
.then(() => {
var domain = (process.env.NODE_ENV == "production") ? new URL(config.get('endpoint')).hostname : "localhost";
var domain = (process.env.NODE_ENV == "production") ? new URL(config.get('endpoint')).hostname : req.headers.hostname;
res.cookie('sdsession', token, { domain: domain, httpOnly: true });
res.status(201).json(session);
});
Expand All @@ -60,16 +56,14 @@ router.post('/', function(req, res) {

router.delete('/current', function(req, res, next) {
if (req.user) {
/*var user = req.user;
var newSessions = user.sessions.filter( function(session){
return session.token != req.token;
});*/
//user.sessions = newSessions;
//user.save(function(err, result) {
var domain = new URL(config.get('endpoint')).hostname;
res.clearCookie('sdsession', { domain: domain });
res.sendStatus(204);
//});
var token = req.cookies['sdsession'];
db.Session.findOne({where: {token: token}})
.then(session => {
session.destroy();
});
var domain = (process.env.NODE_ENV == "production") ? new URL(config.get('endpoint')).hostname : req.headers.hostname;
res.clearCookie('sdsession', { domain: domain });
res.sendStatus(204);
} else {
res.sendStatus(404);
}
Expand Down
3 changes: 2 additions & 1 deletion routes/api/space_artifacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ router.post('/:artifact_id/payload', function(req, res, next) {
var progress_callback = function(progress_msg) {
a.description = progress_msg;
a.save();
redis.sendMessage("update", a, a.toJSON(), req.channelId);
redis.sendMessage("update", a, JSON.stringify(a), req.channelId);
};

stream.on('finish', function() {
Expand Down Expand Up @@ -171,6 +171,7 @@ router.put('/:artifact_id', function(req, res, next) {
}}).then(rows => {
db.unpackArtifact(newAttr);
db.Space.update({ updated_at: new Date() }, {where: {_id: req.space._id} });
newAttr._id = a._id;
res.distributeUpdate("Artifact", newAttr);
});
});
Expand Down
1 change: 0 additions & 1 deletion routes/api/space_exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ router.get('/zip', function(req, res, next) {
});

router.get('/html', function(req, res) {
console.log("!!!!! hello ");
db.Artifact.findAll({where: {
space_id: req.space._id
}}).then(function(artifacts) {
Expand Down
11 changes: 9 additions & 2 deletions routes/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ var glob = require('glob');

router.get('/current', function(req, res, next) {
if (req.user) {
console.log(req.user.team);
res.status(200).json(req.user);
var u = _.clone(req.user.dataValues);
delete u.password_hash;
delete u.password_reset_token;
delete u.confirmation_token;
u.token = req.cookies['sdsession'];

console.log(u);

res.status(200).json(u);
} else {
res.status(401).json({"error":"user_not_found"});
}
Expand Down
30 changes: 7 additions & 23 deletions views/spacedeck.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,13 @@
window.browser_lang = '[[locale]]';
window.csrf_token = '[[csrf_token]]';

{% if process.env.NODE_ENV != "production" %}
var ENV = {
name: 'development',
webHost: "localhost:9666",
webEndpoint:"http://localhost:9666",
apiEndpoint: "http://localhost:9666",
websocketsEndpoint: "ws://localhost:9666"
};
{% else %}
var ENV = {
name: 'production',
webHost: location.host,
webEndpoint: location.origin,
apiEndpoint: location.origin,
websocketsEndpoint: location.origin.replace("https:","wss:").replace("http:","ws:")
};
{% endif %}

{% if subdomain_team %}
var subdomainTeam = [[ subdomain_team | json | safe ]];
{% else %}
var subdomainTeam = null;
{% endif %}
var ENV = {
name: 'development',
webHost: location.host,
webEndpoint: location.origin,
apiEndpoint: location.origin,
websocketsEndpoint: location.origin.replace("https:","wss:").replace("http:","ws:")
};
</script>

{% if process.env.NODE_ENV == "production" %}
Expand Down

0 comments on commit c19f00b

Please sign in to comment.