Skip to content

Commit

Permalink
fix: make sure password is set before validate repo
Browse files Browse the repository at this point in the history
  • Loading branch information
vimiix committed Jul 10, 2024
1 parent 79d19fa commit ca3b05b
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions ssx/ssx.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ func NewSSX(opt *CmdOption) (*SSX, error) {
return nil, err
}

if err := ssx.MakeSurePasswordIsSet(); err != nil {
return nil, err
}
validate, err := ssx.ValidateRepo()
if err != nil {
return nil, err
Expand All @@ -95,12 +98,16 @@ var (
Password = []byte("password")
)

func (s *SSX) ChallengeDBPassword() error {
func (s *SSX) MakeSurePasswordIsSet() error {
v, err := s.repo.GetMetadata(Password)
if err != nil {
return err
}

if len(v) > 0 {
return nil
}
lg.Warn("db password not set, please supplement it")
// not set password, fill it
prompt := promptui.Prompt{
Label: "Input db password",
Mask: '*',
Expand All @@ -111,22 +118,36 @@ func (s *SSX) ChallengeDBPassword() error {
return nil
},
}
if len(v) == 0 {
lg.Warn("db password not set, please supplement it")
// not set password, fill it
password, err := prompt.Run()
if err != nil {
return err
}
encrypt := utils.HashWithSHA256(password)
return s.repo.SetMetadata(Password, []byte(encrypt))
password, err := prompt.Run()
if err != nil {
return err
}
encrypt := utils.HashWithSHA256(password)
return s.repo.SetMetadata(Password, []byte(encrypt))
}

func (s *SSX) ChallengeDBPassword() error {
v, err := s.repo.GetMetadata(Password)
if err != nil {
return err
}

prompt := promptui.Prompt{
Label: "Input db password",
Mask: '*',
Validate: func(s string) error {
if len(s) == 0 {
return errors.New("password can't be empty")
}
return nil
},
}
password, err := prompt.Run()
if err != nil {
return err
}
encrypt := utils.HashWithSHA256(password)
// v is not empty, we have ensure it in MakeSurePasswordIsSet
if encrypt == string(v) {
return s.updateDeviceID()
}
Expand Down

0 comments on commit ca3b05b

Please sign in to comment.