Skip to content
Merged
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
123 changes: 119 additions & 4 deletions cmd/vsp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ func init() {
rootCmd.Flags().String("browser-exec", "", "Path to Chromium-based browser (default: auto-detect Edge, Chrome, Chromium)")
rootCmd.Flags().String("cookie-save", "", "Save browser auth cookies to file for reuse with --cookie-file")

// Programmatic SAML SSO authentication (no browser required)
rootCmd.Flags().Bool("saml-auth", false, "Authenticate via programmatic SAML SSO (no browser, no MFA)")
rootCmd.Flags().String("saml-user", "", "SAML/IAS username (email)")
rootCmd.Flags().String("saml-password", "", "SAML/IAS password")
rootCmd.Flags().String("credential-cmd", "", "External command returning JSON {\"username\":...,\"password\":...} (space-separated argv, no shell)")


// Session keep-alive
rootCmd.Flags().Duration("keepalive", 5*time.Minute, "Session keep-alive interval (e.g., 60s, 5m). Prevents session timeout during idle periods. 0 = disabled")

Expand Down Expand Up @@ -160,6 +167,10 @@ func init() {
viper.BindPFlag("cookie-string", rootCmd.Flags().Lookup("cookie-string"))
viper.BindPFlag("browser-auth", rootCmd.Flags().Lookup("browser-auth"))
viper.BindPFlag("browser-auth-timeout", rootCmd.Flags().Lookup("browser-auth-timeout"))
viper.BindPFlag("saml-auth", rootCmd.Flags().Lookup("saml-auth"))
viper.BindPFlag("saml-user", rootCmd.Flags().Lookup("saml-user"))
viper.BindPFlag("saml-password", rootCmd.Flags().Lookup("saml-password"))
viper.BindPFlag("credential-cmd", rootCmd.Flags().Lookup("credential-cmd"))
viper.BindPFlag("browser-exec", rootCmd.Flags().Lookup("browser-exec"))
viper.BindPFlag("cookie-save", rootCmd.Flags().Lookup("cookie-save"))
viper.BindPFlag("keepalive", rootCmd.Flags().Lookup("keepalive"))
Expand Down Expand Up @@ -207,6 +218,11 @@ func runServer(cmd *cobra.Command, args []string) error {
return err
}

// Programmatic SAML SSO authentication (must run before processCookieAuth)
if err := processSAMLAuth(cmd); err != nil {
return err
}

// Process cookie authentication
if err := processCookieAuth(cmd); err != nil {
return err
Expand All @@ -223,6 +239,8 @@ func runServer(cmd *cobra.Command, args []string) error {
fmt.Fprintf(os.Stderr, "[VERBOSE] SAP Language: %s\n", cfg.Language)
if cfg.Username != "" {
fmt.Fprintf(os.Stderr, "[VERBOSE] Auth: Basic (user: %s)\n", cfg.Username)
} else if cfg.ReauthFunc != nil {
fmt.Fprintf(os.Stderr, "[VERBOSE] Auth: SAML (%d cookies, re-auth on 401)\n", len(cfg.Cookies))
} else if len(cfg.Cookies) > 0 {
fmt.Fprintf(os.Stderr, "[VERBOSE] Auth: Cookie (%d cookies)\n", len(cfg.Cookies))
}
Expand Down Expand Up @@ -306,7 +324,9 @@ func resolveConfig(cmd *cobra.Command) {
cookieAuthViaEnv := viper.GetString("COOKIE_FILE") != "" || viper.GetString("COOKIE_STRING") != ""
browserAuth, _ := cmd.Flags().GetBool("browser-auth")
hasBrowserAuth := browserAuth || viper.GetBool("BROWSER_AUTH")
hasCookieAuth := cookieAuthViaCLI || cookieAuthViaEnv || hasBrowserAuth
samlAuth, _ := cmd.Flags().GetBool("saml-auth")
hasSAMLAuth := samlAuth || viper.GetBool("SAML_AUTH")
hasCookieAuth := cookieAuthViaCLI || cookieAuthViaEnv || hasBrowserAuth || hasSAMLAuth

// URL: flag > SAP_URL env
if cfg.BaseURL == "" {
Expand Down Expand Up @@ -502,7 +522,7 @@ func processBrowserAuth(cmd *cobra.Command) error {
browserExec = viper.GetString("BROWSER_EXEC")
}

ctx := context.Background()
ctx := cmd.Context()
cookies, err := adt.BrowserLogin(ctx, cfg.BaseURL, cfg.InsecureSkipVerify, timeout, browserExec, cfg.Verbose)
if err != nil {
return fmt.Errorf("browser authentication failed: %w", err)
Expand All @@ -526,6 +546,101 @@ func processBrowserAuth(cmd *cobra.Command) error {
return nil
}

func processSAMLAuth(cmd *cobra.Command) error {
samlAuth, _ := cmd.Flags().GetBool("saml-auth")
if !samlAuth && !viper.GetBool("SAML_AUTH") {
return nil
}

if cfg.BaseURL == "" {
return fmt.Errorf("--saml-auth requires --url to be set")
}

// Resolve credential source. Priority: credential-cmd > env vars > flags.
credCmdStr, _ := cmd.Flags().GetString("credential-cmd")
if credCmdStr == "" {
credCmdStr = viper.GetString("CREDENTIAL_CMD")
if credCmdStr != "" && cfg.Verbose {
fmt.Fprintf(os.Stderr, "[SAML-AUTH] Warning: credential-cmd sourced from environment variable\n")
}
}

var credProvider adt.CredentialProvider

if credCmdStr != "" {
// Credential command mode: parse and execute external command on each auth.
credArgs := adt.ParseCredentialCmd(credCmdStr)
if len(credArgs) == 0 {
return fmt.Errorf("--credential-cmd: empty command after parsing")
}
credProvider = func(ctx context.Context) ([]byte, []byte, error) {
user, pass, err := adt.RunCredentialCmd(ctx, credArgs, cfg.Verbose)
if err != nil {
return nil, nil, err
}
return []byte(user), []byte(pass), nil
}
} else {
// Direct credentials mode: env vars > flags.
samlUser, _ := cmd.Flags().GetString("saml-user")
if samlUser == "" {
samlUser = viper.GetString("SAML_USER")
}
samlPassword, _ := cmd.Flags().GetString("saml-password")
if samlPassword == "" {
samlPassword = viper.GetString("SAML_PASSWORD")
}

if samlUser == "" || samlPassword == "" {
return fmt.Errorf("--saml-auth requires credentials: use --credential-cmd, --saml-user/--saml-password, or SAP_SAML_USER/SAP_SAML_PASSWORD env vars")
}

// Build credential provider that re-reads env vars on each call.
// This supports credential rotation and avoids long-term retention.
flagUser := samlUser
flagPassword := samlPassword
credProvider = func(ctx context.Context) ([]byte, []byte, error) {
u := os.Getenv("SAP_SAML_USER")
if u == "" {
u = flagUser
}
p := os.Getenv("SAP_SAML_PASSWORD")
if p == "" {
p = flagPassword
}
return []byte(u), []byte(p), nil
}
}

ctx := cmd.Context()
cookies, err := adt.SAMLLogin(ctx, cfg.BaseURL, credProvider, cfg.InsecureSkipVerify, cfg.Verbose)
if err != nil {
return fmt.Errorf("SAML authentication failed: %w", err)
}

cfg.Cookies = cookies

// Set re-auth function for 401 recovery.
cfg.ReauthFunc = func(ctx context.Context) (map[string]string, error) {
return adt.SAMLLogin(ctx, cfg.BaseURL, credProvider, cfg.InsecureSkipVerify, cfg.Verbose)
}

// Save cookies if requested.
cookieSave, _ := cmd.Flags().GetString("cookie-save")
if cookieSave == "" {
cookieSave = viper.GetString("COOKIE_SAVE")
}
if cookieSave != "" {
if err := adt.SaveCookiesToFile(cookies, cfg.BaseURL, cookieSave); err != nil {
fmt.Fprintf(os.Stderr, "[SAML-AUTH] Warning: failed to save cookies: %v\n", err)
} else {
fmt.Fprintf(os.Stderr, "[SAML-AUTH] Cookies saved to %s (reuse with --cookie-file)\n", cookieSave)
}
}

return nil
}

func processCookieAuth(cmd *cobra.Command) error {
cookieFile, _ := cmd.Flags().GetString("cookie-file")
cookieString, _ := cmd.Flags().GetString("cookie-string")
Expand Down Expand Up @@ -555,11 +670,11 @@ func processCookieAuth(cmd *cobra.Command) error {
}

if authMethods > 1 {
return fmt.Errorf("only one authentication method can be used at a time (basic auth, cookie-file, cookie-string, or browser-auth)")
return fmt.Errorf("only one authentication method can be used at a time (basic auth, cookie-file, cookie-string, browser-auth, or saml-auth)")
}

if authMethods == 0 {
return fmt.Errorf("authentication required. Use --user/--password, --cookie-file, --cookie-string, or --browser-auth")
return fmt.Errorf("authentication required. Use --user/--password, --cookie-file, --cookie-string, --browser-auth, or --saml-auth")
}

// If cookies already set by browser auth, we're done
Expand Down
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/oisee/vibing-steampunk

go 1.24.0

toolchain go1.24.10
go 1.25.0

require (
github.com/chromedp/cdproto v0.0.0-20250803210736-d308e07a266d
Expand All @@ -15,6 +13,7 @@ require (
github.com/spf13/viper v1.21.0
github.com/tetratelabs/wazero v1.11.0
github.com/yuin/gopher-lua v1.1.1
golang.org/x/net v0.52.0
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -38,6 +37,6 @@ require (
github.com/subosito/gotenv v1.6.0 // indirect
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/sys v0.38.0 // indirect
golang.org/x/text v0.28.0 // indirect
golang.org/x/sys v0.42.0 // indirect
golang.org/x/text v0.35.0 // indirect
)
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/net v0.52.0 h1:He/TN1l0e4mmR3QqHMT2Xab3Aj3L9qjbhRm78/6jrW0=
golang.org/x/net v0.52.0/go.mod h1:R1MAz7uMZxVMualyPXb+VaqGSa3LIaUqk0eEt3w36Sw=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng=
golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo=
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/text v0.35.0 h1:JOVx6vVDFokkpaq1AEptVzLTpDe9KGpj5tR4/X+ybL8=
golang.org/x/text v0.35.0/go.mod h1:khi/HExzZJ2pGnjenulevKNX1W67CUy0AsXcNubPGCA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
7 changes: 7 additions & 0 deletions internal/mcp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ type Config struct {
// Debugger configuration
TerminalID string // SAP GUI terminal ID for cross-tool breakpoint sharing

// ReauthFunc is called on 401 to re-authenticate (e.g., re-run SAML dance).
// Returns fresh cookies. Passed through to adt.Config.
ReauthFunc func(ctx context.Context) (map[string]string, error)

// Session keep-alive interval (0 = disabled)
// Sends periodic pings to prevent session timeout during idle periods.
// Useful for cookie/browser-auth where sessions expire server-side.
Expand Down Expand Up @@ -122,6 +126,9 @@ func NewServer(cfg *Config) *Server {
if cfg.Verbose {
opts = append(opts, adt.WithVerbose())
}
if cfg.ReauthFunc != nil {
opts = append(opts, adt.WithReauthFunc(cfg.ReauthFunc))
}

// Configure safety settings
safety := adt.UnrestrictedSafetyConfig() // Default: unrestricted for backwards compatibility
Expand Down
Loading