From 86dbb4ee4fe52f460bf646711a98ebefb99ded79 Mon Sep 17 00:00:00 2001 From: hakansa <43675540+hakansa@users.noreply.github.com> Date: Mon, 7 Apr 2025 14:39:53 +0800 Subject: [PATCH] [client] Add no-browser flag to login and up commands for SSO login control (#3610) * [client] Add no-browser flag to login and up commands for SSO login control (#3610) --- client/cmd/login.go | 30 +++++++++++++++++++++--------- client/cmd/up.go | 9 ++++++++- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/client/cmd/login.go b/client/cmd/login.go index b91cedede..c86d6c636 100644 --- a/client/cmd/login.go +++ b/client/cmd/login.go @@ -19,6 +19,10 @@ import ( "github.com/netbirdio/netbird/util" ) +func init() { + loginCmd.PersistentFlags().BoolVar(&noBrowser, noBrowserFlag, false, noBrowserDesc) +} + var loginCmd = &cobra.Command{ Use: "login", Short: "login to the Netbird Management Service (first run)", @@ -127,7 +131,7 @@ var loginCmd = &cobra.Command{ } if loginResp.NeedsSSOLogin { - openURL(cmd, loginResp.VerificationURIComplete, loginResp.UserCode) + openURL(cmd, loginResp.VerificationURIComplete, loginResp.UserCode, noBrowser) _, err = client.WaitSSOLogin(ctx, &proto.WaitSSOLoginRequest{UserCode: loginResp.UserCode, Hostname: hostName}) if err != nil { @@ -198,7 +202,7 @@ func foregroundGetTokenInfo(ctx context.Context, cmd *cobra.Command, config *int return nil, fmt.Errorf("getting a request OAuth flow info failed: %v", err) } - openURL(cmd, flowInfo.VerificationURIComplete, flowInfo.UserCode) + openURL(cmd, flowInfo.VerificationURIComplete, flowInfo.UserCode, noBrowser) waitTimeout := time.Duration(flowInfo.ExpiresIn) * time.Second waitCTX, c := context.WithTimeout(context.TODO(), waitTimeout) @@ -212,19 +216,27 @@ func foregroundGetTokenInfo(ctx context.Context, cmd *cobra.Command, config *int return &tokenInfo, nil } -func openURL(cmd *cobra.Command, verificationURIComplete, userCode string) { +func openURL(cmd *cobra.Command, verificationURIComplete, userCode string, noBrowser bool) { var codeMsg string if userCode != "" && !strings.Contains(verificationURIComplete, userCode) { codeMsg = fmt.Sprintf("and enter the code %s to authenticate.", userCode) } - cmd.Println("Please do the SSO login in your browser. \n" + - "If your browser didn't open automatically, use this URL to log in:\n\n" + - verificationURIComplete + " " + codeMsg) + if noBrowser { + cmd.Println("Use this URL to log in:\n\n" + verificationURIComplete + " " + codeMsg) + } else { + cmd.Println("Please do the SSO login in your browser. \n" + + "If your browser didn't open automatically, use this URL to log in:\n\n" + + verificationURIComplete + " " + codeMsg) + } + cmd.Println("") - if err := open.Run(verificationURIComplete); err != nil { - cmd.Println("\nAlternatively, you may want to use a setup key, see:\n\n" + - "https://docs.netbird.io/how-to/register-machines-using-setup-keys") + + if !noBrowser { + if err := open.Run(verificationURIComplete); err != nil { + cmd.Println("\nAlternatively, you may want to use a setup key, see:\n\n" + + "https://docs.netbird.io/how-to/register-machines-using-setup-keys") + } } } diff --git a/client/cmd/up.go b/client/cmd/up.go index 926317b8e..8b716a96d 100644 --- a/client/cmd/up.go +++ b/client/cmd/up.go @@ -32,12 +32,16 @@ const ( const ( dnsLabelsFlag = "extra-dns-labels" + + noBrowserFlag = "no-browser" + noBrowserDesc = "do not open the browser for SSO login" ) var ( foregroundMode bool dnsLabels []string dnsLabelsValidated domain.List + noBrowser bool upCmd = &cobra.Command{ Use: "up", @@ -65,6 +69,9 @@ func init() { `E.g. --extra-dns-labels vpc1 or --extra-dns-labels vpc1,mgmt1 `+ `or --extra-dns-labels ""`, ) + + upCmd.PersistentFlags().BoolVar(&noBrowser, noBrowserFlag, false, noBrowserDesc) + } func upFunc(cmd *cobra.Command, args []string) error { @@ -349,7 +356,7 @@ func runInDaemonMode(ctx context.Context, cmd *cobra.Command) error { if loginResp.NeedsSSOLogin { - openURL(cmd, loginResp.VerificationURIComplete, loginResp.UserCode) + openURL(cmd, loginResp.VerificationURIComplete, loginResp.UserCode, noBrowser) _, err = client.WaitSSOLogin(ctx, &proto.WaitSSOLoginRequest{UserCode: loginResp.UserCode, Hostname: hostName}) if err != nil {