flask-security icon indicating copy to clipboard operation
flask-security copied to clipboard

Server Side session expiration

Open kellyjanderson opened this issue 8 years ago • 6 comments

We are looking to implement a feature much like Facebook and Google's logout all other sessions. To do this the server needs to be able to invalidate a token. Is there any way for flask security to do this?

kellyjanderson avatar Jan 29 '17 20:01 kellyjanderson

We have done something which you might find useful. You can create a model [1] that stores relation between users and sessions. Then you need set of functions [2] for populating/cleanning these relations which you can connect to specific signals or views.

-- [1] https://github.com/inveniosoftware/invenio-accounts/blob/master/invenio_accounts/models.py#L109-L126 [2] https://github.com/inveniosoftware/invenio-accounts/blob/master/invenio_accounts/sessions.py

jirikuncar avatar Jan 30 '17 09:01 jirikuncar

@jirikuncar That is what we were thinking of doing. Didn't you have to update the functions that require authorization to check the state of the tokens in the database?

RoosterKelly avatar Feb 02 '17 21:02 RoosterKelly

@RoosterKelly the sessions get deleted on logout and expired by Redis automatically. There is however one more thing you want to do when deactivating user - removing all active sessions (see https://github.com/inveniosoftware/invenio-accounts/blob/master/invenio_accounts/datastore.py).

jirikuncar avatar Feb 03 '17 10:02 jirikuncar

Seems like a huge oversight that there's no way to invalidate sessions. If a user logs onto a public computer and forgets to log out, there's literally no way to kill the session. Resetting the password at the very least should kill it, and there should be a function that lets us kill the sessions.

patrickyan avatar Jun 26 '17 18:06 patrickyan

@patrickyan you would need server side sessions to invalidate sessions remotely (please see Flask-KVSession extension).

jirikuncar avatar Jun 29 '17 08:06 jirikuncar

Hey, I'm keen to have session expiry happen when a user changes their password.

This library could take a leaf out of Django's book which stores a hmac of the password in the session during login. Then checks it during each request; as part of loading the user.

If this sounds like a valid approach I'm happy to spend some time and put a PR up for it.

software-opal avatar Jan 15 '19 01:01 software-opal