How to override Flask-Security default messages?
flask, flask-security, python
Solution
From reading through the code, it looks like it sets config defaults on all of the messages:
_default_messages = {
'INVALID_PASSWORD': ('Invalid password', 'error'),
}
... later on in the init method ....
for key, value in _default_messages.items():
app.config.setdefault('SECURITY_MSG_' + key, value)
To change the message, it looks like all you'd have to do is set this in your app.config:
SECURITY_MSG_INVALID_PASSWORD = ('Your username and password do not match our records', 'error'),
If it doesn't, it looks like it would not be difficult to refactor the module to use babel, or something similar. Would be a great thing to do as a good FOSS citizen.
Problem
Flask-Security takes a lot of the grunt work out of authentication and authorization for Python Flask web application development. I've run into one snag, though. On the login page, messages are flashed in response to various invalid inputs. Examples include: - Specified user does not exist - Invalid password - Account is disabled This isn't in accordance with security best practices. You should not divulge to the user the details of why his or her login attempt was rejected. The above messages make it easier for a hacker to identify valid usernames. I'd like to override these standard Flask-Security messages, replacing them all with something like "Invalid username or password." However, I haven't found a convenient way to do so. These messages are stored in _default_messages in site-packages/flask_security/core.py. I could modify that file, but that's not a good solution: it will break if I reinstall or update Flask-Security. I know I can customize Flask-Security's default views. But the views contain helpful code such as ``` {{ render_field_with_errors(login_user_form.email) }} ``` that hides the implementation details of the login form. I wouldn't want to discard all that helpful code and rewrite most of it just to change a few messages. Does anyone know a better way to customize Flask-Security's login messages?