Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cmd/vsp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 9 additions & 1 deletion pkg/adt/browser_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/url"
"os"
"os/exec"
"regexp"
"runtime"
"strings"
"time"
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/adt/browser_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down