Repository navigation
Expand file tree
/
Copy pathextensions.py
More file actions
49 lines (40 loc) · 1.46 KB
/
Copy pathextensions.py
File metadata and controls
49 lines (40 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Flask extensions initialization.
Extensions are initialized here and imported by app.py, models and services.
"""
from flask_wtf.csrf import CSRFProtect
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
import bcrypt as bcrypt_lib
# CSRF Protection
csrf = CSRFProtect()
# Rate Limiter
limiter = Limiter(
key_func=get_remote_address,
default_limits=["200 per day", "50 per hour"],
storage_uri="memory://", # Will be overridden by config
)
# Bcrypt wrapper
class BcryptWrapper:
"""Wrapper for bcrypt to match Flask-Bcrypt API."""
def __init__(self, app=None):
self.app = app
if app is not None:
self.init_app(app)
def init_app(self, app):
"""Initialize with Flask app."""
self.app = app
self.log_rounds = app.config.get('BCRYPT_LOG_ROUNDS', 12)
def generate_password_hash(self, password):
"""Generate password hash."""
if isinstance(password, str):
password = password.encode('utf-8')
return bcrypt_lib.hashpw(password, bcrypt_lib.gensalt(self.log_rounds)).decode('utf-8')
def check_password_hash(self, pw_hash, password):
"""Check password against hash."""
if isinstance(password, str):
password = password.encode('utf-8')
if isinstance(pw_hash, str):
pw_hash = pw_hash.encode('utf-8')
return bcrypt_lib.checkpw(password, pw_hash)
bcrypt = BcryptWrapper()