quart-security 2.0.3__tar.gz → 2.0.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. {quart_security-2.0.3 → quart_security-2.0.5}/PKG-INFO +1 -1
  2. {quart_security-2.0.3 → quart_security-2.0.5}/pyproject.toml +1 -1
  3. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/core.py +0 -1
  4. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/datastore.py +15 -0
  5. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/forms.py +11 -4
  6. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/password.py +12 -0
  7. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/views.py +13 -5
  8. {quart_security-2.0.3 → quart_security-2.0.5}/LICENSE +0 -0
  9. {quart_security-2.0.3 → quart_security-2.0.5}/README.md +0 -0
  10. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/__init__.py +0 -0
  11. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/breach.py +0 -0
  12. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/decorators.py +0 -0
  13. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/email.py +0 -0
  14. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/models.py +0 -0
  15. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/proxies.py +0 -0
  16. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/py.typed +0 -0
  17. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/signals.py +0 -0
  18. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/state.py +0 -0
  19. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/templates/security/_macros.html +0 -0
  20. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/templates/security/change_password.html +0 -0
  21. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/templates/security/login_user.html +0 -0
  22. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/templates/security/mf_recovery.html +0 -0
  23. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/templates/security/mf_recovery_codes.html +0 -0
  24. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/templates/security/register_user.html +0 -0
  25. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/templates/security/two_factor_select.html +0 -0
  26. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/templates/security/two_factor_setup.html +0 -0
  27. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/templates/security/two_factor_verify_code.html +0 -0
  28. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/templates/security/wan_register.html +0 -0
  29. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/templates/security/wan_signin.html +0 -0
  30. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/templates/security/wan_verify.html +0 -0
  31. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/totp.py +0 -0
  32. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/utils.py +0 -0
  33. {quart_security-2.0.3 → quart_security-2.0.5}/quart_security/webauthn.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: quart-security
3
- Version: 2.0.3
3
+ Version: 2.0.5
4
4
  Summary: Native async auth for Quart
5
5
  Author: quart-security maintainers
6
6
  Requires-Python: >=3.11
@@ -4,7 +4,7 @@ build-backend = "flit_core.buildapi"
4
4
 
5
5
  [project]
6
6
  name = "quart-security"
7
- version = "2.0.3"
7
+ version = "2.0.5"
8
8
  description = "Native async auth for Quart"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.11"
@@ -200,7 +200,6 @@ class Security:
200
200
  "SECURITY_WAN_EXPECTED_ORIGIN": None,
201
201
  "SECURITY_WAN_REQUIRE_USER_VERIFICATION": True,
202
202
  "SECURITY_FRESHNESS": timedelta(minutes=60),
203
- "SECURITY_FRESHNESS_GRACE_PERIOD": timedelta(minutes=60),
204
203
  "SECURITY_LOGIN_MAX_ATTEMPTS": 5,
205
204
  "SECURITY_LOCKOUT_MINUTES": 15,
206
205
  "SECURITY_POST_LOGIN_VIEW": "/",
@@ -58,6 +58,21 @@ class SQLAlchemyUserDatastore:
58
58
  return stmt
59
59
 
60
60
  async def find_user(self, **kwargs):
61
+ if "email" in kwargs:
62
+ email = kwargs.pop("email")
63
+ # ponytail: lower(email) skips a plain email index, so each login scans
64
+ # the user table; add a functional index if logins slow with user count.
65
+ stmt = (
66
+ self._select(self.user_model)
67
+ .filter_by(**kwargs)
68
+ .where(func.lower(self.user_model.email) == email.lower())
69
+ .limit(2)
70
+ )
71
+ result = await self.session.execute(stmt)
72
+ users = result.scalars().all()
73
+ if len(users) > 1:
74
+ raise ValueError("Multiple users share the same case-insensitive email")
75
+ return users[0] if users else None
61
76
  return await self._first(self.user_model, **kwargs)
62
77
 
63
78
  async def find_role(self, name):
@@ -4,11 +4,15 @@ import secrets
4
4
 
5
5
  from markupsafe import Markup, escape
6
6
  from quart import request
7
- from wtforms import BooleanField, PasswordField, RadioField, SelectChoice, StringField
7
+ from wtforms import PasswordField, RadioField, SelectChoice, StringField
8
8
  from wtforms.form import Form
