diff options
Diffstat (limited to 'src/jwebmail/read_mails.py')
-rw-r--r-- | src/jwebmail/read_mails.py | 65 |
1 files changed, 47 insertions, 18 deletions
diff --git a/src/jwebmail/read_mails.py b/src/jwebmail/read_mails.py index e1c3b8c..915567c 100644 --- a/src/jwebmail/read_mails.py +++ b/src/jwebmail/read_mails.py @@ -1,11 +1,17 @@ -import dbm -import shelve - +import redis from flask import current_app, g -from flask_login import current_user +from flask_login import UserMixin, current_user from .model.read_mails import QMailAuthuser +EXPIRATION_SEC = 60 * 60 * 25 + + +class JWebmailUser(UserMixin): + def __init__(self, mail_addr, password): + self.id = mail_addr + self.password = password + def build_qma(username, password): authenticator = current_app.config["JWEBMAIL"]["READ_MAILS"]["AUTHENTICATOR"] @@ -22,29 +28,52 @@ def login(username, password): return build_qma(username, password).verify_user() -def add_user(user): - with shelve.open("user_sessions", flag="c") as s: - s[user.get_id()] = user +def add_user(user: JWebmailUser): + passwd = current_app.config["JWEBMAIL"]["READ_MAILS"]["SESSION_STORE_PASSWD"] + r = redis.Redis( + host="localhost", + port=6379, + decode_responses=True, + protocol=3, + username="jwebmail", + password=passwd, + ) + r.setex(f"jwm:user:{user.get_id()}", EXPIRATION_SEC, user.password) -def load_user(username): - try: - with shelve.open("user_sessions", flag="r") as s: - user = s[username] - return user - except dbm.error: - return None - except KeyError: +def load_user(username: str) -> JWebmailUser: + passwd = current_app.config["JWEBMAIL"]["READ_MAILS"]["SESSION_STORE_PASSWD"] + r = redis.Redis( + host="localhost", + port=6379, + decode_responses=True, + protocol=3, + username="jwebmail", + password=passwd, + ) + passwd = r.getex(f"jwm:user:{username}", EXPIRATION_SEC) + if passwd is None: return None + return JWebmailUser(username, passwd) def get_read_mails_logged_in(): if "read_mails" in g: return g.read_mails - with shelve.open("user_sessions", flag="r") as s: - user_data = s[current_user.get_id()] + passwd = current_app.config["JWEBMAIL"]["READ_MAILS"]["SESSION_STORE_PASSWD"] + r = redis.Redis( + host="localhost", + port=6379, + decode_responses=True, + protocol=3, + username="jwebmail", + password=passwd, + ) + passwd = r.get(f"jwm:user:{current_user.get_id()}") + if passwd is None: + raise KeyError(current_user.get_id()) - qma = build_qma(current_user.get_id(), user_data.password) + qma = build_qma(current_user.get_id(), passwd) g.read_mails = qma return qma |