Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .surface
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ hey attachment save --force
hey attachment save --output
hey auth
hey auth login
hey auth login --device
hey auth login --mcp
hey auth login --no-browser
hey auth login --token
Expand Down Expand Up @@ -282,6 +283,7 @@ hey label view --all
hey label view --limit
hey label view --page
hey login
hey login --device
hey login --mcp
hey login --no-browser
hey login --token
Expand Down
5 changes: 5 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ scripts and agents can handle it.
# Browser-based OAuth against HEY's own OAuth server (primary method)
hey auth login

# Headless device code flow (for SSH, servers, CI, Docker)
hey auth login --device

# Or use a pre-generated token
hey auth login --token TOKEN
```
Expand All @@ -51,6 +54,8 @@ Browser session cookies are no longer supported. Existing cookie-only logins mus

OAuth tokens refresh automatically on expiry. Credentials are stored in the system keyring (with file fallback at `~/.config/hey-cli/credentials.json`).

The device code flow (`--device`) prints a link and a one-time code, waits for you to approve the code in HEY from any device (your phone, another computer), and never opens a browser. It's designed for headless environments where a browser cannot open or a callback server cannot listen.

```bash
hey auth status # check auth status
hey auth token # print the bearer token for scripting
Expand Down
8 changes: 4 additions & 4 deletions internal/auth/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
// a scoped request is refused with invalid_request (or, on #9041 alone, the row with
// unauthorized_client), and the sign-in falls back to loopback authorization.

// errDeviceFlowUnavailable means this HEY can't serve a scoped device sign-in, so the
// ErrDeviceFlowUnavailable means this HEY can't serve a scoped device sign-in, so the
// caller should sign in by loopback instead.
var errDeviceFlowUnavailable = errors.New("this HEY doesn't take scoped device sign-ins yet")
var ErrDeviceFlowUnavailable = errors.New("this HEY doesn't take scoped device sign-ins yet")

// deviceGrantType is RFC 8628 §3.4's grant type.
const deviceGrantType = "urn:ietf:params:oauth:grant-type:device_code"
Expand All @@ -45,7 +45,7 @@ type deviceAuthorization struct {
}

// LoginDevice signs in with the device authorization grant, asking for the manager's
// scope. It returns errDeviceFlowUnavailable, wrapped with HEY's answer, when this HEY
// scope. It returns ErrDeviceFlowUnavailable, wrapped with HEY's answer, when this HEY
// can't serve the request, so the caller can fall back to Login.
func (m *Manager) LoginDevice(ctx context.Context, opts LoginOptions) error {
tokenEndpoint := m.baseURL + "/oauth/tokens"
Expand Down Expand Up @@ -135,7 +135,7 @@ func (m *Manager) requestDeviceAuthorization(ctx context.Context) (*deviceAuthor
if status != http.StatusOK {
code := oauthErrorCode(body)
if deviceFlowUnavailable(status, code) {
return nil, fmt.Errorf("%w (HTTP %d %s)", errDeviceFlowUnavailable, status, code)
return nil, fmt.Errorf("%w (HTTP %d %s)", ErrDeviceFlowUnavailable, status, code)
}
return nil, fmt.Errorf("device authorization failed: HTTP %d %s", status, code)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/auth/mcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (m *Manager) LoginMCP(ctx context.Context, opts LoginOptions) (string, erro
}

err := m.LoginDevice(ctx, opts)
if !errors.Is(err, errDeviceFlowUnavailable) {
if !errors.Is(err, ErrDeviceFlowUnavailable) {
return "device", err
}
opts.log(fmt.Sprintf("\n%v, so signing in through the browser instead.\n", err))
Expand Down
32 changes: 30 additions & 2 deletions internal/cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func buildLoginCommand(path string) *cobra.Command {
var (
token string
noBrowser bool
device bool
mcp bool
)

Expand All @@ -64,6 +65,10 @@ func buildLoginCommand(path string) *cobra.Command {
Opens a browser for OAuth authentication against HEY's own OAuth server, using PKCE.
Use --token for non-interactive login.

--device signs in with a device code (RFC 8628): prints a link and a one-time code,
waits for you to approve the code in HEY from any device, and never opens a browser.
Designed for headless environments (servers, CI, Docker) and remote sessions over SSH.

--mcp signs in hey mcp instead, as its own app ("HEY MCP (local)" in HEY's Connected
Apps), asking to read, write and send but nothing more. Its credential is kept apart
from the CLI's, and hey mcp serves on it alone. It uses a device code where HEY
Expand All @@ -73,17 +78,24 @@ saves drafts but can't send them.`,
" " + path,
" " + path + " --token YOUR_BEARER_TOKEN",
" " + path + " --no-browser",
" " + path + " --device",
" " + path + " --mcp",
}, "\n"),
RunE: func(cmd *cobra.Command, args []string) error {
if mcp {
if token != "" || cmd.Flags().Changed("token") {
return apierr.ErrUsage("--mcp signs in through HEY; it takes no --token")
}
if device {
return apierr.ErrUsage("--mcp signs in through HEY; it takes no --device")
}
return loginMCP(cmd, noBrowser)
}

if token != "" {
if device {
return apierr.ErrUsage("--device signs in through HEY; it takes no --token")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The new mode-conflict checks use flag values instead of Changed state, so explicitly supplied empty or false flags can bypass them: --device --token= starts device login, and --mcp --device=false starts MCP login. Validate the credential-mode flags with cmd.Flags().Changed(...) before dispatch.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At internal/cmd/auth.go, line 97:

<comment>The new mode-conflict checks use flag values instead of Changed state, so explicitly supplied empty or false flags can bypass them: `--device --token=` starts device login, and `--mcp --device=false` starts MCP login. Validate the credential-mode flags with `cmd.Flags().Changed(...)` before dispatch.</comment>

<file context>
@@ -73,17 +78,24 @@ saves drafts but can't send them.`,
 
 			if token != "" {
+				if device {
+					return apierr.ErrUsage("--device signs in through HEY; it takes no --token")
+				}
 				if err := authMgr.LoginWithToken(token); err != nil {
</file context>

}
Comment on lines 101 to +104

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Empty tokens bypass conflict checks

An explicitly empty --token skips this check because it sits inside if token != "". For example, hey login --device --token="$TOKEN" starts device sign-in when TOKEN is unset instead of reporting conflicting flags. Check cmd.Flags().Changed("token"), as the --mcp branch already does.

Knowledge Base Used: Command framework and output contracts

Prompt To Fix With AI
This is a comment left during a code review.
Path: internal/cmd/auth.go
Line: 96-98

Comment:
**Empty tokens bypass conflict checks**

An explicitly empty `--token` skips this check because it sits inside `if token != ""`. For example, `hey login --device --token="$TOKEN"` starts device sign-in when `TOKEN` is unset instead of reporting conflicting flags. Check `cmd.Flags().Changed("token")`, as the `--mcp` branch already does.

**Knowledge Base Used:** [Command framework and output contracts](https://app.greptile.com/-/switch-tenant?tenant=37signals&next=%2Fcustom-context%2Fknowledge-base%2Fbasecamp%2Fhey-cli%2F-%2Fdocs%2Fcommand-framework-and-output.md)

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

if err := authMgr.LoginWithToken(token); err != nil {
return apierr.ErrAuth(fmt.Sprintf("could not save token: %v", err))
}
Expand All @@ -95,7 +107,22 @@ saves drafts but can't send them.`,
ctx, cancel := context.WithTimeout(cmd.Context(), 6*time.Minute)
defer cancel()

if err := authMgr.Login(ctx, auth.LoginOptions{NoBrowser: noBrowser}); err != nil {
var method string
var err error
if device {
logFunc := func(msg string) {
fmt.Fprint(cmd.ErrOrStderr(), msg)
}
err = authMgr.LoginDevice(ctx, auth.LoginOptions{NoBrowser: true, Logger: logFunc})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: --device --no-browser is accepted even though these login options are required to conflict, and the second flag is silently ignored. Reject the combination before selecting the device flow while continuing to allow --no-browser with browser OAuth and token login.

(Based on your team's feedback about auth login flag conflict rules.)

View Feedback

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At internal/cmd/auth.go, line 116:

<comment>`--device --no-browser` is accepted even though these login options are required to conflict, and the second flag is silently ignored. Reject the combination before selecting the device flow while continuing to allow `--no-browser` with browser OAuth and token login.

(Based on your team's feedback about auth login flag conflict rules.) </comment>

<file context>
@@ -95,7 +107,22 @@ saves drafts but can't send them.`,
+				logFunc := func(msg string) {
+					fmt.Fprint(cmd.ErrOrStderr(), msg)
+				}
+				err = authMgr.LoginDevice(ctx, auth.LoginOptions{NoBrowser: true, Logger: logFunc})
+				method = "device"
+			} else {
</file context>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Device login ends too early

The new device path inherits the browser flow's six-minute deadline. Existing loginMCP allows twelve minutes because device codes live for ten minutes. Someone who approves after minute six finds that the CLI has already stopped with context deadline exceeded, even though their code is still valid. Give device login enough time to cover the code's lifetime.

Prompt To Fix With AI
This is a comment left during a code review.
Path: internal/cmd/auth.go
Line: 116

Comment:
**Device login ends too early**

The new device path inherits the browser flow's six-minute deadline. Existing `loginMCP` allows twelve minutes because device codes live for ten minutes. Someone who approves after minute six finds that the CLI has already stopped with `context deadline exceeded`, even though their code is still valid. Give device login enough time to cover the code's lifetime.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

method = "device"
} else {
err = authMgr.Login(ctx, auth.LoginOptions{NoBrowser: noBrowser})
method = "oauth"
}
if err != nil {
if errors.Is(err, auth.ErrDeviceFlowUnavailable) {
return apierr.ErrAuth(fmt.Sprintf("device flow sign-in failed: %v\n\nThis HEY server does not support device authorization (RFC 8628) yet. Sign in with the browser flow instead: %s", err, authMgr.LoginCommand()))
}
return apierr.ErrAuth(fmt.Sprintf("login failed: %v", err))
}
// Replaced credentials orphan whatever the old ones cached.
Expand All @@ -112,12 +139,13 @@ saves drafts but can't send them.`,
ensureOmarchyBarPluginAfterLogin(cmd.ErrOrStderr())
return nil
}
return writeOK(map[string]string{"method": "oauth"}, output.WithSummary("Logged in successfully"))
return writeOK(map[string]string{"method": method}, output.WithSummary("Logged in successfully"))
},
}

cmd.Flags().StringVar(&token, "token", "", "Pre-generated Bearer token")
cmd.Flags().BoolVar(&noBrowser, "no-browser", false, "Don't open browser, print URL instead")
cmd.Flags().BoolVar(&device, "device", false, "Headless device code flow: print link and code, never open browser")
cmd.Flags().BoolVar(&mcp, "mcp", false, "Sign in hey mcp with its own credential (read, write and send)")

return cmd
Expand Down
Loading
Loading