Subscribe to the Non-Human & AI Identity Journal

How should security teams choose between PKCE and device flow for CLIs?

Use PKCE for interactive users on their own machines, because it binds the authorization code to the local host and reduces phishing exposure. Use device flow only when no local browser or loopback redirect is practical, such as SSH sessions or containers. If the CLI targets enterprise tenants, ship both and default to the safer path.

Why This Matters for Security Teams

PKCE and device flow both solve the same core problem for CLI sign-in, but they do so under very different trust assumptions. PKCE is designed for interactive users on a local machine, where the CLI can start a browser flow and complete the redirect safely. Device flow exists for cases where that local redirect is unavailable, but it shifts more burden onto the user and the environment. For security teams, the decision is really about reducing phishing risk without breaking legitimate workflows, especially as NHI governance increasingly expects tighter control over how tool-bound identities authenticate. The broader identity picture matters too: NHI estates are often poorly visible, and Ultimate Guide to NHIs notes that only 5.7% of organisations have full visibility into their service accounts.

That visibility gap is why authentication choices for CLIs should be treated as part of identity design, not just developer convenience. A CLI that signs in like a browser app on a laptop can usually benefit from stronger session binding and lower user error. A CLI used through SSH, jump hosts, or ephemeral containers may need a fallback that accepts the environment’s limits. Current guidance from NIST Cybersecurity Framework 2.0 still points teams toward access control, identity proofing, and continuous risk management rather than one fixed login pattern. In practice, many security teams encounter broken CLI sign-in only after users begin copying codes into the wrong browser tab or reusing insecure workarounds.

How It Works in Practice

For most desktop CLIs, PKCE should be the default because it creates a tighter link between the authorization request and the local client session. The user authenticates in a browser, the authorization code is delivered back to the local loopback listener, and the CLI verifies the code challenge before exchanging tokens. That reduces the chance that a stolen code can be replayed elsewhere. For teams defining enterprise login patterns, this is usually the safer first choice, especially when the CLI is acting as a front end to privileged API access governed by RBAC, PAM, or JIT workflows.

Device flow is better reserved for constrained environments where the CLI cannot reliably open a browser or receive a callback. Common examples include SSH sessions, headless servers, containers, remote shells, and some VDI setups. The user completes authentication on a second device, then the CLI polls for the result. That tradeoff is practical, but it is also easier to misuse, because users can be tricked into approving the wrong prompt or entering the code into an untrusted page.

  • Use PKCE when the CLI can launch a browser and bind the redirect to localhost.
  • Use device flow only when the local redirect path is genuinely unavailable.
  • Set shorter token lifetimes and step-up checks for higher-risk enterprise tenants.
  • Log authentication method, tenant, and device context for audit and anomaly detection.

For teams building policy around non-human access, it helps to align CLI auth with broader identity governance and lifecycle controls described in Ultimate Guide to NHIs. The same thinking applies to NIST Cybersecurity Framework 2.0: identify the asset, limit access, monitor use, and recover quickly when trust is lost. These controls tend to break down when a single CLI binary has to work across mixed environments such as local laptops, remote shells, and ephemeral build containers because the browser and redirect assumptions are no longer consistent.

Common Variations and Edge Cases

Tighter authentication often increases friction, so security teams have to balance user experience against the risk of code interception or token misuse. There is no universal standard for every CLI scenario yet, so the right choice often depends on whether the user context is stable and browser-capable. In managed endpoints, PKCE with a loopback redirect is usually the cleaner option. In locked-down environments, device flow may be the only workable path, but it should be paired with tenant restrictions, explicit user prompts, and strong monitoring.

Enterprise distribution adds another wrinkle. If a CLI serves both developers and operators, ship both flows and default to PKCE where possible, then fall back to device flow only when the runtime cannot support a local browser. Teams should also avoid treating device flow as a blanket exception for privileged work, because it can become the path of least resistance and quietly expand the attack surface. For NHI-heavy estates, that concern is consistent with the wider findings in Ultimate Guide to NHIs and the identity controls emphasised by NIST Cybersecurity Framework 2.0.

Best practice is evolving for CLIs used by service teams, automation wrappers, and developer tools that can impersonate user intent. In those cases, the safer pattern is to separate human sign-in from workload authentication rather than stretching device flow to cover everything. Security teams that ignore that boundary usually end up with a login method chosen for compatibility, not for trust.

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 address the attack and risk surface, while NIST CSF 2.0 and NIST Zero Trust (SP 800-207) set the governance and control requirements practitioners need to meet.

Framework Control / Reference Relevance
OWASP Non-Human Identity Top 10 NHI-01 Covers identity and secret handling for CLI-accessed non-human workloads.
NIST CSF 2.0 PR.AC-1 Access control guidance supports selecting the safest login method per environment.
NIST Zero Trust (SP 800-207) ID Zero Trust identity validation fits the need to verify context at each CLI session.

Choose the least-privilege auth flow and keep CLI tokens short-lived, traceable, and revocable.