Files
romm/backend/handler/notification_channels/confirmation.py
Georges-Antoine Assi 96e2c2ad42 refactor(backend): pr-ready follow-ups for device installs
- Replace the inline bytes-to-text decodes with redis_handler.as_text
- Add utils.auth.token_device_id for a request's token-bound device
- Look up a group's member tokens in one query and close them in one pass

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 15:04:56 -04:00

95 lines
3.2 KiB
Python

"""The code an email channel is confirmed with, proving the address is the user's."""
import asyncio
import hashlib
import hmac
import secrets
from typing import Final
from handler.email_handler import send_email
from handler.redis_handler import as_text, async_cache
CODE_TTL_SECONDS: Final = 30 * 60
RESEND_COOLDOWN_SECONDS: Final = 60
MAX_ATTEMPTS: Final = 5
class CodeCooldownError(RuntimeError):
"""A code went out too recently to send another."""
def _key(channel_id: int, part: str) -> str:
return f"notification-channel:{channel_id}:{part}"
def _cooldown_keys(user_id: int, address: str) -> tuple[str, str]:
# Per user and per address, since a channel is easy to delete and add again.
address_digest = hashlib.sha256(address.strip().lower().encode()).hexdigest()
return (
f"notification-channel-cooldown:user:{user_id}",
f"notification-channel-cooldown:address:{address_digest}",
)
def _digest(channel_id: int, code: str) -> str:
return hashlib.sha256(f"{channel_id}:{code}".encode()).hexdigest()
async def issue_code(channel_id: int, user_id: int, address: str) -> None:
"""Email a fresh code to the address, replacing any earlier one.
Raises:
CodeCooldownError: A code went out to the user, or to the address, less
than a minute ago.
EmailError: The email could not be sent.
"""
cooldowns = _cooldown_keys(user_id, address)
claimed = [
key
for key in cooldowns
if await async_cache.set(key, "1", ex=RESEND_COOLDOWN_SECONDS, nx=True)
]
if len(claimed) < len(cooldowns):
if claimed:
await async_cache.delete(*claimed)
raise CodeCooldownError("Wait a minute before asking for another code")
code = f"{secrets.randbelow(10**6):06d}"
await async_cache.delete(_key(channel_id, "attempts"))
await async_cache.set(
_key(channel_id, "code"), _digest(channel_id, code), ex=CODE_TTL_SECONDS
)
try:
await asyncio.to_thread(
send_email,
address,
"Your RomM confirmation code",
f"Your code is {code}. Enter it in RomM to start getting notifications "
f"at this address. It expires in {CODE_TTL_SECONDS // 60} minutes.\n\n"
"If you didn't add this address to RomM, you can ignore this email.",
)
except Exception:
await async_cache.delete(_key(channel_id, "code"), *cooldowns)
raise
async def check_code(channel_id: int, code: str) -> bool:
"""Whether the code is the one last sent; a match or too many misses spends it."""
stored = await async_cache.get(_key(channel_id, "code"))
if not stored:
return False
pipe = async_cache.pipeline()
pipe.incr(_key(channel_id, "attempts"))
# In the same transaction, so no counter is left without a TTL.
pipe.expire(_key(channel_id, "attempts"), CODE_TTL_SECONDS, nx=True)
attempts, _ = await pipe.execute()
if attempts > MAX_ATTEMPTS:
await async_cache.delete(_key(channel_id, "code"))
return False
if not hmac.compare_digest(as_text(stored), _digest(channel_id, code.strip())):
return False
await async_cache.delete(_key(channel_id, "code"), _key(channel_id, "attempts"))
return True