By NHI Mgmt Group Editorial TeamBased on WorkOS: “How to add auth to your Go CLI using WorkOS” (July 30, 2025)

TL;DR: A Go CLI login pattern built on the OAuth 2.0 Device Authorization Grant lets the tool request a device code, have the user approve in a browser, and poll for tokens instead of collecting pasted credentials, according to WorkOS. The pattern reduces token handling friction, but it also shifts identity trust, polling discipline, and lifecycle control into the CLI runtime.


At a glance

What this is: This tutorial explains how to add OAuth 2.0 device flow login to a Go CLI so the tool requests a device code, the user authenticates in a browser, and the CLI later exchanges the code for tokens.

Why it matters: It matters because CLI authentication changes where identity trust is established and where token handling, polling, and expiry must be governed, which affects human IAM, secrets handling, and NHI-adjacent runtime controls.


Context

A Go CLI that authenticates through the browser shifts the trust decision out of the terminal and into an external identity flow. The key governance question is no longer only whether the user can sign in, but how the CLI handles device codes, polling, token exchange, and expiry without creating new exposure.

The OAuth 2.0 Device Authorization Grant exists for cases where embedding a browser is impractical, which makes it relevant to terminal tools, headless utilities, and developer workflows. For IAM teams, the important issue is not the code sample itself but the control boundary: user authentication happens elsewhere while the CLI becomes the runtime that requests, waits, and receives credentials.


Key questions

Q: What should teams do first when adding OAuth device flow to a CLI?

A: Start by separating what the user sees from what the client needs internally. The terminal should show the user code and verification URL only, while the device code stays private for polling and token exchange. That reduces accidental leakage and makes the approval step easier to govern across support, logging, and debugging workflows.

Q: Why does OAuth device flow still need strict token handling in a CLI?

A: Because the user-facing browser step is only the beginning of the trust chain. Once the CLI receives access and refresh tokens, those credentials can be exposed through logs, shell history, memory dumps, or local persistence. The login method changes how authentication happens, but it does not remove the need for careful token stewardship.

Q: What breaks when a CLI ignores the server-provided polling interval?

A: The login flow becomes noisy, brittle, and more likely to hit throttling or timeout conditions. Polling too aggressively can also create a poor user experience and unnecessary load on the authorization endpoint. In practice, the interval is part of the protocol, not an optional UI setting, so clients should respect it exactly.

Q: How should IAM teams govern browser-based login for non-browser tools?

A: Treat the CLI as a registered public client with an owner, an expiry model, and a revocation path. The browser handles user authentication, but the tool still participates in the identity lifecycle through client IDs, device codes, and tokens. Governance should cover who can ship the client, how it is reviewed, and how it is retired.


Technical breakdown

How the device authorization grant works in a CLI

The device flow splits authentication into two channels. The CLI requests a device code and a user code, while the user completes authentication in a browser using the verification URL. The terminal never collects the password or primary factor; it only carries the device code long enough to poll for the authorization result. That design is useful for constrained interfaces, but it also means the CLI becomes the trust-bearing runtime for the session handshake. The security model depends on keeping the device code internal, exposing only the user code, and limiting how long the code can be replayed.

Practical implication: treat the device code as a sensitive transient secret and keep only the user-facing code visible.

Polling discipline and token exchange

After the browser approval step starts, the CLI repeatedly checks the token endpoint until it receives success or an error such as authorization_pending, slow_down, access_denied, or expired_token. This is not just a convenience loop. Polling cadence becomes part of the protocol contract, and bad polling can look abusive, trigger throttling, or create indefinite waits. The final exchange returns access and refresh tokens, which are far more valuable than the interim device code. The operational risk therefore shifts from credential entry to token stewardship inside the client runtime.

Practical implication: enforce timeout, backoff, and token storage controls in the CLI instead of treating polling as a UI detail.

Why device flow changes identity governance for CLIs

Device flow is an OAuth pattern for user authentication, but in practice it also defines how a non-browser client participates in identity governance. The CLI is not an autonomous actor in the strict sense, yet it does hold transient authorization artifacts and can expose them if logs, prompts, or process memory are mishandled. That makes lifecycle questions important: who owns the app registration, how client IDs are managed, and what happens when a terminal session is abandoned. The control problem is not login UX alone; it is governing a short-lived authentication state inside software that often runs outside central management.

Practical implication: align CLI registration, secret handling, and session expiry with the same governance standards you apply to other non-browser clients.


NHI Mgmt Group analysis

Device flow moves authentication risk from credential entry to runtime governance. The terminal no longer asks the user to paste secrets, but it still becomes the place where authorization state is requested, polled, and accepted. That creates a narrower and cleaner user experience, yet it also means the security boundary now depends on client behavior, not just browser-side authentication. The implication is that CLI authentication must be governed as a runtime identity interaction, not as a simple input prompt.

Polling cadence is an identity control, not just an implementation detail. The article's interval and slow_down handling show that the client is participating in a protocol with explicit timing expectations. When teams ignore that discipline, they create noise, brittle login behavior, and avoidable failure states that look operational rather than security-related. Practitioners should treat polling as part of the access path and review it with the same rigor as any token acquisition flow.

