Files
Aurora-Core/app/services/user_service.py
T
NixReaperandClaude Sonnet 5 7bee596a0e
CI / lint (push) Canceled after 0s
CI / typecheck (push) Canceled after 0s
CI / unit-tests (push) Canceled after 0s
CI / integration-tests (push) Canceled after 0s
CI / container-build (push) Canceled after 0s
CI / dependency-audit (push) Canceled after 0s
Add user accounts: registration, login, and JWT auth
The first authenticated-user concept in Aurora Core -- everything else in
this codebase is public, read-only catalog data. Foundation for ratings
and a watchlist (not yet built): POST /auth/register, POST /auth/login,
GET /auth/me.

Security specifics worth knowing:
- Passwords hashed with argon2 (via argon2-cffi), never stored plain or
  logged.
- Login returns an identical 401 for a nonexistent email and a wrong
  password on a real one, including matching response timing
  (burn_time_like_a_password_check) -- otherwise either the status code
  or the timing alone lets a client enumerate registered emails.
- AURORA_SECRET_KEY now actually signs JWTs (previously defined but
  unused anywhere) -- .env.example's existing token_urlsafe(48) guidance
  already produces a long enough key, just flagged its new stakes.
- Email matched case-insensitively via explicit normalization in the
  service layer, not relied on from Pydantic's EmailStr (which does not
  lowercase the local part).

12 new integration tests (register/login/me over real HTTP via the
`client` fixture) plus 10 unit tests for the hashing/JWT logic itself,
following the same-day lesson that only unit tests actually execute
locally without Postgres -- the auth core is tested for real here rather
than only through integration tests that might not run before a deploy.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-06 06:15:39 -05:00

54 lines
1.7 KiB
Python

"""Registration and login. See app/core/auth.py for hashing/token
mechanics -- this layer owns the business rules (duplicate email, wrong
credentials)."""
from __future__ import annotations
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.auth import (
burn_time_like_a_password_check,
hash_password,
normalize_email,
verify_password,
)
from app.models.user import User
from app.repositories.user_repository import create_user, get_user_by_email
class EmailAlreadyRegisteredError(Exception):
pass
class InvalidCredentialsError(Exception):
pass
async def register_user(
session: AsyncSession, *, email: str, password: str, display_name: str | None = None
) -> User:
normalized = normalize_email(email)
if await get_user_by_email(session, normalized) is not None:
raise EmailAlreadyRegisteredError(normalized)
return await create_user(
session,
email=normalized,
hashed_password=hash_password(password),
display_name=display_name,
)
async def authenticate_user(session: AsyncSession, *, email: str, password: str) -> User:
normalized = normalize_email(email)
user = await get_user_by_email(session, normalized)
if user is None:
# See burn_time_like_a_password_check's docstring: an email that
# doesn't exist must fail no faster than a real one with a wrong
# password, or response timing alone reveals which emails are
# registered.
burn_time_like_a_password_check()
raise InvalidCredentialsError()
if not user.is_active or not verify_password(password, user.hashed_password):
raise InvalidCredentialsError()
return user