优化管理员重置验证输入显示
This commit is contained in:
1 parent
7ac9fa3d1a
commit
bbb899b5bd
7 files changed
+32
-22
No files matched your search
@@ -30,7 +30,7 @@ func ReadProtectedPassword() (string, error) {
|
||||
}
|
||||
|
||||
func askPassword(prompt string) (string, error) {
|
||||
value, err := ReadProtectedInput(prompt)
|
||||
value, err := readSystemdInput(prompt, "no")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -40,8 +40,12 @@ func askPassword(prompt string) (string, error) {
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func ReadProtectedInput(prompt string) (string, error) {
|
||||
command := exec.Command("systemd-ask-password", "--no-tty", prompt)
|
||||
func ReadVisibleInput(prompt string) (string, error) {
|
||||
return readSystemdInput(prompt, "yes")
|
||||
}
|
||||
|
||||
func readSystemdInput(prompt, echo string) (string, error) {
|
||||
command := exec.Command("systemd-ask-password", "--no-tty", "--echo="+echo, prompt)
|
||||
command.Stderr = io.Discard
|
||||
output, err := command.Output()
|
||||
if err != nil {
|
||||
|
||||
@@ -2,5 +2,5 @@
|
||||
|
||||
package adminreset
|
||||
|
||||
func ReadProtectedPassword() (string, error) { return "", ErrUnsupported }
|
||||
func ReadProtectedInput(string) (string, error) { return "", ErrUnsupported }
|
||||
func ReadProtectedPassword() (string, error) { return "", ErrUnsupported }
|
||||
func ReadVisibleInput(string) (string, error) { return "", ErrUnsupported }
|
||||
@@ -3,9 +3,11 @@
|
||||
package adminreset
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/utils/password"
|
||||
|
||||
@@ -35,9 +37,18 @@ func ReadProtectedPassword() (string, error) {
|
||||
}
|
||||
|
||||
func readWindowsPassword(prompt string) (string, error) {
|
||||
value, err := ReadProtectedInput(prompt)
|
||||
if !term.IsTerminal(int(os.Stdin.Fd())) {
|
||||
return "", errors.New("受保护的密码输入需要交互式控制台")
|
||||
}
|
||||
fmt.Fprint(os.Stderr, prompt+": ")
|
||||
valueBytes, err := term.ReadPassword(int(os.Stdin.Fd()))
|
||||
fmt.Fprintln(os.Stderr)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", errors.New("受保护的密码输入失败")
|
||||
}
|
||||
value := string(valueBytes)
|
||||
for index := range valueBytes {
|
||||
valueBytes[index] = 0
|
||||
}
|
||||
if password.ValidateUserPassword(value) != nil {
|
||||
return "", errors.New("密码长度必须为 8 到 12 个字符")
|
||||
@@ -45,19 +56,14 @@ func readWindowsPassword(prompt string) (string, error) {
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func ReadProtectedInput(prompt string) (string, error) {
|
||||
func ReadVisibleInput(prompt string) (string, error) {
|
||||
if !term.IsTerminal(int(os.Stdin.Fd())) {
|
||||
return "", errors.New("安全验证需要交互式控制台")
|
||||
}
|
||||
fmt.Fprint(os.Stderr, prompt+": ")
|
||||
value, err := term.ReadPassword(int(os.Stdin.Fd()))
|
||||
fmt.Fprintln(os.Stderr)
|
||||
value, err := bufio.NewReader(os.Stdin).ReadString('\n')
|
||||
if err != nil {
|
||||
return "", errors.New("受保护的密码输入失败")
|
||||
return "", errors.New("读取安全验证输入失败")
|
||||
}
|
||||
normalized := string(value)
|
||||
for i := range value {
|
||||
value[i] = 0
|
||||
}
|
||||
return normalized, nil
|
||||
return strings.TrimSuffix(strings.TrimSuffix(value, "\n"), "\r"), nil
|
||||
}
|
||||
Reference in new issue
Block a user