Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to securely store credentials per session in Flask

I have a Flask application on my local network which connects to a mail server and web service. All three use the same LDAP authentication source, and I would like to avoid requiring users to provide the same credentials each time the application connects to one of these interfaces.

What is the most secure way to hold a user's credentials for the lifetime of the session so they can be shared with the other interfaces?

One option I've found is Flask-KVSession, which stores the session variables server-side.


Update: In testing, I have experimented with dropping the credentials into a dict in app.config when a user logs in. It seems like it should be a bad idea, but I haven't figured out why yet. I know it creates the possibility for the credentials for an active session to disappear, but it's easy enough to see if they exist and prompt for them again if they don't. Plus, they won't be written to the filesystem like variables in server-side sessions would be.

I'd like to know if I'm missing any obvious problems with this approach.

like image 754
robots.jpg Avatar asked Sep 14 '11 20:09

robots.jpg


People also ask

Is Flask session secure?

The browser will never send secure cookies with requests that are not encrypted. With Flask, you can control the secure flag on the session cookie with the SESSION_COOKIE_SECURE configuration setting. By default, it is set to False , which makes the session cookie available to both HTTP and HTTPS connections.

How does Flask store password in database?

Quite simply put, the only way to securely store a password is to not store it at all. Instead what we do is put the password through a hashing algorithm, and store the hash output.

How do I manage my sessions in Flask?

Flask-Session is an extension for Flask that supports Server-side Session to your application. The Session is the time between the client logs in to the server and logs out of the server. The data that is required to be saved in the Session is stored in a temporary directory on the server.


1 Answers

I would definitely use something like Flask-KVSession to store the user's credentials in the server-side session (+1 for that - I hadn't seen that extension before). That will ensure that you are not passing the user's credentials back and forth in a cookie. I would add Flask-Login to deal with the more interesting parts of session management without having to discover all the issues yourself.

Dropping the credentials into app.config is not a good idea because app.config is not a LocalProxy and therefore is not thread-safe. You are not guaranteed that changes you make to app.config for one request will not wind up affecting other requests. (You can read more about context locals here and here).

like image 85
Sean Vieira Avatar answered Sep 28 '22 00:09

Sean Vieira