diff --git a/cmd/keygen.go b/cmd/keygen.go new file mode 100644 index 000000000..9a4faa940 --- /dev/null +++ b/cmd/keygen.go @@ -0,0 +1,28 @@ +// Copyright 2021 Molecula Corp. All rights reserved. +package cmd + +import ( + "context" + "io" + + "github.com/molecula/featurebase/v2/ctl" + "github.com/spf13/cobra" +) + +func newKeygenCommand(stdin io.Reader, stdout io.Writer, stderr io.Writer) *cobra.Command { + cmd := ctl.NewKeygenCommand(stdin, stdout, stderr) + ccmd := &cobra.Command{ + Use: "keygen", + Short: "Generate keys for authentication.", + Long: ` +Generate hash and block keys to configure FeatureBase for Authentication. +`, + RunE: func(c *cobra.Command, args []string) error { + return cmd.Run(context.Background()) + }, + } + + flags := ccmd.Flags() + flags.IntVarP(&cmd.KeyLength, "length", "l", 32, "length of keys to produce") + return ccmd +} diff --git a/cmd/root.go b/cmd/root.go index 4ea6a30e0..c164bed97 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -6,7 +6,7 @@ import ( "io" "strings" - "github.com/molecula/featurebase/v2" + pilosa "github.com/molecula/featurebase/v2" "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/spf13/viper" @@ -62,6 +62,8 @@ at https://docs.molecula.cloud/. rc.AddCommand(newRBFCommand(stdin, stdout, stderr)) rc.AddCommand(newServeCmd(stdin, stdout, stderr)) rc.AddCommand(newHolderCmd(stdin, stdout, stderr)) + rc.AddCommand(newHolderCmd(stdin, stdout, stderr)) + rc.AddCommand(newKeygenCommand(stdin, stdout, stderr)) rc.SetOutput(stderr) return rc diff --git a/ctl/keygen.go b/ctl/keygen.go new file mode 100644 index 000000000..06cc797ad --- /dev/null +++ b/ctl/keygen.go @@ -0,0 +1,31 @@ +// Copyright 2021 Molecula Corp. All rights reserved. +package ctl + +import ( + "context" + "fmt" + "io" + + "github.com/gorilla/securecookie" + pilosa "github.com/molecula/featurebase/v2" +) + +// Keygen represents a command for generating crytographic keys. +type KeygenCommand struct { + CmdIO *pilosa.CmdIO + KeyLength int +} + +// NewKeygen returns a new instance of Keygen. +func NewKeygenCommand(stdin io.Reader, stdout, stderr io.Writer) *KeygenCommand { + return &KeygenCommand{ + CmdIO: pilosa.NewCmdIO(stdin, stdout, stderr), + } +} + +// Run keys to use for authentication . +func (kg *KeygenCommand) Run(_ context.Context) error { + fmt.Printf("hash-key = \"%+x\"\n", securecookie.GenerateRandomKey(kg.KeyLength)) + fmt.Printf("block-key = \"%+x\"\n", securecookie.GenerateRandomKey(kg.KeyLength)) + return nil +}