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 4 commits
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 |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| ) | ||
|
|
||
| // oauthMetadata represents RFC 8414 OAuth Authorization Server Metadata. | ||
| type oauthMetadata struct { | ||
| Issuer string `json:"issuer"` | ||
| AuthorizationEndpoint string `json:"authorization_endpoint,omitempty"` | ||
| TokenEndpoint string `json:"token_endpoint"` | ||
| DeviceAuthorizationEndpoint string `json:"device_authorization_endpoint,omitempty"` | ||
| GrantTypesSupported []string `json:"grant_types_supported,omitempty"` | ||
| CodeChallengeMethodsSupported []string `json:"code_challenge_methods_supported,omitempty"` | ||
| } | ||
|
|
||
| // DiscoverOAuthMetadata fetches RFC 8414 OAuth Authorization Server Metadata | ||
| // from the well-known path for the given base URL. Returns nil metadata with | ||
| // no error when the metadata document is not found (404), allowing fallback | ||
| // to legacy auth. Other errors are returned as-is. | ||
| func (m *Manager) DiscoverOAuthMetadata(ctx context.Context) (*oauthMetadata, error) { | ||
| // Construct the well-known URL from the base URL | ||
| metadataURL := m.baseURL + "/.well-known/oauth-authorization-server" | ||
|
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: This URL construction queries the wrong metadata endpoint for path-based base URLs. Build the RFC 8414 well-known URL by inserting the suffix between the host and issuer path, or reject path-bearing base URLs explicitly. Prompt for AI agents |
||
|
|
||
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, metadataURL, nil) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("building metadata request: %w", err) | ||
| } | ||
|
|
||
| resp, err := m.httpClient.Do(req) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("fetching OAuth metadata: %w", err) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| // 404 means no metadata document - not an error, just means fallback to legacy | ||
| if resp.StatusCode == http.StatusNotFound { | ||
| return nil, nil | ||
| } | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return nil, fmt.Errorf("OAuth metadata request failed: HTTP %d", resp.StatusCode) | ||
| } | ||
|
|
||
| body, err := io.ReadAll(io.LimitReader(resp.Body, 64<<10)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("reading OAuth metadata: %w", err) | ||
| } | ||
|
|
||
| var metadata oauthMetadata | ||
| if err := json.Unmarshal(body, &metadata); err != nil { | ||
| return nil, fmt.Errorf("parsing OAuth metadata: %w", err) | ||
| } | ||
|
|
||
| return &metadata, nil | ||
| } | ||
|
|
||
| // SupportsDeviceFlow reports whether the metadata advertises device authorization support. | ||
| func (md *oauthMetadata) SupportsDeviceFlow() bool { | ||
| if md == nil { | ||
| return false | ||
| } | ||
| if md.DeviceAuthorizationEndpoint == "" { | ||
| return false | ||
| } | ||
| for _, grant := range md.GrantTypesSupported { | ||
| if grant == "urn:ietf:params:oauth:grant-type:device_code" { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,9 +58,9 @@ func (m *Manager) ClientID() string { | |
| return m.clientID | ||
| } | ||
|
|
||
| // LoginMCP signs in the local MCP client. It uses the device flow where HEY serves | ||
| // scoped device requests, and otherwise loopback authorization with the same scope, | ||
| // returning which one it used ("device" or "oauth"). | ||
| // LoginMCP signs in the local MCP client. It uses RFC 8414 metadata discovery to | ||
| // determine whether device flow is available, using it when present and falling back | ||
| // to loopback authorization otherwise. Returns which flow was used ("device" or "oauth"). | ||
| func (m *Manager) LoginMCP(ctx context.Context, opts LoginOptions) (string, error) { | ||
| switch m.clientID { | ||
| case "": | ||
|
|
@@ -71,11 +71,16 @@ func (m *Manager) LoginMCP(ctx context.Context, opts LoginOptions) (string, erro | |
| return "", fmt.Errorf("the configured MCP client id is the CLI's own; set the HEY MCP (local) client id") | ||
| } | ||
|
|
||
| err := m.LoginDevice(ctx, opts) | ||
| if !errors.Is(err, errDeviceFlowUnavailable) { | ||
| metadata, err := m.DiscoverOAuthMetadata(ctx) | ||
| if err != nil { | ||
| return "", fmt.Errorf("OAuth metadata discovery failed: %w", err) | ||
| } | ||
|
|
||
| if metadata != nil && metadata.SupportsDeviceFlow() { | ||
| err := m.LoginDevice(ctx, metadata.DeviceAuthorizationEndpoint, opts) | ||
|
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: This discovery path ignores the advertised Prompt for AI agents |
||
| return "device", err | ||
| } | ||
| opts.log(fmt.Sprintf("\n%v, so signing in through the browser instead.\n", err)) | ||
|
|
||
| return "oauth", m.Login(ctx, opts) | ||
| } | ||
|
|
||
|
|
||
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: This describes endpoint presence as sufficient, but
SupportsDeviceFlowalso requires the RFC 8628 device grant ingrant_types_supported. Document that login falls back when metadata does not advertise complete device-flow support.Prompt for AI agents