Skip to content

fix(auth): th api/admin honor the user JWT, not just M2M#105

Open
brentrager wants to merge 2 commits into
mainfrom
fix-admin-honor-user-jwt
Open

fix(auth): th api/admin honor the user JWT, not just M2M#105
brentrager wants to merge 2 commits into
mainfrom
fix-admin-honor-user-jwt

Conversation

@brentrager

Copy link
Copy Markdown
Contributor

Bug

require_authed() — used by every th api and th admin command — only loaded SmoothApiClient::from_disk(), whose CredentialsStore is hard-wired to the M2M file (~/.smooth/auth/smooai.json). So someone who ran th auth login (which writes smooai-user.json and is honored by th config) still got "run th api login" from th admin — even though the platform API accepts user JWTs (assertMachineTokenAuthorizedForOrg returns early for Supabase auth, and th api crm already relies on the user login).

Fix

require_authed() now prefers a valid user session (loads smooai-user.json via CredentialsStore::at, builds the client with it), falling back to the M2M session otherwise. An expired user JWT isn't Supabase-refreshed in this path — is_authenticated() is false so it falls through to M2M / the re-login prompt.

Verified

  • Builds clean (isolated CARGO_TARGET_DIR, since the shared target is currently wedged by a deleted-worktree proto path — separate issue).
  • No regression: with an expired user JWT, th admin config schemas list falls through to the (valid) M2M session and works exactly as before.
  • The user-JWT-preference path activates for a fresh th auth login (couldn't fully exercise it here — the local user session had expired).

Third small th config/auth fix in this cluster (alongside #102 merged, #104 push-emit-JSON-Schema, smooai#1826 delete-by-key).

🤖 Generated with Claude Code

`require_authed()` (used by every `th api` + `th admin` command) loaded only
`SmoothApiClient::from_disk()`, whose `CredentialsStore` is hard-wired to the M2M
file (`~/.smooth/auth/smooai.json`). So a user who ran `th auth login` (which
writes `smooai-user.json` and is honored by `th config`) still got "run
`th api login`" from `th admin` — even though the platform API accepts user JWTs
(`assertMachineTokenAuthorizedForOrg` returns early for Supabase auth).

Prefer a valid user session first (load `smooai-user.json` via `CredentialsStore::at`,
build the client with it), falling back to the M2M session otherwise. An expired
user JWT isn't Supabase-refreshed here — `is_authenticated()` is false, so it
falls through to M2M / the re-login prompt (no regression; the M2M path is
unchanged and verified working).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Jun 23, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 1b453ed

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

…acy path

The first cut of this fix hardcoded `~/.smooth/auth/smooai-user.json`, but
`th auth login` writes the session under the active profile
(`~/.config/smooth/auth/profiles/<name>/smooai-user.json`, where <name> is in
`.../auth/active` or `$SMOOAI_PROFILE`). So `th admin`/`th api` still fell through
to M2M and 401'd on config-schema writes (which the server forbids for M2M).

`try_user_session` now walks a candidate list — `$SMOOAI_USER_AUTH_FILE` → active
profile → legacy flat path — and uses the first with a valid (non-expired)
session. Verified: with M2M + the legacy file moved aside (only the active-profile
JWT present), `th admin` authenticates and succeeds.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@brentrager brentrager enabled auto-merge (squash) June 23, 2026 02:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant