-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: In the gnokey CLI, add command to update the password
Signed-off-by: Jeff Thompson <jeff@thefirst.org>
- Loading branch information
Showing
5 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
|
||
"github.com/gnolang/gno/tm2/pkg/commands" | ||
"github.com/gnolang/gno/tm2/pkg/crypto/keys" | ||
) | ||
|
||
type UpdateCfg struct { | ||
RootCfg *BaseCfg | ||
|
||
Force bool | ||
} | ||
|
||
func NewUpdateCmd(rootCfg *BaseCfg, io commands.IO) *commands.Command { | ||
cfg := &UpdateCfg{ | ||
RootCfg: rootCfg, | ||
} | ||
|
||
return commands.NewCommand( | ||
commands.Metadata{ | ||
Name: "update", | ||
ShortUsage: "update [flags] <key-name>", | ||
ShortHelp: "update the password of a key in the keybase", | ||
}, | ||
cfg, | ||
func(_ context.Context, args []string) error { | ||
return execUpdate(cfg, args, io) | ||
}, | ||
) | ||
} | ||
|
||
func (c *UpdateCfg) RegisterFlags(fs *flag.FlagSet) { | ||
} | ||
|
||
func execUpdate(cfg *UpdateCfg, args []string, io commands.IO) error { | ||
if len(args) != 1 { | ||
return flag.ErrHelp | ||
} | ||
|
||
nameOrBech32 := args[0] | ||
|
||
kb, err := keys.NewKeyBaseFromDir(cfg.RootCfg.Home) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
oldpass, err := io.GetPassword("Enter the current password:", cfg.RootCfg.InsecurePasswordStdin) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
newpass, err := io.GetCheckPassword( | ||
[2]string{ | ||
"Enter the new password to encrypt your key to disk:", | ||
"Repeat the password:", | ||
}, | ||
cfg.RootCfg.InsecurePasswordStdin, | ||
) | ||
if err != nil { | ||
return fmt.Errorf("unable to parse provided password, %w", err) | ||
} | ||
|
||
getNewpass := func() (string, error) { return newpass, nil } | ||
err = kb.Update(nameOrBech32, oldpass, getNewpass) | ||
if err != nil { | ||
return err | ||
} | ||
io.ErrPrintln("Password updated") | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package client | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/gnolang/gno/tm2/pkg/commands" | ||
"github.com/gnolang/gno/tm2/pkg/crypto/keys" | ||
"github.com/gnolang/gno/tm2/pkg/testutils" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_execUpdate(t *testing.T) { | ||
t.Parallel() | ||
|
||
// make new test dir | ||
kbHome, kbCleanUp := testutils.NewTestCaseDir(t) | ||
defer kbCleanUp() | ||
|
||
// initialize test options | ||
cfg := &UpdateCfg{ | ||
RootCfg: &BaseCfg{ | ||
BaseOptions: BaseOptions{ | ||
Home: kbHome, | ||
InsecurePasswordStdin: true, | ||
}, | ||
}, | ||
} | ||
|
||
io := commands.NewTestIO() | ||
|
||
// Add test accounts to keybase. | ||
kb, err := keys.NewKeyBaseFromDir(kbHome) | ||
assert.NoError(t, err) | ||
|
||
keyName := "updateApp_Key1" | ||
p1, p2 := "1234", "foobar" | ||
mnemonic := "equip will roof matter pink blind book anxiety banner elbow sun young" | ||
|
||
_, err = kb.CreateAccount(keyName, mnemonic, "", p1, 0, 0) | ||
assert.NoError(t, err) | ||
|
||
{ | ||
// test: Key not found | ||
args := []string{"blah"} | ||
io.SetIn(strings.NewReader(p1 + "\n" + p2 + "\n" + p2 + "\n")) | ||
err = execUpdate(cfg, args, io) | ||
require.Error(t, err) | ||
require.Equal(t, "Key blah not found", err.Error()) | ||
} | ||
|
||
{ | ||
// test: Wrong password | ||
args := []string{keyName} | ||
io.SetIn(strings.NewReader("blah" + "\n" + p2 + "\n" + p2 + "\n")) | ||
err = execUpdate(cfg, args, io) | ||
require.Error(t, err) | ||
require.Equal(t, "invalid account password", err.Error()) | ||
} | ||
|
||
{ | ||
// test: New passwords don't match | ||
args := []string{keyName} | ||
io.SetIn(strings.NewReader(p1 + "\n" + p2 + "\n" + "blah" + "\n")) | ||
err = execUpdate(cfg, args, io) | ||
require.Error(t, err) | ||
require.Equal(t, "unable to parse provided password, passphrases don't match", err.Error()) | ||
} | ||
|
||
{ | ||
// Update the password | ||
args := []string{keyName} | ||
io.SetIn(strings.NewReader(p1 + "\n" + p2 + "\n" + p2 + "\n")) | ||
err = execUpdate(cfg, args, io) | ||
require.NoError(t, err) | ||
} | ||
|
||
{ | ||
// test: The old password shouldn't work | ||
args := []string{keyName} | ||
io.SetIn(strings.NewReader(p1 + "\n" + p1 + "\n" + p1 + "\n")) | ||
err = execUpdate(cfg, args, io) | ||
require.Error(t, err) | ||
require.Equal(t, "invalid account password", err.Error()) | ||
} | ||
|
||
{ | ||
// Updating the new password to itself should work | ||
args := []string{keyName} | ||
io.SetIn(strings.NewReader(p2 + "\n" + p2 + "\n" + p2 + "\n")) | ||
err = execUpdate(cfg, args, io) | ||
require.NoError(t, err) | ||
} | ||
} |