Skip to content

Architecture overview

Goal

Automate biweekly timecard sign-off for enrolled users against the API Healthcare portal, without storing portal passwords in plaintext.

Runtime pieces

┌─────────────┐     magic link email      ┌──────────┐
│  Operator   │ ────────────────────────► │  User    │
└──────┬──────┘                           └────┬─────┘
       │ POST /api/magic-link                  │ open link / submit form
       ▼                                       ▼
┌─────────────────────────────────────────────────────┐
│  FastAPI (Railway web)                              │
│  auth cookies · form · validate login · KMS encrypt │
└───────────────┬─────────────────────┬───────────────┘
                │                     │
                ▼                     ▼
         ┌──────────┐          ┌────────────┐
         │ Postgres │          │ AWS KMS CMK│
         └────┬─────┘          └─────▲──────┘
              │                      │ decrypt DEK
              ▼                      │
         ┌──────────┐          ┌─────┴──────┐
         │  Redis   │◄────────►│ Celery     │
         └──────────┘  tasks   │ worker+beat│
                               └─────┬──────┘
                                     │ Playwright
                                     ▼
                               ┌────────────┐
                               │ Timecard   │
                               │ portal     │
                               └────────────┘

Enrollment

  1. Operator calls POST /api/magic-link with X-Internal-Secret.
  2. API stores a SHA-256 hash of a random token in magic_links (24h expiry) and emails the raw link via Resend.
  3. User hits GET /api/validate-magic-link?token=…. Token is marked used; a 30-minute signed cookie (credentials_auth) is set.
  4. User opens /form, enters name + portal username/password.
  5. Before save, Playwright logs into the portal. Failed login → form error, nothing encrypted.
  6. KMSEncryptService calls GenerateDataKey (AES-256). Username and password are AES-GCM encrypted separately. Ciphertext + wrapped DEK land in credentials. Web IAM user cannot decrypt.

A second magic-link type supports account deletion (/delete-account).

Scheduled sign-off

  1. Celery Beat runs enqueue_all_signoffs_if_needed every Sunday 08:30 America/Los_Angeles.
  2. is_bi_weekly_sunday() returns true only on Sundays spaced 2 weeks from anchor 2025-12-21.
  3. Eligible users: needs_password == false and at least one credentials row.
  4. Per user, signoff_user_timecard decrypts with KMSDecryptService, drives Playwright via sign_off_for_user, writes a TimecardRun, updates user counters, and emails the result (Resend). Operator alerts use Mailtrap.

Outcomes (TimecardRunStatus)

Status Meaning
login_success_signed_off Signed off this run
login_success_already_signed Portal already showed signed off
login_failed_bad_credentials Bad login; sets needs_password
login_failed_site_error Timeout / portal / network
login_failed_unknown_error Other automation failure

Trust boundaries

Actor Can
Web process (timecard-web) kms:GenerateDataKey only
Celery worker (timecard-celery) kms:Decrypt only
Postgres alone Ciphertext; not recoverable without CMK + decrypt principal
Worker env + DB dump Full credential recovery (unavoidable for browser login)
Signoff archive • Material for MkDocs