diff --git a/cmd/vsp/main.go b/cmd/vsp/main.go index 92acdb48..5b4f7637 100755 --- a/cmd/vsp/main.go +++ b/cmd/vsp/main.go @@ -522,8 +522,14 @@ func processBrowserAuth(cmd *cobra.Command) error { browserExec = viper.GetString("BROWSER_EXEC") } + // Determine sap client + sapClient := cfg.Client + if sapClient == "" { + sapClient = viper.GetString("CLIENT") + } + ctx := cmd.Context() - cookies, err := adt.BrowserLogin(ctx, cfg.BaseURL, cfg.InsecureSkipVerify, timeout, browserExec, cfg.Verbose) + cookies, err := adt.BrowserLogin(ctx, cfg.BaseURL, cfg.InsecureSkipVerify, timeout, browserExec, cfg.Verbose, sapClient) if err != nil { return fmt.Errorf("browser authentication failed: %w", err) } diff --git a/pkg/adt/browser_auth.go b/pkg/adt/browser_auth.go index e65bc78c..08d94c7a 100644 --- a/pkg/adt/browser_auth.go +++ b/pkg/adt/browser_auth.go @@ -6,6 +6,7 @@ import ( "net/url" "os" "os/exec" + "regexp" "runtime" "strings" "time" @@ -102,7 +103,7 @@ func friendlyBrowserName(path string) string { // The browser navigates to the ADT discovery endpoint which requires authentication, // triggering the SSO redirect. Once SAP-specific cookies appear, they are extracted // and the browser is closed. -func BrowserLogin(ctx context.Context, sapURL string, insecure bool, timeout time.Duration, execPath string, verbose bool) (map[string]string, error) { +func BrowserLogin(ctx context.Context, sapURL string, insecure bool, timeout time.Duration, execPath string, verbose bool, sapClient string) (map[string]string, error) { u, err := url.Parse(sapURL) if err != nil { return nil, fmt.Errorf("invalid SAP URL: %w", err) @@ -120,6 +121,13 @@ func BrowserLogin(ctx context.Context, sapURL string, insecure bool, timeout tim adtURL.Path = "/sap/bc/adt/" adtURL.RawQuery = "" adtURL.Fragment = "" + // if a valid sap-Client is provided, add it as a query parameter to ensure cookies are set for the correct client. + matched, _ := regexp.MatchString(`^[0-9]{3}$`, sapClient) + if matched { + q := adtURL.Query() + q.Set("sap-client", sapClient) + adtURL.RawQuery = q.Encode() + } targetURL := adtURL.String() // Create a headed (non-headless) browser context diff --git a/pkg/adt/browser_auth_test.go b/pkg/adt/browser_auth_test.go index dc39028e..7599f0dc 100644 --- a/pkg/adt/browser_auth_test.go +++ b/pkg/adt/browser_auth_test.go @@ -91,12 +91,12 @@ func TestSaveCookiesToFile_InvalidURL(t *testing.T) { func TestBrowserLogin_InvalidURL(t *testing.T) { ctx := context.TODO() - _, err := BrowserLogin(ctx, "", false, 0, "", false) + _, err := BrowserLogin(ctx, "", false, 0, "", false, "") if err == nil { t.Error("expected error for empty URL") } - _, err = BrowserLogin(ctx, "not-a-url", false, 0, "", false) + _, err = BrowserLogin(ctx, "not-a-url", false, 0, "", false, "") if err == nil { t.Error("expected error for invalid URL") }