53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
from flask_babel import Babel
|
|
from flask_limiter import Limiter
|
|
from flask_limiter.util import get_remote_address
|
|
from flask_login import LoginManager
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
from flask_wtf.csrf import CSRFProtect
|
|
from werkzeug.security import check_password_hash, generate_password_hash
|
|
|
|
# Database and extension initialization
|
|
db = SQLAlchemy()
|
|
login_manager = LoginManager()
|
|
login_manager.login_view = 'auth.login'
|
|
login_manager.login_message_category = 'info'
|
|
csrf = CSRFProtect()
|
|
|
|
# Internationalisation. French is the primary language of the site; English
|
|
# stays available. See app/i18n.py for how a locale is chosen.
|
|
babel = Babel()
|
|
|
|
# Rate limiter for brute-force protection.
|
|
#
|
|
# No storage is named here on purpose: it comes from RATELIMIT_STORAGE_URI in
|
|
# app.config, which create_app fills from the environment and defaults to
|
|
# `memory://` (SEC-WEB-004). Naming it in both places is how the two drift.
|
|
limiter = Limiter(key_func=get_remote_address, default_limits=["1000 per day", "200 per hour"])
|
|
|
|
|
|
def hash_password(password):
|
|
"""
|
|
Hash a plain text password using werkzeug's security functions.
|
|
|
|
Args:
|
|
password (str): The plain text password to hash.
|
|
|
|
Returns:
|
|
str: The hashed password string.
|
|
"""
|
|
return generate_password_hash(password)
|
|
|
|
|
|
def check_password(password_hash, password):
|
|
"""
|
|
Verify a password against its hash.
|
|
|
|
Args:
|
|
password_hash (str): The stored password hash.
|
|
password (str): The plain text password to verify.
|
|
|
|
Returns:
|
|
bool: True if the password matches the hash, False otherwise.
|
|
"""
|
|
return check_password_hash(password_hash, password)
|