zohl / servant-auth-cookie

Authentication via encrypted cookies
BSD 3-Clause "New" or "Revised" License
23 stars 23 forks source link

Getting a unique session id #32

Closed mschristiansen closed 7 years ago

mschristiansen commented 7 years ago

I need a unique session id and I could obviously generate one and store it in cookiePayload together with UserId and whatever else, but is cookieIV already usable for this?

Think I'll end with something like HashMap SessionId (TChan Message) and will also need a way to expire entries in the HashMap. Could possibly use the entire cookie as a key, which would give me that information.

zohl commented 7 years ago

Unfortunately, you cannot use IV as a session id, as it is changing on every request:

So, indeed, you have to store session id in the payload. And for the same reason you cannot use the whole cookie as a key (it starts with IV, which is changing during a session).

As for expiration checks, it's not that straightforward. I don't know details of your use case, so I'll clarify few details that might be useful for you :)

Thus said, it really depends on your use case. For example, if you want to implement CSRF protection, you do not need a table at all: session tokens will be discarded as soon as cookies get expired. Or if you want to forbid a user to have more that N sessions simultaneously, you will have to keep table that will keep session ids and expiration times, and check before each addSession whether there are less that N non-expired sessions. (And eventually clear it from expired ones.)

mschristiansen commented 7 years ago

I'm using server-sent events (SSE) and sending messages using TChans, was looking at having a channel for each user to avoid sending full state to everyone when a new user connects. Having a channel for each user would allow me to send messages to individual users and messages will "wait" for a user. Not strictly required for my application, but would be more elegant.

Thanks for your clarifications.