Files
Andrew Beveridge ☄️andClaude Opus 5.5 d570b1f139 feat(kjbox): partner API for singer email-code sign-in + show credits (v0.239.0) (#1051)
## Summary
Partner API so the kjbox singer UI (live karaoke nights) can turn
patrons into real karaoke-gen customers: the singer verifies their email
with a 6-digit code typed into the kjbox page, and kjbox then searches
audio / creates the job **as that user** (normal gen delivery emails).

All under `/api/kjbox`, gated by `X-Kjbox-Secret` (Secret Manager
`kjbox-partner-secret`, wired in ci.yml; 503 when unset):
- `POST /auth/send-code` — emails a 6-digit code (hashed at rest, 10-min
expiry, new code supersedes old). Exempt from the 2-signups-per-IP cap
(a venue shares one IP) — replaced by a partner-wide 100 new accounts /
24h cap + 5 codes / email / hour. New users tagged `signup_source=kjbox`
(+ venue).
- `POST /auth/verify-code` — 5 attempts then burned, single-use; shares
the post-login path with magic-link verify (refactored into
`complete_verified_login`: welcome credit w/ AI eval, locale, referral,
session, welcome email).
- `POST /credits/show-credit` (+ user Bearer) — +1 credit, no email
("free at Andrew's shows"), idempotent per key, 5 / user / 24h.
- Review-needed + 24h reminder emails for `X-Client-Id: kjbox*` jobs
carry a one-click sign-in link to the review page (singers never signed
in on gen's site).

Paired kjbox PR: nomadkaraoke/kjbox (singer make-it flow, v0.116.0).
Plan: kjbox `docs/archive/2026-09-24-singer-make-it-auto-flow-plan.md`.

## Testing
- New: `backend/tests/test_kjbox_partner_api.py`,
`backend/tests/emulator/test_kjbox_partner_service.py` (+ additions to
existing tests for the verify refactor, review-email links, X-Client-Id
on create-from-search)
- Backend unit suite: 4644 passed; emulator suite passed.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Added partner sign-in with one-time email codes and show-credit
claiming, with limits on code requests and credit grants to prevent
repeated or duplicate claims.
* Added localized sign-in emails and single-use review links for
eligible reminders.
  * Sign-in supports welcome-credit evaluation and referral attribution.
* **Improvements**
* Review reminders can use custom links, and new accounts can record
signup source and venue.
  * Credential-like request headers are redacted from stored metadata.
* **Documentation**
  * Added partner API usage and behavior details.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 00:03:10 -04:00

52 lines
1.9 KiB
Python

"""
Request utility helpers for extracting client information.
"""
from typing import Optional
from fastapi import Request
def get_client_ip(request: Request) -> Optional[str]:
"""
Extract real client IP address from a request.
Cloud Run and load balancers set X-Forwarded-For header with the
original client IP as the first entry. Falls back to request.client.host
for direct connections (local dev).
Returns None if no IP can be determined.
"""
forwarded = request.headers.get("x-forwarded-for", "")
if forwarded:
return forwarded.split(",")[0].strip()
if request.client:
return request.client.host
return None
# Headers never worth recording, even redacted (already captured elsewhere / noise).
_SKIPPED_CUSTOM_HEADERS = frozenset({"x-forwarded-for", "x-forwarded-proto", "x-forwarded-host"})
# Any x-* header whose name contains one of these carries a credential
# (x-kjbox-secret, x-admin-token, x-kh-forwarder-secret, x-e2e-bypass-key,
# x-api-key, x-authorization, ...). request_metadata.custom_headers is returned
# to the job owner by GET /api/jobs/{id}, so these must never be stored raw.
_SECRET_HEADER_MARKERS = ("secret", "token", "key", "auth", "password", "passwd", "cookie", "signature", "credential", "bypass", "jwt", "session")
REDACTED = "[redacted]"
def is_secret_header(name: str) -> bool:
lowered = name.lower()
return any(marker in lowered for marker in _SECRET_HEADER_MARKERS)
def collect_custom_headers(headers) -> dict:
"""All X-* request headers for job request_metadata, with secret-bearing
header values replaced by ``"[redacted]"`` (the name is kept for debugging)."""
custom = {}
for key, value in dict(headers).items():
lowered = key.lower()
if not lowered.startswith("x-") or lowered in _SKIPPED_CUSTOM_HEADERS:
continue
custom[key] = REDACTED if is_secret_header(lowered) else value
return custom