security update
This commit is contained in:
@@ -1,64 +1,35 @@
|
|||||||
from flask import Blueprint, render_template, request, redirect, url_for, flash, make_response
|
from flask import Blueprint, render_template, request, redirect, url_for, flash, make_response, current_app
|
||||||
from flask_login import login_user, logout_user, login_required, current_user
|
from flask_login import login_user, logout_user, login_required, current_user
|
||||||
from models import db
|
from models import db
|
||||||
from models import User
|
from models import User
|
||||||
from log import Log
|
from log import Log
|
||||||
import pickle
|
from itsdangerous import BadSignature, SignatureExpired, URLSafeTimedSerializer
|
||||||
from cryptography.fernet import Fernet
|
|
||||||
import datetime
|
|
||||||
|
|
||||||
|
|
||||||
auth_bp = Blueprint("auth", __name__)
|
auth_bp = Blueprint("auth", __name__)
|
||||||
|
|
||||||
# =====================================================
|
AUTOLOGIN_MAX_AGE_SECONDS = 7 * 24 * 60 * 60
|
||||||
# SECRET KEY (only for this program)
|
AUTOLOGIN_SALT = "lapp-autologin"
|
||||||
# =====================================================
|
|
||||||
# Generate once using: Fernet.generate_key()
|
|
||||||
_SECRET_KEY = b'YFK7QCyTzhyLO4vqrnRxvDAI5uu8mXEYrInEjbRoQgs='
|
|
||||||
fernet = Fernet(_SECRET_KEY)
|
|
||||||
|
|
||||||
# =====================================================
|
def autologin_serializer():
|
||||||
# ENCRYPT
|
return URLSafeTimedSerializer(current_app.config["SECRET_KEY"], salt=AUTOLOGIN_SALT)
|
||||||
# =====================================================
|
|
||||||
def encrypt_object(obj) -> bytes:
|
|
||||||
"""
|
|
||||||
Encrypt any Python object and return encrypted bytes.
|
|
||||||
"""
|
|
||||||
serialized = pickle.dumps(obj)
|
|
||||||
encrypted = fernet.encrypt(serialized)
|
|
||||||
return encrypted
|
|
||||||
|
|
||||||
# =====================================================
|
|
||||||
# DECRYPT
|
|
||||||
# =====================================================
|
|
||||||
def decrypt_object(encrypted_data: bytes):
|
|
||||||
"""
|
|
||||||
Decrypt bytes back into the original Python object.
|
|
||||||
"""
|
|
||||||
decrypted = fernet.decrypt(encrypted_data)
|
|
||||||
obj = pickle.loads(decrypted)
|
|
||||||
return obj
|
|
||||||
|
|
||||||
def create_cookie(userid):
|
def create_cookie(userid):
|
||||||
cookie_data = {
|
cookie_data = {
|
||||||
"user": userid,
|
"user": userid
|
||||||
"datetime": datetime.datetime.now()
|
|
||||||
}
|
}
|
||||||
return encrypt_object(cookie_data).decode('utf-8')
|
return autologin_serializer().dumps(cookie_data)
|
||||||
|
|
||||||
def is_cookie_ok_to_autologin(cookie_content):
|
def is_cookie_ok_to_autologin(cookie_content):
|
||||||
user = 0
|
if not cookie_content:
|
||||||
autologin = False
|
return 0
|
||||||
|
|
||||||
try:
|
try:
|
||||||
cookie_data = decrypt_object(cookie_content)
|
cookie_data = autologin_serializer().loads(cookie_content,
|
||||||
user = cookie_data["user"]
|
max_age=AUTOLOGIN_MAX_AGE_SECONDS)
|
||||||
dt = cookie_data["datetime"]
|
return int(cookie_data["user"])
|
||||||
lastweek = datetime.datetime.now() - datetime.timedelta(days=7)
|
except (BadSignature, SignatureExpired, KeyError, TypeError, ValueError):
|
||||||
autologin = dt > lastweek
|
|
||||||
except:
|
|
||||||
Log.info("Cookie error")
|
Log.info("Cookie error")
|
||||||
if autologin:
|
|
||||||
return user
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
#. @auth_bp.route("/", methods=["GET", "POST"])
|
#. @auth_bp.route("/", methods=["GET", "POST"])
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ if __name__ == "__main__":
|
|||||||
app.app_context().push()
|
app.app_context().push()
|
||||||
query = sa.select(User)
|
query = sa.select(User)
|
||||||
users = db.session.scalars(query).all()
|
users = db.session.scalars(query).all()
|
||||||
print(users)
|
|
||||||
if len(users) == 0:
|
if len(users) == 0:
|
||||||
g = Group(secret="This is the group just for the admin")
|
g = Group(secret="This is the group just for the admin")
|
||||||
db.session.add(g)
|
db.session.add(g)
|
||||||
|
|||||||
Reference in New Issue
Block a user