Short-lived device codes reduce exposure, but token stewardship remains the real control plane. The browser approval step limits manual credential handling, yet the CLI still ends with access and refresh tokens that must be protected in memory, logs, and storage. That makes the decisive governance issue post-authentication handling, not the initial login gesture. Teams should evaluate whether the tool's local execution context is trustworthy enough to hold those tokens at all.

CLI auth should be governed like any other non-browser identity integration. The named concept here is terminal-bound identity handoff: a user authenticates in one channel, but a separate client runtime receives and manages the resulting tokens. That handoff is precisely where governance gaps emerge, because ownership, expiry, and revocation can become blurred once the user leaves the browser and returns to the terminal. Practitioners need to map the lifecycle of that handoff explicitly.

OAuth device flow is compatible with identity security, but not self-governing. The flow solves the browser constraint, not the governance problem. Without clear controls around app registration, prompt handling, and token storage, a cleaner login path can still create the same downstream exposure as any other delegated credential path. The right question is whether the CLI runtime has been treated as a trusted identity participant.

What this signals

The device flow pattern is useful because it removes password handling from the terminal, but it also shifts the decisive control point to token issuance and local client governance. Teams should review whether their CLI tools are being treated as identity participants with defined ownership, expiry, and revocation, rather than as disposable scripts.

Terminal-bound identity handoff: a user authenticates in a browser, yet a separate local runtime receives the resulting tokens and must manage them safely. That handoff is where many CLI designs become weak, because the security model depends on what happens after approval rather than on the login page itself.


For practitioners

  • Constrain device codes to the runtime Never display the device_code to users or logs. Show only the user_code and verification URI, and treat the device code as internal session state used solely for polling and token exchange.
  • Enforce bounded polling behaviour Use the server-provided interval, increase delay on slow_down responses, and stop after expired_token or access_denied so the CLI does not poll indefinitely.
  • Protect tokens after approval Store access and refresh tokens only in memory or a governed secret store, and avoid printing them, persisting them in shell history, or emitting them in debug output.
  • Register CLIs as governed public clients Tie each command-line application to a named client ID, documented owner, and defined revocation path so abandoned tools do not outlive their administrative control.

Key takeaways

  • OAuth device flow is a practical way to authenticate Go CLIs without collecting pasted credentials, but the terminal still becomes part of the identity trust path.
  • Polling, timeout handling, and token stewardship are the controls that determine whether the pattern is orderly or brittle in production use.
  • IAM teams should govern CLI clients like other public applications, with explicit ownership, lifecycle control, and careful handling of issued tokens.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP Non-Human Identity Top 10 and MITRE ATT&CK address the attack and risk surface, while NIST SP 800-63 sets the governance and control requirements practitioners need to meet.

FrameworkControl / ReferenceRelevance
OWASP Non-Human Identity Top 10NHI-04 — Insecure AuthenticationDevice flow authentication depends on correct client handling of device codes and token exchange.
NHI-02 — Secret LeakageThe CLI must not expose device codes, access tokens, or refresh tokens in prompts or logs.
Recommendation — Validate CLI authentication flows against NHI-04 to prevent weak handling of device codes and issued tokens. Apply NHI-02 controls to keep device codes and tokens out of terminal output, logs, and debug traces.
NIST SP 800-63SP 800-63C — FederationThe article uses OAuth device flow to federate browser-based user authentication into a CLI client.
Recommendation — Use SP 800-63C to govern browser-mediated federation between the user, IdP, and CLI client.
MITRE ATT&CKTA0006;TA0008 — Credential Access; Lateral MovementCompromised tokens from CLI flows can support credential abuse and movement into other systems.
Recommendation — Map exposed CLI tokens to TA0006 and TA0008 to prioritise detection around token theft and reuse.

Key terms

  • OAuth Device Flow: An OAuth pattern that lets a CLI obtain user-authorised tokens without hosting a browser callback on the same device. The user completes login on another device, which preserves SSO and MFA while giving the CLI a session-bound credential that is better suited to headless or terminal-first environments.
  • Device Code: A device code is a short, user friendly code generated during device authorization flow. The application displays it so the user can link the terminal session to a browser login. The code ties the authentication attempt to the correct device and helps the server complete token issuance securely.
  • Polling Interval: The delay a client uses when repeatedly checking an authorization endpoint for login completion. In device flow, the interval is a protocol control, not a UI choice, and respecting it helps avoid throttling, noisy retries, and broken sign-in behavior.
  • Bearer Token Stewardship: The discipline of controlling where bearer credentials are stored, logged, refreshed, and revoked after issuance. For command-line tools, this is the main security boundary because possession is enough to use the token. Without strong handling rules, even short-lived access can create an avoidable exposure window.

Deepen your knowledge

NHI governance, agentic AI identity, and machine identity lifecycle are core topics in our NHI Foundation Level course, the industry's only accredited NHI security programme. If you are responsible for identity security strategy or NHI governance in your organisation, it is worth exploring.
NHIMG Editorial Note
Published by the NHIMG editorial team on June 8, 2026.
Updated on October 7, 2026.
NHI Mgmt Group, the independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org