user-secure-engine — Identity, OTP, and Firebase Token Exchange

By Engineering Team•October 10, 2026•6 min read

Responsibility

user-secure-engine is the sole owner of shopper identity, credential verification, and consent — nothing in this monorepo decides a shopper’s verified phone/email, Firebase identity, or policy-acceptance state except this service.

  • Firebase ID token verification + App Check — backs the shared gateway’s /_auth_verify subrequest for every protected route monorepo-wide, and a separate App-Check-only /_app_check_verify path for the four unauthenticated phone-OTP routes.
  • Custom 6-digit OTP (not Firebase’s native email-link/verifyPhoneNumber flows) for signup verification, password reset, phone-first login/signup, and the Settings feature’s decoupled phone/email change (OtpPurpose::ProfileUpdate).
  • Core identity row (user table, Postgres) — profile fields (name, DOB, gender, avatar URL), verification status, consent status.
  • Terms/Privacy consent — one-shot acceptance recorded against the policy version in effect at acceptance time.
  • Customer avatar Pub/Sub consumer — commits the processed-avatar storage path from content-processor’s shared image pipeline, distinguished by transaction_name == "customer_avatar".

Tech Stack

Concern Choice
Language Rust
HTTP Axum
gRPC tonic server (UserService.ValidateAndInitUser) — second, independent server in the same binary
Data stores Dual-database: PostgreSQL (SeaORM, schema "user-secure-engine") for user; MongoDB for otp and otp_verify_audit
Shared logic rust-lang-core-lib — error types, logging macros, #[tracing::instrument], telemetry
gRPC client pii-service (PiiClient), for entity_type="user_personal" account-profile attributes

token_exchange_audit was removed entirely as a persisted collection/table — token-verification audit is now log_info!/log_warn! lines only, captured by the monitoring pipeline rather than a DB write.

Ports

Gateway HTTP/REST gRPC Service Name
8000 (Nginx /user-secure/*, auth_request /_auth_verify on every route except the 4 pre-auth phone-OTP exact-match paths, which use /_app_check_verify instead) 8087 (host) / 8080 (container) 50058 (host) / 50054 (container) user-secure-engine

Nginx never routes to the gRPC port — firebase-function and other internal callers reach it directly on the Docker network.

API Endpoints

Method Path Auth Purpose
POST /admin/api/v1/token/verify BearerTokenClaims + App Check Firebase ID token + App Check verification for the gateway’s /_auth_verify
GET /internal/app-check/verify App Check only Internal-only, backs /_app_check_verify for the 4 pre-auth phone-OTP routes
POST /admin/api/v1/otp/send / /verify / /resend BearerTokenClaims OTP send/verify/resend for signup verification, password reset, and profile phone/email change
GET / PATCH /admin/api/v1/user/profile BearerTokenClaims Read/update full_name/dob/gender only — phone/email are structurally absent from this request type
GET / PATCH /admin/api/v1/user/consent BearerTokenClaims Read/update Terms & Privacy acceptance
POST /auth/otp/login/send / /verify None (unauthenticated) Phone-OTP-first login — 404 on send if no account exists; verify mints a custom token with no Firebase writes
POST /auth/otp/signup/send / /verify None (unauthenticated) Phone-OTP-first signup — 409 on send if an account already exists; verify creates the Firebase user, stamps consent, mints a custom token

This service’s own CLAUDE.md API table previously omitted the /user/profile and /user/consent routes above — see TD-00020, TD-07.

Data

Store Purpose
user (Postgres, schema "user-secure-engine") Core identity row — unique (tenant_id, user_id), incl. avatar_url.
otp_codes (MongoDB) Keyed by (tenant_id, channel, contact, purpose); HMAC-SHA256-hashed code binding full request context, TTL-indexed expires_at, rate-limit fields.
otp_verify_audit (MongoDB) One row per verify attempt (success or failure), TTL-indexed; send/resend are not audited (rate limit already bounds volume).

Profile PII fields beyond the core row (entity_type="user_personal") are delegated to pii-service, the same shared encrypted-attribute store user-preference-service uses for address bodies under a different entity_type — see pii-service.

Real Consumers

Consumer Transport Usage
ui-application (Flutter) REST Profile screen, Terms/Privacy consent screens, decoupled OTP-commit phone/email change sheet, phone-OTP login/signup
firebase-function / internal callers gRPC ValidateAndInitUser (post-signup init saga)
nginx-api-router REST (internal) /_auth_verify and /_app_check_verify subrequests, gating every other gateway-protected service
content-processor Pub/Sub Publishes the avatar-processed/-removed events this service’s message_receiver consumes

Implementation Notes

  • Phone/email change is intentionally not part of the profile PATCH’s type (ProfileUpdateRequest carries only full_name/dob/gender) — it’s a decoupled OTP-commit flow (OtpPurpose::ProfileUpdate) that commits the new value immediately on successful verification, with no client-held verification-ticket state (the former service/verification_ticket/ was deleted). A phone change additionally forces re-login and syncs Firebase Auth (FirebaseServiceTrait::update_phone_number), since phone is the Firebase sign-in identity; an email change does neither. See ADR-00040 and SR-026 FR-03.
  • This service is the token verifier, so it defines its own BearerTokenClaims extractor instead of consuming rust-lang-core-lib’s standard HeaderAuthClaims — it reads the raw Authorization: Bearer <firebase_token> directly rather than trusting headers synthesized by an upstream hop.
  • OTP plaintext codes are log_debug!-only, never log_info! — the code stands in for a real email/SMS provider (none exists yet) and must stay structurally absent from production (LOG_LEVEL=info) logs.
  • No cross-engine transaction spans both databases — init_user’s Postgres insert and pii-service’s gRPC-backed write remain a saga with manual compensation, not a distributed transaction.
  • Customer avatar events share the same product-image-processed/product-image-removed topics as product images and tenant logos, distinguished only by transaction_name == "customer_avatar" — tenant_id/user_id for this consumer come from the envelope payload itself, since there is no HTTP request to extract auth from in a Pub/Sub handler.

Architecture overview (settings): Architecture — Customer Settings: Component Topology and Data Flow. Architecture overview (login/signup): Architecture — Mobile Login & Signup: Component Topology and Data Flow, ADR-00041. Nested service docs: this service’s own service/user/CLAUDE.md has drifted stale on several points, see TD-00021, TD-22.