-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
key_store_authenticator.go
80 lines (70 loc) · 2.1 KB
/
key_store_authenticator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package cmd
import (
"fmt"
"strings"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)
// TerminalKeyStoreAuthenticator contains fields for prompting the user and an
// exit code.
type TerminalKeyStoreAuthenticator struct {
Prompter Prompter
}
type keystorePassword interface {
Keystore() string
}
func (auth TerminalKeyStoreAuthenticator) authenticate(keyStore keystore.Master, password keystorePassword) error {
isEmpty, err := keyStore.IsEmpty()
if err != nil {
return errors.Wrap(err, "error determining if keystore is empty")
}
pw := password.Keystore()
if len(pw) != 0 {
// Because we changed password requirements to increase complexity, to
// not break backward compatibility we enforce this only for empty key
// stores.
if err = auth.validatePasswordStrength(pw); err != nil && isEmpty {
return err
}
return keyStore.Unlock(pw)
}
interactive := auth.Prompter.IsTerminal()
if !interactive {
return errors.New("no password provided")
} else if !isEmpty {
pw = auth.promptExistingPassword()
} else {
pw, err = auth.promptNewPassword()
}
if err != nil {
return err
}
return keyStore.Unlock(pw)
}
func (auth TerminalKeyStoreAuthenticator) validatePasswordStrength(password string) error {
return utils.VerifyPasswordComplexity(password)
}
func (auth TerminalKeyStoreAuthenticator) promptExistingPassword() string {
password := auth.Prompter.PasswordPrompt("Enter key store password:")
return password
}
func (auth TerminalKeyStoreAuthenticator) promptNewPassword() (string, error) {
for {
password := auth.Prompter.PasswordPrompt("New key store password: ")
if err := auth.validatePasswordStrength(password); err != nil {
return "", err
}
if strings.TrimSpace(password) != password {
return "", utils.ErrPasswordWhitespace
}
clearLine()
passwordConfirmation := auth.Prompter.PasswordPrompt("Confirm password: ")
clearLine()
if password != passwordConfirmation {
fmt.Printf("Passwords don't match. Please try again... ")
continue
}
return password, nil
}
}