Search code examples
node.jsexpresscookiesnode.js-connect

Convenience method for parsing a signed cookie in Express/Connect?


Using node.js with Express. This question also pertains to Connect since that's the Express sub-component in question.

I receive a signed cookie from the client over socket.io. Then I take the value of it which is in this format:

s:sessionID.signature

Currently to validate this I had to manually require the cookie-signature module and use its unsign method, which requires me to .slice(2) the s: off of this string, and just feels kludgy.

Is there a convenience method for this? I feel like I ought to be able to just do something like:

mySessionID = express.unsignCookie(thisCookie, forThisSessionIDKey, withThisSecret);

But no such method exists... am I missing something?

Oh, BTW, I can't access res/req due to being in socket.io... Which is why I'm having to pass the session ID over from the client's cookie in the first place...


Solution

  • Here was the solution I ultimately found. Inside socket.js:

    var parseSignedCookie = connect.utils.parseSignedCookie;
    
    ...
    
    socket.on('taste:cookie', function (data, callback) {
       var sid = data.sid;
       sid = parseSignedCookie(sid['connect.sid'], "mySecret");