Skip to content

Add a CLI token for requests the CLI makes on its own behalf - #89

Closed
zmofei wants to merge 6 commits into
fix/help-groups-navigationfrom
cli-token
Closed

zmofei wants to merge 6 commits into
fix/help-groups-navigationfrom
cli-token

Conversation

@zmofei

@zmofei zmofei commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

Adds cli_token::send, which sends a request the CLI makes for itself rather than for a user's command. The first caller will be telemetry delivery to Mapbox Events (#46).

The user's own token always comes first; the CLI's token is only the fallback for someone with none. In order: --token or MAPBOX_ACCESS_TOKEN (ranked as commands rank them, via cli_token::user_token), then the stored login, loaded as commands load it with refresh, then MAPBOX_CLI_TOKEN at run time, then a pk. token compiled in from MAPBOX_CLI_BUNDLED_TOKEN at build time. A 401 moves on to the next one. send may block on the credentials lock and refresh the login, so it is meant for requests nobody is waiting on, such as a detached child. It checks read-only that a login exists first, so a machine that never logged in does not get a ~/.mapbox.

The built-in token can be extracted from the binary, so build.rs fails the build unless it is a pk. token. A plain cargo build has none.

The CLI's token is billed to the account that owns it, so it is only sent to the APIs in CLI_TOKEN_APIS (host, path prefix and reason; today just telemetry). A command whose API is listed falls back to it when the user has no token of their own, with a note on stderr; one whose token is rejected, or run with --use-login, does not.

Testing: unit tests cover the order, the fallback and the whitelist. Reverted ten of the guards one at a time and watched their tests fail. Checked by hand that an sk. bundled token fails the build. cargo build, fmt, clippy -D warnings and cargo test pass. Not tested: against the real API, send's login step end to end, MAPBOX_ACCESS_TOKEN read from the environment, or a release build carrying a real token.

No caller yet. A background request will use it like this, with the first being telemetry:

cli_token::send(cli_token::user_token(&matches).as_deref(), profile, || client.post(url).json(&body))

Normal commands keep resolving their own token as before. Nothing calls this yet, so no user-visible change and no changelog entry.

@zmofei zmofei added the locationai-team-skills Opened via the location-ai PR creation skill label Oct 9, 2026
@zmofei
zmofei added this pull request to stack #90 October 9, 2026 11:34
Requests the CLI sends for itself, starting with telemetry delivery, must not borrow the token behind a user's command. Resolve a separate one: the MAPBOX_CLI_TOKEN override, then the stored login if it is not about to expire (never refreshed, so a background request cannot spend the refresh token), then a token bundled at build time. Nothing calls it yet.
@zmofei
zmofei removed this pull request from stack #90 October 9, 2026 11:50
@zmofei
zmofei changed the base branch from main to fix/help-groups-navigation October 9, 2026 11:50
@zmofei
zmofei added this pull request to stack #92 October 9, 2026 11:50
zmofei added 5 commits October 9, 2026 14:58
The stored login now wins over MAPBOX_CLI_TOKEN, which applies only when
the user is not logged in. The bundled token comes from its own build-time
variable, MAPBOX_CLI_BUNDLED_TOKEN, so a token exported in a dev shell is
not compiled into local builds, and build.rs refuses anything but pk.
cli_token::send attaches the token itself and moves to the next one on a
401: the default public token kept with the login, then MAPBOX_CLI_TOKEN,
then the bundled token. The OAuth login token is never sent as the
request's token; it is only read, never refreshed, to fetch the default
token when it is missing or was rotated. The save never waits on the
credentials lock and never writes into a profile that has logged out or
switched account.
The CLI's token is only the fallback for someone with none of their own.
--token and MAPBOX_ACCESS_TOKEN now come first, ranked as a command ranks
them, and the login's OAuth token is used when the default public token
cannot be had.
Every request made with the CLI's token is billed to the account that
owns it, so CLI_TOKEN_APIS lists where it may go, by host and path
prefix, each with its reason. send() skips the CLI's token for anything
else, and a command whose API is listed now falls back to it when the
user has no token of their own. The list holds only telemetry today, so
no command changes.
The login is loaded with load_fresh_credentials, refresh included, so it
never goes stale and there is nothing extra to store, fetch or replace.
It is looked for read-only first, so a machine that never logged in does
not get a ~/.mapbox from the credentials lock.
@zmofei

zmofei commented Oct 9, 2026

Copy link
Copy Markdown
Member Author

Merged into #46, which now carries this together with its first caller.

@zmofei zmofei closed this Oct 9, 2026
@zmofei
zmofei removed this pull request from stack #92 October 9, 2026 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

locationai-team-skills Opened via the location-ai PR creation skill

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant