Subscribe to the Non-Human & AI Identity Journal

How should security teams handle authentication for CLI tools without embedding browser login in the terminal?

Use the OAuth 2.0 Device Authorization Grant so the human signs in through a browser while the CLI only requests and exchanges a device code. Register the CLI as a public client, keep scopes minimal, and make sure the terminal never handles a reusable password or client secret. That gives you browser-grade authentication without turning the CLI into a credential store.

Why This Matters for Security Teams

Terminal-based login is a poor fit for CLI automation because it encourages the wrong security boundary. A CLI is not a trusted browser, and embedding interactive login flow inside a terminal often leads teams to cache credentials, reuse tokens too broadly, or add custom exceptions that become permanent. For command-line tools, the safer pattern is a device-based browser handoff so the user authenticates in a real browser while the CLI only receives a short-lived authorization result. That aligns better with the broader identity lessons in the Ultimate Guide to NHIs and with identity governance expectations in the NIST Cybersecurity Framework 2.0.

The operational issue is not whether a terminal can display a login prompt. It is whether the tool can authenticate without turning the shell into a credential-bearing session that outlives the task. In practice, many teams discover the weakness only after tokens are copied into scripts, shared across environments, or left valid long after the original user session ended.

How It Works in Practice

The standard approach is the OAuth 2.0 Device Authorization Grant. The CLI starts by requesting a device code, then shows the user a verification URL and a short code. The human completes sign-in in a browser, usually with the same identity controls already used for workforce access. The CLI polls the authorization server until it receives a token response, at which point it can act on behalf of the authenticated user without ever handling a reusable password or browser session cookie.

Security teams should treat this as a workload-identity problem as much as a user-authentication problem. The CLI itself should be registered as a public client, with no embedded secret, and scopes should be minimal and task-specific. Current guidance suggests keeping access tokens short-lived, binding refresh behavior to risk, and logging both device-code issuance and token exchange so abnormal use can be investigated. Where possible, pair the flow with conditional access, phishing-resistant human authentication, and step-up checks for sensitive actions.

  • Use the browser for human authentication and consent, not the terminal.
  • Issue short-lived tokens only after the browser completes verification.
  • Register the CLI as a public client and avoid static client secrets.
  • Limit scopes to the smallest permissions required for the command.
  • Log issuance, polling, exchange, and revocation events for auditability.

That model fits the access patterns described in The State of Non-Human Identity Security, especially where OAuth-connected tooling creates visibility gaps and overbroad access. It also aligns with the NIST view that access control should be measurable and continuously governed, not assumed safe because the entry point was interactive. These controls tend to break down in headless enterprise environments where proxy-authentication, shared build hosts, or unattended long-running jobs make browser handoff impractical because token lifecycle and user presence are no longer tightly coupled.

Common Variations and Edge Cases

Tighter authentication usually adds friction, so teams have to balance usability against the risk of credential sprawl. That tradeoff becomes sharper for power-user CLI tools used dozens of times per day, where over-aggressive reauth prompts can drive users toward unsafe workarounds. For lower-risk tools, current guidance suggests device flow is usually the right default; for high-risk administrative CLIs, best practice is evolving toward stronger step-up authentication and narrower token lifetimes.

There are also cases where device flow is not enough. Shared terminals, air-gapped systems, and automation runners without a human present should not be forced into browser login at all. Those environments usually need a different trust model, such as workload identity, federated service credentials, or delegated short-lived access through a separate control plane. Security teams should also be careful with cached sessions: even if the first login is browser-based, long-lived refresh tokens or local token caches can recreate the same problem in slower motion. The practical question is not whether the CLI can authenticate once, but whether the resulting access can be revoked, traced, and limited cleanly across the full task lifecycle.

In environments with heavy compliance requirements, teams should align the flow to documented identity controls and prove that the terminal never receives reusable secrets. That is especially important where third-party CLIs access cloud APIs, because device flow solves interactive sign-in only if token storage, revocation, and scope design are equally disciplined.

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 CSA MAESTRO address the attack and risk surface, while NIST AI RMF set the governance and control requirements practitioners need to meet.

Framework Control / Reference Relevance
OWASP Non-Human Identity Top 10 NHI-01 Covers unsafe secret handling in CLI and token-based NHI access.
CSA MAESTRO A1 Addresses identity and access control for autonomous and tool-using workloads.
NIST AI RMF Supports risk-based governance for identity-enabled AI and automation.

Apply risk management to token issuance, scope limitation, and revocation for automated access.