Use the device authorization flow so the CLI starts authentication, shows a short code, and sends users to a browser for login. This avoids storing long lived API keys on disk or in environment files, while keeping access tied to the normal session lifecycle. Pair it with MFA, SSO, and standard revocation controls for safer non browser authentication.
Why This Matters for Security Teams
oauth device flow is often treated as a convenience feature for headless tools, but for CLI authentication it becomes an identity control surface. If the flow is implemented poorly, teams can simply replace one risk with another: long-lived API keys on disk, shared refresh tokens, or uncontrolled device codes that can be replayed. Current guidance from the OWASP Non-Human Identity Top 10 is clear that non-human access needs explicit governance, not just functional auth.
The core issue is that CLI tools are often used by engineers, automation jobs, and AI-assisted workflows that behave differently from browsers. Security teams need to bind the CLI to the user’s normal identity lifecycle, enforce MFA and SSO, and avoid creating a new class of dormant credentials that are hard to inventory or revoke. NHIMG research on the State of Non-Human Identity Security shows how limited visibility into OAuth-connected apps remains, which is exactly why device flow must be designed for revocation and monitoring from day one. In practice, many security teams discover the weakness only after a token appears in logs or a stolen workstation has already turned a “temporary” login into persistent access.
How It Works in Practice
The safest pattern is to treat device flow as a short-lived bootstrap for a verified session, not as a credential store. The CLI requests a device code, displays a short user code, and asks the user to complete authentication in a trusted browser. The identity provider then returns tokens to the CLI only after the browser session satisfies normal controls such as SSO, MFA, conditional access, and device trust where applicable. For identity assurance, align the user login with NIST SP 800-63 Digital Identity Guidelines and use the authorization server’s standard token lifetimes rather than extending them for convenience.
Implementation should reduce the number of places a token can persist. Prefer in-memory token handling, encrypted OS keychains only when persistence is unavoidable, and explicit logout or revocation on job completion. Pair the flow with these guardrails:
- Use short TTL access tokens and rotate refresh tokens aggressively.
- Bind tokens to the minimum scopes required for the CLI task.
- Record device-code issuance, approval, token exchange, and revocation events.
- Require re-authentication for sensitive actions instead of silently renewing access.
- Block shared device codes and prevent reuse across terminals or users.
This is also where NHI governance matters. Secrets sprawl is a recurring failure mode, and NHIMG’s Ultimate Guide to NHIs — Static vs Dynamic Secrets is useful for framing why static credentials age badly compared with time-bound tokens. Device flow should be monitored like any other privileged identity event, not treated as a harmless login shortcut. These controls tend to break down in shared jump hosts and build runners because locally cached tokens can outlive the session that created them.
Common Variations and Edge Cases
Tighter token controls often increase user friction and support overhead, so security teams need to balance usability against the risk of credential persistence. That tradeoff is especially visible in automation-heavy environments where developers expect a CLI to “just work” across repeated runs.
Best practice is evolving for non-browser authentication in two main edge cases. First, if the CLI is used by a script, pipeline, or agent rather than an interactive human, device flow may be the wrong pattern entirely. In those cases, current guidance suggests using workload identity or service-to-service auth instead of a user-bound device code. Second, if the organization permits token caching, the cache must be protected by OS-backed storage, scoped tightly, and invalidated on logout, host compromise, or role change. The NIST SP 800-53 Rev 5 Security and Privacy Controls and Salesloft OAuth token breach both reinforce the same operational lesson: access that is easy to mint must also be easy to revoke and hard to reuse. For teams that need broader threat context, the Guide to the Secret Sprawl Challenge shows how quickly convenience features can become durable attack paths if not governed carefully.
Where this guidance breaks down most often is in developer tools that silently re-authenticate in the background, because users lose visibility into when a “temporary” session becomes a standing privilege.
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, OWASP Agentic AI Top 10 and CSA MAESTRO address the attack and risk surface, while NIST AI RMF and NIST CSF 2.0 set the governance and control requirements practitioners need to meet.
| Framework | Control / Reference | Relevance |
|---|---|---|
| OWASP Non-Human Identity Top 10 | NHI-02 | Device flow can still create persistent NHI tokens if lifecycle controls are weak. |
| OWASP Agentic AI Top 10 | A-03 | CLI automation can behave like an agent if it acts without direct user oversight. |
| CSA MAESTRO | IAM-01 | MAESTRO emphasizes identity and access governance for machine and agent workflows. |
| NIST AI RMF | AI RMF helps assess whether auth patterns match the autonomy and context of the workload. | |
| NIST CSF 2.0 | PR.AA-01 | Identity proofing and authentication are central to secure CLI device flow. |
Avoid user-bound device flow for autonomous workflows; use workload identity with runtime authorization.
Related resources from NHI Mgmt Group
- How should security teams implement biometric authentication for citizen access without creating new privacy and fraud risks?
- How should security teams implement passwordless authentication without creating new recovery risk?
- How should security teams implement cloud IAM without creating new privilege sprawl?
- How should security teams implement just-in-time access without creating new governance gaps?
Deepen Your Knowledge
Reviewed and updated by the NHIMG editorial team on August 28, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org