For security reasons we want to be able to invalidate all of a user's active sessions, for example if they change their password, or just want to be able to force log out their other sessions. We're using Node.js, Express, express-sessions and the Redis session store. In our app we have (CoffeeScript):
app.use express.session
cookie:
maxAge: 5 * 24 * 60 * 60 * 1000 # 5 days in ms
store: new RedisStore(client: rclient)
key: "secret-key"
The Redis store works by mapping the unique session id to whatever data you store in the session. For example:
# In an HTTP request
req.session.user = { _id: "user-id" }
in Redis becomes:
> get "sess:<session-id>"
'{ "user": { "_id": "user-id" } } '
What we need is a way to track all sessions that correspond to each user id, so that we can remove these from Redis if we want to invalidate a user's sessions. The following caveats apply:
- The sessions are given a TTL in Redis equal to the maxAge of the cookie. The tracking mechanism for each session should also expire after this time to avoid stale data.
- Not all sessions will necessarily be associate to a user. Some are just used to track anonymous session details.
The naive approach of adding another reverse look up key in Redis (for example mapping user_id to a set of session ids for the user) fails when it comes to caveat (1).
This feels like a problem that other sites using Express must have encountered, since it's a very common security pattern. Does anyone have any suggestions for how to track the user sessions and then invalidate them on demand?
Thank you!