-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetpass.go
68 lines (60 loc) · 1.8 KB
/
getpass.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
package main
import (
"errors"
"fmt"
"log"
"strings"
zxcvbn "github.com/nbutton23/zxcvbn-go"
"golang.org/x/crypto/ssh/terminal"
)
const (
maxNewPasswordTries = 10
)
// PromptNewPassword prompts the user for a new vault password.
// The user is requird to type the password a second time for confirmation,
// and the password must meet minimum entropy.
// If unsure, a value of 50.0 is reasonable for a moderately-strong password.
func promptNewPassword(prompt string, minEntropy float64) (string, error) {
for i := 0; i < maxNewPasswordTries; i++ {
password, err := terminalGetSecret(prompt)
if err != nil {
return "", fmt.Errorf("Input error: %v", err)
}
if len(password) == 0 || zxcvbn.PasswordStrength(password, nil).Entropy < minEntropy {
log.Printf("Password weak - please try again\n\n")
continue
}
confirm, err := terminalGetSecret("Confirm password:")
if err != nil {
log.Printf("Input error: please try again\n\n")
continue
}
if password != confirm {
log.Printf("Passwords did not match - please try again\n\n")
continue
}
return password, nil
}
return "", errors.New("Too many tries. Please try again later")
}
// promptPassword asks the user to enter a password.
// This can be used to ask for a password for an existing vault.
func promptPassword() (string, error) {
password, err := terminalGetSecret("Enter vault password:")
if err != nil {
return "", fmt.Errorf("Input error: %v", err)
}
return password, nil
}
// terminalGetSecret - ask user for password or secret key.
// Typed entry is not echoed to terminal.
func terminalGetSecret(prompt string) (string, error) {
fmt.Print(prompt)
bytePassword, err := terminal.ReadPassword(0)
fmt.Println()
if err != nil {
return "", err
}
password := string(bytePassword)
return strings.TrimSpace(password), nil
}