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¶
- Operator calls
POST /api/magic-linkwithX-Internal-Secret. - API stores a SHA-256 hash of a random token in
magic_links(24h expiry) and emails the raw link via Resend. - User hits
GET /api/validate-magic-link?token=…. Token is marked used; a 30-minute signed cookie (credentials_auth) is set. - User opens
/form, enters name + portal username/password. - Before save, Playwright logs into the portal. Failed login → form error, nothing encrypted.
KMSEncryptServicecallsGenerateDataKey(AES-256). Username and password are AES-GCM encrypted separately. Ciphertext + wrapped DEK land incredentials. Web IAM user cannot decrypt.
A second magic-link type supports account deletion (/delete-account).
Scheduled sign-off¶
- Celery Beat runs
enqueue_all_signoffs_if_neededevery Sunday 08:30 America/Los_Angeles. is_bi_weekly_sunday()returns true only on Sundays spaced 2 weeks from anchor 2025-12-21.- Eligible users:
needs_password == falseand at least onecredentialsrow. - Per user,
signoff_user_timecarddecrypts withKMSDecryptService, drives Playwright viasign_off_for_user, writes aTimecardRun, 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) |