Skip to content

Commit 0612cb4

Browse files
committed
feat(cli): auto-detect SSH sessions and fix fallback URL
- Detect SSH sessions via SSH_CLIENT/SSH_TTY/SSH_CONNECTION env vars and automatically use headless auth (no localhost callback) - Strip callback param from the fallback URL shown in browser flow so users on a different machine get the token inline when navigating Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
1 parent 28b746e commit 0612cb4

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

app/cli/cmd/auth_login.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"net"
2424
"net/http"
2525
"net/url"
26+
"os"
2627
"os/exec"
2728
"runtime"
2829
"time"
@@ -75,7 +76,7 @@ func interactiveAuth(forceHeadless bool) error {
7576
q.Set(oauth.QueryParamLongLived, "true")
7677
serverLoginURL.RawQuery = q.Encode()
7778

78-
if forceHeadless {
79+
if forceHeadless || isRemoteSession() {
7980
return headlessAuth(serverLoginURL)
8081
}
8182

@@ -87,10 +88,17 @@ func interactiveAuth(forceHeadless bool) error {
8788

8889
a.serverChan = make(chan error)
8990

91+
// Show the headless URL (no callback) as fallback so users on a different
92+
// machine can navigate to it and get the token to paste back.
93+
manualURL := *serverLoginURL
94+
mq := manualURL.Query()
95+
mq.Set(oauth.QueryParamCallback, "")
96+
manualURL.RawQuery = mq.Encode()
97+
9098
fmt.Println()
9199
fmt.Println(" Browser didn't open? Use the URL below to sign in:")
92100
fmt.Println()
93-
fmt.Printf(" %s\n", serverLoginURL.String())
101+
fmt.Printf(" %s\n", manualURL.String())
94102
fmt.Println()
95103

96104
// Start the local callback server
@@ -203,6 +211,17 @@ func openbrowser(url string) error {
203211
return cmd.Wait()
204212
}
205213

214+
// isRemoteSession returns true if the CLI is running inside an SSH session
215+
// where a local browser callback would not be reachable.
216+
func isRemoteSession() bool {
217+
for _, key := range []string{"SSH_CLIENT", "SSH_TTY", "SSH_CONNECTION"} {
218+
if os.Getenv(key) != "" {
219+
return true
220+
}
221+
}
222+
return false
223+
}
224+
206225
// Retrieve loginURL from the control plane
207226
func retrieveLoginURL() (string, error) {
208227
client := pb.NewStatusServiceClient(ActionOpts.CPConnection)

0 commit comments

Comments
 (0)