46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
from flask_sqlalchemy import SQLAlchemy
|
|
from flask_login import LoginManager
|
|
from flask_wtf.csrf import CSRFProtect
|
|
from werkzeug.security import generate_password_hash, check_password_hash
|
|
from flask_limiter import Limiter
|
|
from flask_limiter.util import get_remote_address
|
|
|
|
# Database and extension initialization
|
|
db = SQLAlchemy()
|
|
login_manager = LoginManager()
|
|
login_manager.login_view = 'auth.login'
|
|
login_manager.login_message_category = 'info'
|
|
csrf = CSRFProtect()
|
|
|
|
# Rate limiter for brute-force protection
|
|
limiter = Limiter(
|
|
key_func=get_remote_address,
|
|
default_limits=["200 per day", "50 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) |