aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/auth.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--webmentions_ssg/auth.py27
1 files changed, 24 insertions, 3 deletions
diff --git a/webmentions_ssg/auth.py b/webmentions_ssg/auth.py
index 48a95b7..147220e 100644
--- a/webmentions_ssg/auth.py
+++ b/webmentions_ssg/auth.py
@@ -4,6 +4,8 @@
"""Configure user authentication and session loading."""
+from hmac import compare_digest
+
from flask_login import LoginManager
from . import DATABASE as db
@@ -15,11 +17,30 @@ AUTH.login_view = "root.login" # pyright: ignore[reportAttributeAccessIssue]
@AUTH.user_loader
-def load_user(id: int) -> User | None:
+def load_user(session_id: str) -> User | None:
"""
Load a user from the database by identifier.
- :param id: User identifier stored in the login session.
+ :param session_id: Database ID and password-hash fingerprint stored in the session.
:return: Matching user, or ``None`` if no user exists with the identifier.
"""
- return db.session.get(User, int(id))
+ try:
+ user_id, fingerprint = session_id.split(":", 1)
+ if (
+ not user_id.isascii()
+ or not user_id.isdecimal()
+ or len(user_id) > 19
+ or len(fingerprint) != 64
+ or any(char not in "0123456789abcdef" for char in fingerprint)
+ ):
+ return None
+ user = db.session.get(User, int(user_id))
+ except AttributeError, TypeError, ValueError:
+ return None
+
+ if user is None:
+ return None
+
+ if not compare_digest(session_id, user.get_id()):
+ return None
+ return user