Repository navigation
Add device code login with auto-detection (matching basecamp-cli) #652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
41e4fb4
720c1ff
e61c0b8
f858f8e
512d6fe
71b21c2
66ebe92
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,6 +53,7 @@ func buildLoginCommand(path string) *cobra.Command { | |
| var ( | ||
| token string | ||
| noBrowser bool | ||
| device bool | ||
| mcp bool | ||
| ) | ||
|
|
||
|
|
@@ -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 | ||
|
|
@@ -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") | ||
| } | ||
|
Comment on lines
101
to
+104
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
An explicitly empty Knowledge Base Used: Command framework and output contracts Prompt To Fix With AIThis 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)) | ||
| } | ||
|
|
@@ -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}) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: (Based on your team's feedback about auth login flag conflict rules.) Prompt for AI agentsThere was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new device path inherits the browser flow's six-minute deadline. Existing Prompt To Fix With AIThis 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. | ||
|
|
@@ -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 | ||
|
|
||
There was a problem hiding this comment.
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=falsestarts MCP login. Validate the credential-mode flags withcmd.Flags().Changed(...)before dispatch.Prompt for AI agents