# Device flow login on headless machines: signing a coding agent's machine in as a person

> An agent's cloud machine has no browser, so a CLI there cannot open a sign-in page. The OAuth device authorization grant (RFC 8628) solves that: a short code you approve on your phone or laptop. How it works, the one phishing risk to teach your team, and how to use sandscale login --device on each agent's machine.

An agent's cloud machine has no browser, so a CLI there cannot open a sign-in page. The OAuth device authorization grant (RFC 8628) solves that: a short code you approve on your phone or laptop. How it works, the one phishing risk to teach your team, and how to use sandscale login --device on each agent's machine.

December 2, 2026 · 5 min read · The Sandscale team

Give every coding agent its own cloud machine and you meet a small, recurring problem: tools on that machine need to sign in, and there is no browser there. Claude Code, Codex, `gh`, your cloud CLI and the Sandscale CLI all want to know who they are acting for. The tempting fixes are to paste an API key into the machine’s environment or to copy a credentials file from your laptop. Both work, and both leave a long-lived secret in a place that is hard to account for.

The better fix for a machine that acts for a person is the OAuth 2.0 device authorization grant, defined in [RFC 8628](https://www.rfc-editor.org/rfc/rfc8628). Many CLIs call it “device login” or “sign in with a code”. This post explains how it works, what to teach your team about it, and how to use it with the Sandscale CLI (1.4.1) on an agent’s machine.

## How the device flow works

There are two devices: the one that needs a token (the agent’s machine, with no browser) and the one the person uses to approve (a phone or laptop, with a browser). The flow is four steps.

1. **The CLI asks for a code.** It sends its client ID to the authorization server’s device authorization endpoint. The answer has a `device_code` (secret, kept by the CLI), a short `user_code` (for the person), a `verification_uri` to visit, sometimes a `verification_uri_complete` with the code already filled in, an `expires_in`, and an `interval`.
2. **The CLI shows the person the code and the address**, and starts waiting.
3. **The person approves.** On their own device they open the address, sign in normally (with their passkey, SSO or second factor), enter the code, and approve.
4. **The CLI polls for the token.** Every `interval` seconds it asks the token endpoint, with grant type `urn:ietf:params:oauth:grant-type:device_code` and the device code. Until approval it gets `authorization_pending`. If it polls too fast it gets `slow_down` and must wait five seconds longer between requests. It stops on `access_denied` or `expired_token`. On approval it gets an access token and usually a refresh token.

The important property: the person’s password, passkey and second factor never touch the headless machine. The machine only ever holds the tokens it was issued, for the client it identified as, and those can be revoked.

## The one risk to teach

RFC 8628 is candid about its main weakness, in its security considerations: remote phishing. An attacker starts a device sign-in on their own machine, then sends the code to a victim with a believable message (“enter this code to keep your access”). If the victim approves, the attacker’s machine gets the victim’s tokens.

The defence is a habit: **only enter a code you started yourself, just now, on a machine you know.** Two things help people keep it:

- The approval page should name the app asking, so a person can tell what they are about to approve.
- The CLI should say the same thing out loud.

The Sandscale CLI does the second. When you run `sandscale login --device`, it prints the address to open, the code to enter (and an address with the code filled in when the server gives one), tells you to enter the code only if you started this sign-in, says roughly how long the code stays valid, and waits for approval until you press Ctrl+C. Before showing the address, it checks that the verification address is on the sign-in server’s own domain, and refuses it otherwise.

## Device flow with the Sandscale CLI

The CLI signs in at `auth.sandscale.dev`. Its help describes two modes:

- `sandscale login` opens a browser on this computer, using the authorization code flow with PKCE.
- `sandscale login --device` gives you a code to approve on any device.

You often do not need the flag. The CLI’s help says the code is used by itself in a Sandscale workspace, over SSH, on Linux without a display, and when no browser opens, because a browser sign-in can only come back to the computer it started on. The sign-in server’s public metadata lists the device code grant alongside the authorization code and refresh token grants, so this is the supported path, not a workaround.

Where the tokens go matters on a shared or headless machine. The CLI keeps them in the OS credential store under “Sandscale CLI”. On a machine without one (a server, an SSH session, CI), it keeps them in `~/.config/sandscale/credentials.json`, readable only by your user, and tells you it did.

To check who a machine is signed in as:

```sh
sandscale whoami
```

And when the machine’s work is done:

```sh
sandscale logout
```

`logout` revokes the session at the server and deletes the local tokens, so a machine you forget about does not keep a working sign-in.

## A team practice for agent machines

Put together, a simple policy for the machines your agents run on:

1. **One person per machine.** The machine runs one person’s agent and is signed in as that person, with the device flow. The [Sandscale sandboxes](https://sandscale.dev/sandboxes) and other machines the agent starts from there are then started as that person, under their permissions and plan limits, and the audit log says who.
2. **Never copy a credentials file between machines.** Sign each machine in on its own. A copied token is invisible to the person who owns it.
3. **Sign out when the agent is finished.** `sandscale logout` as the last step of the agent’s task, or when the machine is stopped for good.
4. **Keep API keys for jobs with no person behind them.** A nightly job that runs whether or not anyone is at work is a reasonable place for a scoped API key. An agent someone started this morning is not.
5. **Teach the phishing rule** once, at onboarding: a sign-in code you did not just ask for is a reason to stop and ask.

The same pattern applies to the other tools on that machine. Most CLIs that offer a device flow describe it in their own help; use it, one machine and one person at a time, rather than moving secrets around.

## Related

- Reviewing what an agent built, from your laptop: [port forwarding a dev server from a workspace](https://sandscale.dev/blog/port-forwarding-a-dev-server-from-a-workspace).
- Making the CLI’s results usable from scripts and agents: [scripting the Sandscale CLI with exit codes and JSON](https://sandscale.dev/blog/scripting-the-sandscale-cli-exit-codes-and-json).

**On this page**

1. How the device flow works
2. The one risk to teach
3. Device flow with the Sandscale CLI
4. A team practice for agent machines
5. Related

**Run your agents in a box of their own**

Each sandbox is a Firecracker microVM. Free includes 100 awake vCPU-hours a month.

[Start free](https://cloud.sandscale.dev/auth/signup)

## Keep reading

- [December 2, 2026 · **Auditing the MCP tool calls your team's coding agents make** · When Claude Code, Codex, Cursor or Gemini CLI call MCP tools, the record of what happened can live in three places: the client, the MCP server, and the system behind it. What each layer can see, which one to trust as the record, whether to keep arguments, and how to read Sandscale's audit log for agent calls from the CLI.](https://sandscale.dev/blog/auditing-mcp-tool-calls)
- [December 2, 2026 · **Gemini CLI and MCP: a settings.json that gives the agent its own sandbox, for a team** · Gemini CLI reads MCP servers from settings.json. How a team adds Sandscale there so the agent's commands run on its own sandbox: the remote server for people at a laptop, the local stdio server on a headless agent machine, and the Gemini settings that decide what runs without asking.](https://sandscale.dev/blog/gemini-cli-mcp-config-remote-sandbox)

---
Source: https://sandscale.dev/blog/device-flow-login-on-headless-machines
