fix(cli): accept account-owned Cloudflare API tokens - #268
Merged
Merged
Conversation
Token validation only used the user-scoped /user/tokens/verify endpoint,
which returns 401 for account-owned tokens, so the installer rejected
them with a misleading 'Invalid or expired token' error (after printing
'Token Validated').
- Retry validation against the account-scoped
/accounts/{account_id}/tokens/verify endpoint on 401 when the account
ID is known
- Pass the selected account ID from the install flow
- Resolve the account ID in 'counterscale env token' via wrangler
whoami so account tokens work there too (falls back to user-token
validation if lookup fails)
- Stop the validation spinner with the correct message before throwing
- Link the account API tokens page with the real account ID
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #268 +/- ##
==========================================
+ Coverage 82.68% 84.74% +2.06%
==========================================
Files 56 56
Lines 2010 2026 +16
Branches 539 544 +5
==========================================
+ Hits 1662 1717 +55
+ Misses 310 282 -28
+ Partials 38 27 -11 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Adds an install() flow test exercising the missing-CF_BEARER_TOKEN path, which covers the account API tokens note (including the account-scoped dashboard URL) and the promptApiToken(accountId) call flagged by Codecov patch coverage.
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.
Overview
Fixes #246.
The installer (and
counterscale env token) only accepted User API tokens because validation used the user-scopedGET /user/tokens/verifyendpoint, which returns 401 for account-owned tokens created from an account's "Manage Account → Account API Tokens" page. This PR retries validation against the account-scopedGET /accounts/{account_id}/tokens/verifyendpoint when the user endpoint returns 401.It also fixes the confusing output shown in the issue — the spinner printed "Token Validated" before checking the validation result, so users saw "Token Validated" immediately followed by "Error: Invalid or expired token".
Changes by Package
@counterscale/cli
cloudflare.ts: ExtractedverifyTokenAtUrl();validateToken(token, accountId?)now first tries/user/tokens/verifyand, on 401 with a known account ID, retries against/accounts/{account_id}/tokens/verify(the documented endpoint for account-owned tokens, identical response shape). Non-401 failures (403 = valid token lacking permissions, network errors) do not trigger the retry.ui.ts:promptApiToken(accountId?)passes the account ID through, and stops the validation spinner with the appropriate message ("Token Validated" / "Token validation failed") before throwing, fixing the misleading sequence from the issue.install.ts: Passes the selected account ID topromptApiTokenand prints a direct link to the account's API tokens page using the real account ID instead of a<account>placeholder.env.ts:counterscale env tokennow resolves the account ID viawrangler whoamibefore prompting, so account-owned tokens work there too. If the lookup fails or returns nothing, it gracefully degrades to the previous user-token-only validation.installandenvflows, spinner messaging, andenvfallback behavior.@counterscale/server
No changes.
@counterscale/tracker
No changes.
Other Changes
No changes.
Additional Notes
envflow comes fromwrangler whoami, which returns the first account — multi-account users whose token belongs to a different account will still see validation fail there, same as before this change.@counterscale/clitest suite passes (117 tests),tsc --noEmitclean, lint 0 errors.