#### Security

# Sign in to the CLI with a trusted issuer

A [trusted issuer](/build/agents-window/security/trusted-issuers) can sign a member in to the legacy Cursor CLI (`cursor-agent`) on a machine you run, with no browser login and no stored API key. The CLI uses the issuer's access token as its API key and acts as that member, in the team that owns the issuer.

Access tokens last an hour at most, so a long session needs a way to get the next one. This page shows three ways to supply tokens.

## Before you start

* **A team's issuer with `cli:session`.** A team owns the issuer, the issuer allows **On behalf of a member**, and that mode includes the `cli:session` scope. An organization's issuer can't mint `cli:session`. See [Set up an issuer](/build/agents-window/security/trusted-issuers#set-up-an-issuer).
* **A member of that team.** The member belongs to the team, or is an admin of the organization the team belongs to.
* **A backend that signs for the member.** It authenticates the member, then signs the assertion described in [Token exchange](/build/agents-window/security/trusted-issuers/token-exchange#sign-the-assertion). The assertion's `sub` is the member's `user_…` ID from the Admin API's [Get Team Members](/build/agents-window/account/teams/admin-api#get-team-members) endpoint, and `cli:session` is the only scope.

## What the session can do

A CLI session from a trusted issuer has the member's access, with these limits:

* **One team.** The session always runs in the team that owns the issuer. A request that names another team is refused.
* **No lasting credentials in Origin.** The session can't add SSH keys, manage commit signing keys, create or update Origin apps, or add app signing keys. Those would outlive the session, so the member does them from a regular login.

## Choose how the CLI gets tokens

| Method | How it renews | Use it when |
| :--- | :--- | :--- |
| Access token in `CURSOR_API_KEY` | It doesn't. The session ends when the token expires. | A job finishes within the token's lifetime. |
| Token command in `CURSOR_TOKEN_COMMAND` | The CLI runs your command again and exchanges a new signed assertion. | The machine can reach your backend. This is the default choice. |
| Refresh token in `CURSOR_REFRESH_TOKEN` | The CLI exchanges the refresh token with Grok Build. | The machine can't run a token command. An admin must turn refresh tokens on. |

If more than one is set, the CLI uses an API key first, then a token command, then a refresh token.

## Use an access token

Have your backend [exchange a signed assertion](/build/agents-window/security/trusted-issuers/token-exchange#token-request), then pass the access token to the CLI as its API key:

```bash
CURSOR_API_KEY="$ACCESS_TOKEN" cursor-agent -p "Summarize the open pull requests"
```

The SDK reads the same variable. The CLI doesn't renew a token passed this way. If you start it with a token that has expired, it exits with `This API key or token has expired. Create a new API key, or obtain a new token from its issuer, and try again.`

## Use a token command

A token command is a program you provide. It asks your backend for a freshly signed assertion and prints it. The CLI does the exchange, so the access token never touches disk and your signing key stays on your backend.

1. **Write the command**

   The command prints one JSON object to standard output and exits with status 0:

   ```json
   {
     "client_id": "authority_client_EXAMPLE",
     "assertion": "SIGNED_ASSERTION_JWT",
     "expires_in": 900
   }
   ```

   `expires_in` is optional. It asks for a shorter access token, in seconds.

2. **Point the CLI at it**

   Set `CURSOR_TOKEN_COMMAND` to the command and its arguments:

   ```bash
   export CURSOR_TOKEN_COMMAND="/usr/local/bin/acme-token-helper --audience cli"
   cursor-agent
   ```

   You can also set `tokenCommand` in your user-level `cli-config.json`. The CLI ignores `tokenCommand` in a project's configuration, so a repository can't choose the command.

The CLI runs the command under these rules:

* **No shell.** The CLI splits the string into a program and its arguments and runs the program directly. Pipes, redirects, and variable expansion don't work. Put that logic in a script.
* **Clean environment.** The command doesn't receive Grok Build credential variables such as `CURSOR_API_KEY`, or `CURSOR_TOKEN_COMMAND` itself.
* **Standard output only.** Standard input is closed and standard error is discarded.
* **10 seconds, 64 KiB.** The command must finish within 10 seconds and print at most 64 KiB.

The CLI keeps the access token in memory. It runs the command again shortly before the token expires: 5 minutes before, or a quarter of the token's lifetime if that's shorter. If a renewal fails while the current token is still valid, the CLI keeps using that token and tries again.

Each run must return a newly signed assertion, because Grok Build accepts each assertion once. The CLI ignores a refresh token in the exchange response.

## Use a refresh token

A refresh token lets the CLI renew with Grok Build directly. An admin turns on **Issue refresh tokens** for the issuer. After that, each member-mode exchange your backend makes returns a `refresh_token`.

Pass that refresh token to the CLI:

```bash
export CURSOR_REFRESH_TOKEN="$REFRESH_TOKEN"
cursor-agent
```

* **The CLI rotates and stores it.** Each renewal returns a new refresh token. The CLI saves it in its credential store and removes `CURSOR_REFRESH_TOKEN` from its own environment.
* **Restarts continue the chain.** If you start the CLI again with the same `CURSOR_REFRESH_TOKEN` value, it continues from the stored token. You don't need to hand it the newest one.
* **One refresh token for each machine.** Each exchange your backend makes returns its own refresh token, so a member can be signed in on several machines at once. Don't copy one refresh token to two machines. When one presents a token the other already replaced, Grok Build revokes every token the issuer minted for that member.
* **Renewed tokens carry the mode's scopes.** A renewed access token carries every scope the issuer's member mode allows for the member, not only `cli:session`.

> [!WARNING]
>
> A refresh token is a bearer credential. Anyone who holds it can get access tokens for that member until it expires or is revoked. Pass it through a secret store, and don't write it to logs, images, or shell history.

Each refresh token lasts 30 days at most, and each renewal issues a new one. To end a member's refresh tokens early, an admin selects **Revoke grants** on the issuer. Turning off **Issue refresh tokens** only pauses renewal: refresh tokens that haven't expired work again if it's turned back on. See [Refresh an access token](/build/agents-window/security/trusted-issuers/token-exchange#refresh-an-access-token) for the protocol.

## Troubleshooting

| Message | Cause | What to do |
| :--- | :--- | :--- |
| `Token command could not be started.` | The program wasn't found or can't be executed. | Use an absolute path, and check the file's permissions. |
| `Token command timed out after 10000ms.` | The command ran longer than 10 seconds. | Make the call to your backend faster, or add a timeout inside the command. |
| `Token command output is not valid JSON.` | The command printed something other than one JSON object. | Print logs to standard error, and only the JSON object to standard output. |
| `Token command field assertion must be a non-empty string.` | A required field is missing or empty. The message names the field. | Return `client_id` and `assertion`. |
| `Trusted issuer exchange failed with HTTP 400 (invalid_grant).` | Grok Build refused the signed assertion. The message carries the status and error code. | Look up the code in [Errors](/build/agents-window/security/trusted-issuers/token-exchange#errors). |
| `Trusted issuer refresh failed with HTTP 400 (invalid_grant).` | The refresh token expired, was replaced, or was revoked. | Have your backend exchange a new signed assertion, and pass the new refresh token. |
| `The trusted issuer refresh credential can no longer be used. Sign in again before continuing.` | An earlier renewal was interrupted, and Grok Build refused the stored refresh token when the CLI tried it again. | Have your backend exchange a new signed assertion, and pass the new refresh token. |
| `The rotated trusted issuer credential could not be stored. Sign in again before continuing.` | The CLI couldn't save the new refresh token. | Fix access to the CLI's credential store, then start again with a new refresh token. |

## Related

* [Trusted issuers](/build/agents-window/security/trusted-issuers)
* [Token exchange](/build/agents-window/security/trusted-issuers/token-exchange)
* [API keys and service tokens](/build/agents-window/security/api-keys)
