fix(auth): th api/admin honor the user JWT, not just M2M#105
Open
brentrager wants to merge 2 commits into
Open
fix(auth): th api/admin honor the user JWT, not just M2M#105brentrager wants to merge 2 commits into
brentrager wants to merge 2 commits into
Conversation
`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>
|
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
require_authed()— used by everyth apiandth admincommand — only loadedSmoothApiClient::from_disk(), whoseCredentialsStoreis hard-wired to the M2M file (~/.smooth/auth/smooai.json). So someone who ranth auth login(which writessmooai-user.jsonand is honored byth config) still got "runth api login" fromth admin— even though the platform API accepts user JWTs (assertMachineTokenAuthorizedForOrgreturns early for Supabase auth, andth api crmalready relies on the user login).Fix
require_authed()now prefers a valid user session (loadssmooai-user.jsonviaCredentialsStore::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
CARGO_TARGET_DIR, since the shared target is currently wedged by a deleted-worktree proto path — separate issue).th admin config schemas listfalls through to the (valid) M2M session and works exactly as before.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