auth: affirmative versioned sign-up acknowledgement (EN + IT)

Replaces the passive "by signing in you agree…" paragraph on /login with
an active un-pre-ticked checkbox over three short statements that the
user actively accepts. Each acceptance is recorded against a code-level
version pointer, so a future wording revision bumps the version and
prompts every user again.

- New app/legal.py::ACKNOWLEDGEMENT_VERSION (currently 1).
- New UserAcknowledgement model + migration 0027 (user_id FK CASCADE,
  version, lang, accepted_at, composite index on user_id+version).
- auth_service: has_acknowledged_current() and record_acknowledgement()
  helpers; POST /login validates the checkbox, falls through to a 400
  with the localised error otherwise, and writes a row iff the user has
  no current-version row (so existing-already-accepted users don't
  produce duplicates).
- GET /login: language detection mirrors the landing's
  detect_public_lang(); ?lang=en|it overrides; stamps the rtm.lang
  cookie; passes the locale dict + version into the template.
- login.html: EN/IT pill, localised lede/banner/legal footer, required
  checkbox in an acknowledgement block, hidden lang+ack_version fields.
  Submit disabled until the box is ticked (UX polish; the server check
  is what carries weight).
- locales/{en,it}.yaml: new auth.ack.* section with TODO(legal) marker.
  Wording matches the brief's substance pending solicitor sign-off.
- tests/test_signup_acknowledgement.py: 10 tests (EN + IT rejection,
  one row per acceptance, displayed-lang recorded, idempotent on
  current version, version-bump writes new row, helper unit tests).

The acknowledgement strengthens the user-civil-claim vector — combined
with the liability cap and the Ltd, it makes "I was misled into thinking
this was advice" much harder to argue. It does NOT move the regulatory
perimeter, which is governed by the content discipline shipped in
47dce1a. Belt-and-braces, not a substitute.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Giorgio Gilestro 2026-05-29 20:50:38 +02:00
parent 47dce1a1a4
commit f3ac65f8f7
9 changed files with 714 additions and 13 deletions

19
app/legal.py Normal file
View file

@ -0,0 +1,19 @@
"""Legal-surface constants.
Currently a one-liner: the version of the sign-up acknowledgement text
the user actively accepts at ``/login``.
Bumping this constant re-prompts every existing user on their next
sign-in, because the new value won't match their stored
``user_acknowledgements.version`` rows. The recorded row is what gives
the acknowledgement its evidentiary weight; the constant is the pointer
that says "the current canonical text is at version N". The text itself
lives in ``app/locales/{en,it}.yaml`` under ``auth.ack``.
When the solicitor finalises wording, edit the YAML files **and** bump
this number in the same change so the version pointer stays accurate.
"""
from __future__ import annotations
ACKNOWLEDGEMENT_VERSION: int = 1

View file

@ -118,3 +118,17 @@ footer:
meta:
description: "Understand markets. Don't gamble on them."
# TODO(legal): final wording pending solicitor sign-off. Bumping
# ACKNOWLEDGEMENT_VERSION (app/legal.py) re-prompts every existing user
# on next sign-in and writes a fresh row at the new version.
auth:
ack:
heading: "Before you create an account, please confirm:"
items:
- "This service is editorial and educational commentary on public market data. It is not financial, investment, or tax advice and is not a personal recommendation."
- "The operator is not authorised or regulated to provide investment advice. Nothing here is a recommendation to buy, sell, or hold any investment."
- "I make my own investment decisions and am solely responsible for them. For personal advice I will consult a regulated adviser."
checkbox_label: "I have read and accept these statements."
error_required: "Please tick the box to confirm the acknowledgement before continuing."
lang_switch_aria: "Language"

View file

@ -124,3 +124,17 @@ footer:
meta:
description: "Capisci i mercati. Non scommetterci sopra."
# TODO(legal): final wording pending solicitor sign-off. Bumping
# ACKNOWLEDGEMENT_VERSION (app/legal.py) re-prompts every existing user
# on next sign-in and writes a fresh row at the new version.
auth:
ack:
heading: "Prima di creare un account, conferma:"
items:
- "Questo servizio è commento editoriale ed educativo su dati di mercato pubblici. Non è consulenza finanziaria, di investimento o fiscale e non è una raccomandazione personalizzata."
- "L'operatore non è autorizzato né regolamentato a fornire consulenza in materia di investimenti. Nulla qui è una raccomandazione di acquisto, vendita o detenzione di alcuno strumento finanziario."
- "Prendo da solo le mie decisioni di investimento e ne sono unico responsabile. Per una consulenza personalizzata mi rivolgerò a un consulente regolamentato."
checkbox_label: "Ho letto e accetto queste dichiarazioni."
error_required: "Spunta la casella per confermare prima di continuare."
lang_switch_aria: "Lingua"

View file

@ -246,6 +246,35 @@ class ReviewerVerdict(Base):
model: Mapped[str | None] = mapped_column(String(64))
class UserAcknowledgement(Base):
"""Append-only record of a user actively accepting the sign-up
acknowledgement at ``/login``. See app/legal.py for the version
constant and app/locales/{en,it}.yaml::auth.ack for the canonical text.
One row per (user, version) acceptance. The value of this table is
evidentiary: 'user X accepted the version-N text on date D, in the
language they were shown'. A future wording revision bumps
ACKNOWLEDGEMENT_VERSION; existing users see the prompt again on next
sign-in and a new row is written at the new version."""
__tablename__ = "user_acknowledgements"
id: Mapped[int] = mapped_column(_PK, primary_key=True, autoincrement=True)
user_id: Mapped[int] = mapped_column(
Integer,
ForeignKey("users.id", ondelete="CASCADE"),
nullable=False,
)
version: Mapped[int] = mapped_column(SmallInteger, nullable=False)
# Language the user actually saw — what they accepted is keyed to it.
lang: Mapped[str] = mapped_column(String(8), nullable=False)
accepted_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), default=utcnow, nullable=False, index=True,
)
__table_args__ = (
Index("ix_user_acknowledgements_user_version", "user_id", "version"),
)
# Portfolio / PortfolioSnapshot / Position removed in Phase G —
# holdings live in the browser, the server stores only the anonymous
# ticker universe + public market data.

View file

@ -34,13 +34,38 @@ from app.auth import (
)
from app.config import get_settings
from app.db import get_session, utcnow
from app.legal import ACKNOWLEDGEMENT_VERSION
from app.logging import get_logger
from app.services.auth_service import AuthError, get_or_create_user, get_user
from app.services.auth_service import (
AuthError,
get_or_create_user,
get_user,
has_acknowledged_current,
record_acknowledgement,
)
from app.services import otp_service, referral_service
from app.services.email_service import EmailSendError, send_otp, send_welcome_email
from app.services.locales import ACTIVE_PUBLIC_LANGS, detect_public_lang, get_locale
from app.templates_env import templates
_LANG_COOKIE = "rtm.lang"
def _resolve_login_lang(request: Request, explicit: str | None) -> str:
"""Resolve the language to render /login in. An explicit ?lang= wins
(used by the on-page EN/IT pill); otherwise the same detection
landing.py uses cookie Accept-Language cf-ipcountry en."""
if explicit and explicit in ACTIVE_PUBLIC_LANGS:
return explicit
return detect_public_lang(
cookie_lang=request.cookies.get(_LANG_COOKIE),
accept_language=request.headers.get("accept-language"),
cf_country=request.headers.get("cf-ipcountry"),
user_lang=None,
)
log = get_logger("auth_router")
router = APIRouter(tags=["auth"])
@ -111,6 +136,7 @@ async def login_page(
next: str | None = None,
error: str | None = None,
ref: str | None = None,
lang: str | None = None,
session: AsyncSession = Depends(get_session),
):
# If a valid referral code is supplied, surface a small "invited"
@ -121,15 +147,27 @@ async def login_page(
await referral_service.lookup_referrer(session, ref_norm)
if ref_norm else None
)
return templates.TemplateResponse(
resolved_lang = _resolve_login_lang(request, lang)
response = templates.TemplateResponse(
request, "login.html",
{
"next_path": _safe_next(next),
"error": error,
"ref": ref_norm if referrer else None,
"referrer_present": referrer is not None,
"lang": resolved_lang,
"t": get_locale(resolved_lang),
"ack_version": ACKNOWLEDGEMENT_VERSION,
},
)
# Stamp the language cookie if the user used the on-page pill so
# subsequent visits land on the same translation without re-detecting.
if lang and lang in ACTIVE_PUBLIC_LANGS:
response.set_cookie(
_LANG_COOKIE, lang,
max_age=60 * 60 * 24 * 365, samesite="lax", httponly=False,
)
return response
@router.post("/login")
@ -138,6 +176,9 @@ async def login_submit(
email: str = Form(...),
next: str | None = Form(default=None),
ref: str | None = Form(default=None),
lang: str | None = Form(default=None),
acknowledged: str | None = Form(default=None),
ack_version: int | None = Form(default=None),
session: AsyncSession = Depends(get_session),
):
s = get_settings()
@ -150,6 +191,27 @@ async def login_submit(
if ref_norm else None
)
# Resolve language from the form (what the user actually saw) or
# fall back to detection. This is the language we'll record on the
# acknowledgement row if one gets written.
resolved_lang = _resolve_login_lang(request, lang)
t = get_locale(resolved_lang)
# Affirmative-acknowledgement check. Browsers only POST the checkbox
# name when ticked, so any truthy value here means accepted.
if not acknowledged:
return templates.TemplateResponse(
request, "login.html",
{"next_path": _safe_next(next),
"error": str(t.auth.ack.error_required),
"email": email,
"ref": ref_norm if referrer else None,
"referrer_present": referrer is not None,
"lang": resolved_lang, "t": t,
"ack_version": ACKNOWLEDGEMENT_VERSION},
status_code=400,
)
# Track whether THIS request creates the user row (i.e. a referral
# capture window). Cleanest way: probe for existence first.
from app.services.auth_service import get_user_by_email
@ -164,7 +226,9 @@ async def login_submit(
request, "login.html",
{"next_path": _safe_next(next), "error": str(e), "email": email,
"ref": ref_norm if referrer else None,
"referrer_present": referrer is not None},
"referrer_present": referrer is not None,
"lang": resolved_lang, "t": t,
"ack_version": ACKNOWLEDGEMENT_VERSION},
status_code=400,
)
@ -175,6 +239,15 @@ async def login_submit(
if was_new and referrer is not None:
await referral_service.link_new_user(session, user, referrer)
# Record the acknowledgement at the current version, idempotently.
# If the user has already accepted this version we skip — every
# sign-in needn't write a row, only every version bump does.
if not await has_acknowledged_current(session, user):
await record_acknowledgement(session, user, resolved_lang)
log.info("auth.acknowledgement_recorded",
user_id=user.id, version=ACKNOWLEDGEMENT_VERSION,
lang=resolved_lang)
# Issue OTP only if cooldown allows; if a fresh one was sent in the
# last 60s we just reuse the existing one (silently) to avoid
# spamming the user's inbox on a refreshed form submit.

View file

@ -17,7 +17,8 @@ from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.db import utcnow
from app.models import User
from app.legal import ACKNOWLEDGEMENT_VERSION
from app.models import User, UserAcknowledgement
class AuthError(Exception):
@ -69,3 +70,35 @@ async def get_or_create_user(
await session.commit()
await session.refresh(user)
return user
async def has_acknowledged_current(
session: AsyncSession, user: User,
) -> bool:
"""True iff ``user`` has a ``user_acknowledgements`` row at the
currently-canonical ``ACKNOWLEDGEMENT_VERSION``. Composite-index hit
on (user_id, version); fast enough to run on every sign-in."""
row = (await session.execute(
select(UserAcknowledgement.id)
.where(UserAcknowledgement.user_id == user.id)
.where(UserAcknowledgement.version == ACKNOWLEDGEMENT_VERSION)
.limit(1)
)).scalar_one_or_none()
return row is not None
async def record_acknowledgement(
session: AsyncSession, user: User, lang: str,
) -> None:
"""Insert one ``user_acknowledgements`` row at the current version,
recording the language the user actually saw. The caller is
expected to gate on ``has_acknowledged_current`` first this helper
does not de-duplicate so the audit trail can carry repeated
acceptances if a future caller wants them."""
session.add(UserAcknowledgement(
user_id=user.id,
version=ACKNOWLEDGEMENT_VERSION,
lang=lang,
accepted_at=utcnow(),
))
await session.commit()

View file

@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="en">
<html lang="{{ lang or 'en' }}">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
@ -17,40 +17,110 @@
<body>
<div class="auth-shell">
<div class="auth-card">
<div class="auth-card__brand">{{ BRAND_NAME }}</div>
<div class="auth-card__hint">sign in with email</div>
<div class="auth-card__brand" style="display:flex; justify-content:space-between; align-items:center;">
<span>{{ BRAND_NAME }}</span>
<span class="auth-card__lang-switch" role="group"
aria-label="{{ (t and t.auth.ack.lang_switch_aria) or 'Language' }}"
style="font-size:11px; letter-spacing:0.04em;">
<a href="/login?lang=en{% if next_path and next_path != '/' %}&next={{ next_path }}{% endif %}{% if ref %}&ref={{ ref }}{% endif %}"
style="text-decoration:none; opacity:{{ '1' if lang == 'en' else '0.55' }};">EN</a>
<span style="opacity:0.4;">·</span>
<a href="/login?lang=it{% if next_path and next_path != '/' %}&next={{ next_path }}{% endif %}{% if ref %}&ref={{ ref }}{% endif %}"
style="text-decoration:none; opacity:{{ '1' if lang == 'it' else '0.55' }};">IT</a>
</span>
</div>
<div class="auth-card__hint">
{% if lang == 'it' %}accedi via email{% else %}sign in with email{% endif %}
</div>
{% if referrer_present %}
<div class="auth-info auth-info--invited">
<strong>You've been invited.</strong>
<strong>{% if lang == 'it' %}Sei stato invitato.{% else %}You've been invited.{% endif %}</strong>
{% if lang == 'it' %}
Quando ti abboni, tu e il tuo amico ricevete entrambi
<strong>50% di sconto per 3 mesi</strong>. Iscriviti qui sotto per attivarlo.
{% else %}
When you subscribe, you and your friend both get
<strong>50% off for 3 months</strong>. Sign up below to lock it in.
{% endif %}
</div>
{% endif %}
<p class="auth-card__lede">
{% if lang == 'it' %}
Inserisci la tua email e ti invieremo un codice di 6 cifre. Niente password.
I nuovi visitatori creano un account; chi torna fa l'accesso.
{% else %}
Enter your email and we'll send you a 6-digit code. No password.
First-time visitors get an account; returning visitors get a sign-in.
{% endif %}
</p>
{% if error %}<div class="auth-error">{{ error }}</div>{% endif %}
<form method="post" action="/login" autocomplete="on">
<input type="hidden" name="next" value="{{ next_path or '/' }}">
<input type="hidden" name="lang" value="{{ lang or 'en' }}">
<input type="hidden" name="ack_version" value="{{ ack_version }}">
{% if ref %}<input type="hidden" name="ref" value="{{ ref }}">{% endif %}
<label>Email
<input type="email" name="email" value="{{ email or '' }}" required autofocus>
</label>
<button type="submit">Send code</button>
{# Affirmative un-pre-ticked acknowledgement. The substance is
locale-driven (auth.ack.*); the checkbox is required client-side
AND server-side. Replaces a passive "by continuing you agree…"
paragraph because an active acceptance carries more weight. #}
{% if t %}
<div class="auth-ack" style="margin-top:18px; padding:14px 16px;
border:1px solid var(--border, #ddd); border-radius:6px;
font-size:12.5px; line-height:1.55;">
<div style="font-weight:600; margin-bottom:8px;">{{ t.auth.ack.heading }}</div>
<ul style="margin:0 0 12px 18px; padding:0;">
{% for item in t.auth.ack.items %}
<li style="margin-bottom:6px;">{{ item }}</li>
{% endfor %}
</ul>
<label style="display:flex; gap:8px; align-items:flex-start; cursor:pointer;">
<input type="checkbox" name="acknowledged" id="ack-box"
value="on" required style="margin-top:3px;">
<span>{{ t.auth.ack.checkbox_label }}</span>
</label>
</div>
{% endif %}
<button type="submit" id="ack-submit" disabled
style="margin-top:14px;">
{% if lang == 'it' %}Invia codice{% else %}Send code{% endif %}
</button>
</form>
<p class="auth-card__legal" style="margin-top:18px; font-size:11px; color: var(--muted); line-height:1.6;">
By signing in you agree to our
<a href="/terms">Terms</a> and
<a href="/privacy">Privacy notice</a>, and confirm you&rsquo;ve read
the <a href="/disclaimer">financial disclaimer</a>.
{% if lang == 'it' %}
Vedi i nostri
<a href="/terms">Termini</a>, l&rsquo;<a href="/privacy">Informativa privacy</a>
e il <a href="/disclaimer">disclaimer finanziario</a>.
{% else %}
See our
<a href="/terms">Terms</a>,
<a href="/privacy">Privacy notice</a>, and
<a href="/disclaimer">financial disclaimer</a>.
{% endif %}
</p>
</div>
</div>
<script>
// Tiny UX polish: disable the submit until the box is ticked. The
// server-side validation is what protects us — this is just so the
// user can't burn a click and immediately see an error.
(function () {
var box = document.getElementById('ack-box');
var btn = document.getElementById('ack-submit');
if (!box || !btn) return;
function sync() { btn.disabled = !box.checked; }
box.addEventListener('change', sync);
sync();
})();
</script>
</body>
</html>