Last active
December 10, 2015 13:29
-
-
Save shaharke/4441555 to your computer and use it in GitHub Desktop.
socket-io session bases authorization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script src="http://localhost:3000/socket.io/socket.io.js" type="text/javascript"></script> | |
<script type="text/javascript"> | |
tick = io.connect('http://localhost:3000/'); | |
tick.on('data', function (data) { | |
console.log(data); | |
}); | |
tick.on('error', function (reason){ | |
console.error('Unable to connect Socket.IO', reason); | |
}); | |
tick.on('connect', function (){ | |
console.info('successfully established a working and authorized connection'); | |
}); | |
</script> | |
</head> | |
<body> | |
Open the browser console to see tick-tocks! | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = express(); | |
app.configure(function () { | |
app.use(express.cookieParser()); | |
app.use(express.session({secret: 'secret', key: 'express.sid'})); | |
}); | |
app.get('/', function (req, res) { | |
res.sendfile(__dirname + '/index.html'); | |
}); | |
server = http.createServer(app) | |
server.listen(3000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
io = io.listen(server); | |
io.set('authorization', function (handshakeData, accept) { | |
if (handshakeData.headers.cookie) { | |
handshakeData.cookie = cookie.parse(handshakeData.headers.cookie); | |
handshakeData.sessionID = connect.utils.parseSignedCookie(handshakeData.cookie['express.sid'], 'secret'); | |
if (handshakeData.cookie['express.sid'] == handshakeData.sessionID) { | |
return accept('Cookie is invalid.', false); | |
} | |
} else { | |
return accept('No cookie transmitted.', false); | |
} | |
accept(null, true); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment