Skip to content

Commit 4a06d63

Browse files
authored
feat: open login browser directly when agent is logging in (#22)
1 parent 1095c77 commit 4a06d63

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

internal/api/events/metadata.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ func RegisterMetadata(key string, value interface{}) {
3636
metadata[key] = value
3737
}
3838

39+
// GetMetadata retrieves the value for the specified metadata, and false if it doesn't
40+
// exist or the type is wrong.
41+
func GetMetadata[V any](key string) (V, bool) {
42+
value, ok := metadata[key]
43+
if !ok {
44+
var v V
45+
return v, false
46+
}
47+
v, ok := value.(V)
48+
return v, ok
49+
}
50+
3951
func stripVersion(v string) string {
4052
parsed, err := semver.NewVersion(v)
4153
if err != nil {

pkg/auth/browser/browser.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,17 @@ func Open(url string) error {
2424
}
2525

2626
// WaitAndOpen prints a message and waits for the user to press Enter before opening the specified URL.
27-
// It stops waiting and does not open the browser if the context is cancelled.
28-
func WaitAndOpen(ctx context.Context, url string) {
27+
// It stops waiting and does not open the browser if the context is canceled.
28+
func WaitAndOpen(ctx context.Context, url string, automatic bool) {
29+
if automatic {
30+
err := Open(url)
31+
if err != nil {
32+
fmt.Printf("Failed to open browser: %v\n", err)
33+
fmt.Println("Please open the above URL manually.")
34+
}
35+
return
36+
}
37+
2938
fmt.Printf("\nPress Enter to open the browser automatically...\n")
3039

3140
go func() {

pkg/auth/device_flow.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/infracost/cli/internal/api/events"
78
"github.com/infracost/cli/pkg/auth/browser"
89
"golang.org/x/oauth2"
910
)
1011

1112
func (c *Config) DeviceFlow(ctx context.Context) (oauth2.TokenSource, *oauth2.Token, error) {
13+
caller, _ := events.GetMetadata[string]("caller")
14+
1215
config := c.OAuth2Config()
1316
verifier := oauth2.GenerateVerifier()
1417

@@ -22,7 +25,7 @@ func (c *Config) DeviceFlow(ctx context.Context) (oauth2.TokenSource, *oauth2.To
2225

2326
browserCtx, cancel := context.WithCancel(ctx)
2427
defer cancel()
25-
browser.WaitAndOpen(browserCtx, response.VerificationURIComplete)
28+
browser.WaitAndOpen(browserCtx, response.VerificationURIComplete, len(caller) > 0)
2629

2730
token, err := config.DeviceAccessToken(ctx, response, oauth2.VerifierOption(verifier))
2831
if err != nil {

pkg/auth/pkce.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ import (
1010
"sync"
1111
"time"
1212

13+
"github.com/infracost/cli/internal/api/events"
1314
"github.com/infracost/cli/pkg/auth/browser"
1415
"golang.org/x/oauth2"
1516
)
1617

1718
func (c *Config) PKCE(ctx context.Context) (oauth2.TokenSource, *oauth2.Token, error) {
19+
caller, _ := events.GetMetadata[string]("caller")
20+
1821
config := c.OAuth2Config()
1922

2023
verifier := oauth2.GenerateVerifier()
@@ -80,7 +83,7 @@ func (c *Config) PKCE(ctx context.Context) (oauth2.TokenSource, *oauth2.Token, e
8083
fmt.Printf("Please go to the following URL to log in:\n%s\n", authURL)
8184
browserCtx, cancel := context.WithCancel(ctx)
8285
defer cancel()
83-
browser.WaitAndOpen(browserCtx, authURL)
86+
browser.WaitAndOpen(browserCtx, authURL, len(caller) > 0)
8487

8588
wg.Wait() // wait for the server to finish
8689

0 commit comments

Comments
 (0)