9
9
  from wtforms.validators import DataRequired, Email, EqualTo, Optional
10
10
 
11
11
 
12
+ def _strip_email(value):
13
+ return value.strip() if value is not None else value
14
+
15
+
12
16
  class QuartForm(Form):
13
17
  """Small helper to create WTForms from Quart request form data."""
14
18
 
@@ -52,14 +56,17 @@ class QuartForm(Form):
52
56
 
53
57
 
54
58
  class LoginForm(QuartForm):
55
- email = StringField("Email", validators=[DataRequired(), Email()])
59
+ email = StringField(
60
+ "Email", filters=[_strip_email], validators=[DataRequired(), Email()]
61
+ )
56
62
  password = PasswordField("Password", validators=[DataRequired()])
57
- remember = BooleanField("Remember me")
58
63
 
59
64
 
60
65
  class RegisterForm(QuartForm):
61
66
  name = StringField("Full Name", validators=[Optional()])
62
- email = StringField("Email", validators=[DataRequired(), Email()])
67
+ email = StringField(
68
+ "Email", filters=[_strip_email], validators=[DataRequired(), Email()]
69
+ )
63
70
  password = PasswordField("Password", validators=[DataRequired()])
64
71
  password_confirm = PasswordField(
65
72
  "Confirm Password",
@@ -1,6 +1,7 @@
1
1
  """Password hashing and validation helpers."""
2
2
 
3
3
  import asyncio
4
+ import secrets
4
5
 
5
6
  from passlib.context import CryptContext
6
7
  from quart import current_app
@@ -82,6 +83,17 @@ async def verify_password_async(password: str, password_hash: str, *, app=None)
82
83
  return await asyncio.to_thread(verify_password, password, password_hash, app=app)
83
84
 
84
85
 
86
+ async def unknown_user_hash_async(*, app=None) -> str:
87
+ """A hash to verify against when no account matches, so a login for an
88
+ unknown or inactive email costs the same as a wrong password."""
89
+ app = app or current_app._get_current_object()
90
+ cached = app.extensions.get("quart_security_unknown_hash")
91
+ if cached is None:
92
+ cached = await hash_password_async(secrets.token_urlsafe(32), app=app)
93
+ app.extensions["quart_security_unknown_hash"] = cached
94
+ return cached
95
+
96
+
85
97
  def validate_password(password: str, min_length: int = 12) -> list[str]:
86
98
  errors: list[str] = []
87
99
  if len(password) < min_length:
@@ -37,6 +37,7 @@ from .forms import (
37
37
  from .password import (
38
38
  hash_password_async,
39
39
  password_needs_rehash,
40
+ unknown_user_hash_async,
40
41
  validate_password,
41
42
  verify_password_async,
42
43
  )
@@ -364,11 +365,14 @@ async def login():
364
365
  user.locked_until = None
365
366
  await _commit()
366
367
 
367
- if (
368
- user
369
- and getattr(user, "active", True)
370
- and await verify_password_async(form.password.data, user.password)
371
- ):
368
+ # Always run one verify, so response time does not reveal which emails
369
+ # have an active account.
370
+ known = user is not None and getattr(user, "active", True)
371
+ password_ok = await verify_password_async(
372
+ form.password.data,
373
+ user.password if known else await unknown_user_hash_async(),
374
+ )
375
+ if known and password_ok:
372
376
  # Transparent rehash: upgrade weak/legacy hash on successful login
373
377
  if password_needs_rehash(user.password):
374
378
  user.password = await hash_password_async(form.password.data)
@@ -929,7 +933,11 @@ async def wan_register_response():
929
933
  await flash("Passkey registration failed.", "error")
930
934
  return redirect(url_for_security("wan_register"))
931
935
 
936
+ # Look across all users too: a software authenticator can pick any credential
937
+ # ID, and sign-in resolves the owner by ID alone.
932
938
  existing = await _find_webauthn_credential(
939
+ verification["credential_id"], user=None
940
+ ) or await _find_webauthn_credential(
933
941
  verification["credential_id"], user=current_user
934
942
  )
935
943
  if existing:
File without changes
File without changes