Skip to content

Conversation

@nicknisi
Copy link
Member

Summary

Eliminates manual credential entry by automatically fetching WorkOS staging credentials through OAuth 2.0 device authorization flow.

  • Credential discovery: Scans project .env files for existing WORKOS_CLIENT_ID / WORKOS_API_KEY (with user consent)
  • Device auth flow: Browser-based OAuth when no credentials found
  • Credential caching: Reuses stored credentials from ~/.workos/credentials.json when valid
  • Graceful fallback: Falls back to manual entry if device auth fails

Credential Resolution Priority

  1. CLI flags (--api-key, --client-id) → Use directly
  2. Project .env files (with consent) → Ask user first, scan if approved
  3. Stored auth (~/.workos/credentials) → Fetch from staging API
  4. Device authorization flow → Fetch from staging API
  5. Manual entry prompt → Fallback

New Modules

File Purpose
src/lib/device-auth.ts OAuth 2.0 device authorization flow (RFC 8628)
src/lib/staging-api.ts Staging credentials API client
src/lib/credential-discovery.ts Scan project for existing credentials

API Endpoint

GET https://api.workos.com/x/installer/staging-environment/credentials
Authorization: Bearer <access_token>
Scope: staging-environment:credentials:read

Add automatic credential fetching flow that:
- Discovers existing credentials from .env files (with user consent)
- Uses OAuth device authorization to authenticate users
- Fetches staging credentials from WorkOS API
- Falls back to manual entry if any step fails

New modules:
- credential-discovery.ts: env file scanning with consent
- device-auth.ts: reusable OAuth device flow
- staging-api.ts: WorkOS staging credentials API client

State machine expansion:
- gatheringCredentials now has 8 substates with priority-based
  resolution: CLI flags → env files → stored auth → device auth → manual

Closes auto-credentials feature request.
Add test coverage for:
- staging-api.ts: API response handling (camelCase/snake_case),
  HTTP error codes (401, 403, 404, 500), network errors, timeouts
- credential-discovery.ts: env file detection, credential parsing,
  validation, priority order, quote handling
- credentials.ts: staging credential cache (save/get/invalidation)

Brings test count from 191 to 228.
Remove AI-generated slop: redundant catch block comments,
unused runDeviceAuthFlow function, verbose test comments.
- Store envCredentialPath in context from DiscoveryResult.sourcePath
- Emit correct source path instead of hardcoded '.env'
- Throw DeviceAuthError on malformed JSON from auth server
Add logInfo/logError calls to credential-discovery, device-auth,
and staging-api modules for easier debugging of auth flows.

Also includes skill file updates and tanstack-start validation rules.
@nicknisi nicknisi merged commit a1e44b0 into main Jan 27, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants