From b0f3afc02acf3b2024475c8e679d0cdcc983b15d Mon Sep 17 00:00:00 2001 From: Javad Date: Sun, 30 Jun 2024 13:31:31 +0330 Subject: [PATCH 1/2] fix: pactus shell support basic auth --- cmd/shell/main.go | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/cmd/shell/main.go b/cmd/shell/main.go index 04ac9f95a..a10f56455 100644 --- a/cmd/shell/main.go +++ b/cmd/shell/main.go @@ -1,16 +1,15 @@ package main import ( - "context" - + "encoding/base64" + "fmt" "github.com/NathanBaulch/protoc-gen-cobra/client" "github.com/NathanBaulch/protoc-gen-cobra/naming" "github.com/pactus-project/pactus/cmd" - "github.com/pactus-project/pactus/www/grpc/basicauth" pb "github.com/pactus-project/pactus/www/grpc/gen/go" "github.com/spf13/cobra" "github.com/spf13/pflag" - "google.golang.org/grpc" + "google.golang.org/grpc/metadata" ) const ( @@ -19,23 +18,20 @@ const ( ) func main() { + var ( + username string + password string + ) + rootCmd := &cobra.Command{ Use: "shell", Short: "Pactus Shell", Long: `pactus-shell is a command line tool for interacting with the Pactus blockchain using gRPC`, } - auth := &basicauth.BasicAuth{} - client.RegisterFlagBinder(func(fs *pflag.FlagSet, namer naming.Namer) { - fs.StringVar(&auth.Username, namer("auth-username"), "", "username for gRPC basic authentication") - fs.StringVar(&auth.Password, namer("auth-password"), "", "password for gRPC basic authentication") - }) - - client.RegisterPreDialer(func(_ context.Context, opts *[]grpc.DialOption) error { - *opts = append(*opts, grpc.WithPerRPCCredentials(auth)) - - return nil + fs.StringVar(&username, namer("auth-username"), "", "username for gRPC basic authentication") + fs.StringVar(&password, namer("auth-password"), "", "password for gRPC basic authentication") }) changeDefaultParameters := func(c *cobra.Command) *cobra.Command { @@ -48,6 +44,16 @@ func main() { return c } + rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { + if username != "" && password != "" { + auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password))) + md := metadata.Pairs("authorization", "Basic "+auth) + ctx := metadata.NewOutgoingContext(cmd.Context(), md) + cmd.SetContext(ctx) + } + return nil + } + rootCmd.AddCommand(changeDefaultParameters(pb.BlockchainClientCommand())) rootCmd.AddCommand(changeDefaultParameters(pb.NetworkClientCommand())) rootCmd.AddCommand(changeDefaultParameters(pb.TransactionClientCommand())) From 1514eed24368d08ef8a81d24096f413f758690a1 Mon Sep 17 00:00:00 2001 From: Javad Date: Sun, 30 Jun 2024 13:34:41 +0330 Subject: [PATCH 2/2] fix: lint and fmt error --- cmd/shell/main.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cmd/shell/main.go b/cmd/shell/main.go index a10f56455..7bf5352ff 100644 --- a/cmd/shell/main.go +++ b/cmd/shell/main.go @@ -3,6 +3,7 @@ package main import ( "encoding/base64" "fmt" + "github.com/NathanBaulch/protoc-gen-cobra/client" "github.com/NathanBaulch/protoc-gen-cobra/naming" "github.com/pactus-project/pactus/cmd" @@ -44,13 +45,14 @@ func main() { return c } - rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { + rootCmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error { if username != "" && password != "" { auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password))) md := metadata.Pairs("authorization", "Basic "+auth) ctx := metadata.NewOutgoingContext(cmd.Context(), md) cmd.SetContext(ctx) } + return nil }