Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
add headless auth
Antonio Ojeac2c16fad3168
diff --git a/.github/k8s/dex-config.yaml b/.github/k8s/dex-config.yamlindex cc6cba22..d5e976d5 100644--- a/.github/k8s/dex-config.yaml+++ b/.github/k8s/dex-config.yaml@@ -48,3 +48,4 @@ data:redirectURIs:- 'http://127.0.0.1/callback'- 'http://localhost/callback'+ - 'urn:ietf:wg:oauth:2.0:oob'diff --git a/cmd/sam-node/main.go b/cmd/sam-node/main.goindex 8e98fe17..8954cd14 100644--- a/cmd/sam-node/main.go+++ b/cmd/sam-node/main.go@@ -71,6 +71,7 @@ var (keyGracePeriodFlag time.DurationdataDirFlag stringallowLoopbackFlag bool+ headlessFlag boolapiTokenFlag stringtlsCertFlag string@@ -421,6 +422,7 @@ func main() {rootCmd.PersistentFlags().StringVar(&deviceAuthURLFlag, "device-auth-url", "", "OIDC Device Authorization URL")rootCmd.PersistentFlags().StringVar(&audienceFlag, "audience", api.DefaultAudience, "OIDC Audience")rootCmd.PersistentFlags().StringVar(&dataDirFlag, "data-dir", os.Getenv("SAM_DATA_DIR"), "Override directory for the agent store (defaults to OS user config dir)")+ rootCmd.PersistentFlags().BoolVar(&headlessFlag, "headless", false, "Force headless out-of-band (OOB) authentication flow")rootCmd.AddCommand(runCmd)rootCmd.AddCommand(joinCmd)diff --git a/cmd/sam-node/oidc.go b/cmd/sam-node/oidc.goindex e6e5bbb1..a2ed99f1 100644--- a/cmd/sam-node/oidc.go+++ b/cmd/sam-node/oidc.go@@ -25,6 +25,7 @@ import ("net""net/http""net/url"+ "os""os/exec""runtime""strings"@@ -85,14 +86,24 @@ func (n *SamNode) InteractiveLogin(ctx context.Context, authURL, tokenURL, clienreturn "", fmt.Errorf("failed to generate PKCE: %w", err)}- listener, err := net.Listen("tcp", "127.0.0.1:0")- if err != nil {- return "", fmt.Errorf("failed to start local server: %w", err)- }- defer func() { _ = listener.Close() }()+ isHeadless := headlessFlag || os.Getenv("SSH_CLIENT") != "" || os.Getenv("SSH_TTY") != ""++ var redirectURI string+ var listener net.Listener++ if isHeadless {+ redirectURI = "urn:ietf:wg:oauth:2.0:oob"+ } else {+ var err error+ listener, err = net.Listen("tcp", "127.0.0.1:0")+ if err != nil {+ return "", fmt.Errorf("failed to start local server: %w", err)+ }+ defer func() { _ = listener.Close() }()- port := listener.Addr().(*net.TCPAddr).Port- redirectURI := fmt.Sprintf("http://127.0.0.1:%d/callback", port)+ port := listener.Addr().(*net.TCPAddr).Port+ redirectURI = fmt.Sprintf("http://127.0.0.1:%d/callback", port)+ }authReq, err := http.NewRequest("GET", authURL, nil)if err != nil {@@ -117,7 +128,11 @@ func (n *SamNode) InteractiveLogin(ctx context.Context, authURL, tokenURL, clienfmt.Println("------------------------------------------------------------")fmt.Printf("To authenticate, please go to the following URL in your browser:\n\n")fmt.Printf(" %s\n\n", targetURL)- fmt.Println("Waiting for authorization...")+ if isHeadless {+ fmt.Println("After authorizing, paste the authorization code below:")+ } else {+ fmt.Println("Waiting for authorization... (If your browser fails, you can paste the callback URL or code here)")+ }fmt.Println("------------------------------------------------------------")// Try to open browser automatically@@ -126,62 +141,93 @@ func (n *SamNode) InteractiveLogin(ctx context.Context, authURL, tokenURL, cliencodeChan := make(chan string)errChan := make(chan error)- mux := http.NewServeMux()- mux.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {- query := r.URL.Query()- if query.Get("state") != state {- http.Error(w, "Invalid state parameter", http.StatusBadRequest)- select {- case errChan <- fmt.Errorf("invalid state parameter received"):- case <-ctx.Done():+ var srv *http.Server+ if !isHeadless {+ mux := http.NewServeMux()+ mux.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {+ query := r.URL.Query()+ if query.Get("state") != state {+ http.Error(w, "Invalid state parameter", http.StatusBadRequest)+ select {+ case errChan <- fmt.Errorf("invalid state parameter received"):+ case <-ctx.Done():+ }+ return}- return- }- if errStr := query.Get("error"); errStr != "" {- desc := query.Get("error_description")- http.Error(w, "Authorization failed: "+errStr, http.StatusBadRequest)+ if errStr := query.Get("error"); errStr != "" {+ desc := query.Get("error_description")+ http.Error(w, "Authorization failed: "+errStr, http.StatusBadRequest)+ select {+ case errChan <- fmt.Errorf("authorization failed: %s - %s", errStr, desc):+ case <-ctx.Done():+ }+ return+ }+ code := query.Get("code")+ if code == "" {+ http.Error(w, "No code in request", http.StatusBadRequest)+ select {+ case errChan <- fmt.Errorf("no code received"):+ case <-ctx.Done():+ }+ return+ }++ w.Header().Set("Content-Type", "text/html")+ _, _ = w.Write([]byte("<html><body><h1>Authorization successful!</h1><p>You can close this window and return to the CLI.</p></body></html>"))+select {- case errChan <- fmt.Errorf("authorization failed: %s - %s", errStr, desc):+ case codeChan <- code:case <-ctx.Done():}+ })++ srv = &http.Server{Handler: mux}+ go func() {+ if err := srv.Serve(listener); err != nil && err != http.ErrServerClosed {+ select {+ case errChan <- fmt.Errorf("local server error: %w", err):+ case <-ctx.Done():+ }+ }+ }()+ }++ // Also allow manual code entry via stdin+ go func() {+ var input string+ if _, err := fmt.Scanln(&input); err != nil {return}- code := query.Get("code")- if code == "" {- http.Error(w, "No code in request", http.StatusBadRequest)- select {- case errChan <- fmt.Errorf("no code received"):- case <-ctx.Done():- }+ input = strings.TrimSpace(input)+ if input == "" {return}-- w.Header().Set("Content-Type", "text/html")- _, _ = w.Write([]byte("<html><body><h1>Authorization successful!</h1><p>You can close this window and return to the CLI.</p></body></html>"))-+ if parsed, err := url.Parse(input); err == nil && parsed.Query().Get("code") != "" {+ input = parsed.Query().Get("code")+ }select {- case codeChan <- code:+ case codeChan <- input:case <-ctx.Done():}- })-- srv := &http.Server{Handler: mux}- go func() {- if err := srv.Serve(listener); err != nil && err != http.ErrServerClosed {- errChan <- fmt.Errorf("local server error: %w", err)- }}()var code stringselect {case <-ctx.Done():- _ = srv.Close()+ if srv != nil {+ _ = srv.Close()+ }return "", ctx.Err()case err := <-errChan:- _ = srv.Close()+ if srv != nil {+ _ = srv.Close()+ }return "", errcase code = <-codeChan:- _ = srv.Close()+ if srv != nil {+ _ = srv.Close()+ }}tokenData := url.Values{}