Device flow login on headless machines: signing a coding agent's machine in as a person
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 al
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. 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.
-
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 shortuser_code(for the person), averification_urito visit, sometimes averification_uri_completewith the code already filled in, anexpires_in, and aninterval. - The CLI shows the person the code and the address, and starts waiting.
- 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.
-
The CLI polls for the token. Every
intervalseconds it asks the token endpoint, with grant typeurn:ietf:params:oauth:grant-type:device_codeand the device code. Until approval it getsauthorization_pending. If it polls too fast it getsslow_downand must wait five seconds longer between requests. It stops onaccess_deniedorexpired_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 loginopens a browser on this computer, using the authorization code flow with PKCE. -
sandscale login --devicegives 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:
sandscale whoami
And when the machine's work is done:
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:
- 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 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.
- 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.
-
Sign out when the agent is finished.
sandscale logoutas the last step of the agent's task, or when the machine is stopped for good. - 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.
- 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.
- Making the CLI's results usable from scripts and agents: scripting the Sandscale CLI with exit codes and JSON.
Originally published on the Sandscale blog.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.