-
Notifications
You must be signed in to change notification settings - Fork 0
/
askpass.go
55 lines (46 loc) · 1.19 KB
/
askpass.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
// askpass.go -- Interactive password prompt
//
// (c) 2016 Sudhi Herle <[email protected]>
//
// # Placed in the Public Domain
//
// This software does not come with any express or implied
// warranty; it is provided "as is". No claim is made to its
// suitability for any purpose.
package utils
import (
"fmt"
"syscall"
"golang.org/x/crypto/ssh/terminal"
)
// Askpass prompts user for an interactive password.
// If verify is true, confirm a second time.
// Mistakes during confirmation cause the process to restart upto a
// maximum of 2 times.
func Askpass(prompt string, verify bool) (string, error) {
for i := 0; i < 2; i++ {
fmt.Printf("%s: ", prompt)
pw1, err := terminal.ReadPassword(int(syscall.Stdin))
fmt.Printf("\n")
if err != nil {
return "", err
}
if !verify {
return string(pw1), nil
}
fmt.Printf("%s again: ", prompt)
pw2, err := terminal.ReadPassword(int(syscall.Stdin))
fmt.Printf("\n")
if err != nil {
return "", err
}
a := string(pw1)
b := string(pw2)
if a == b {
return a, nil
}
fmt.Printf("** password mismatch; try again ..\n")
}
return "", fmt.Errorf("Too many tries getting password")
}
// vim: ft=go:sw=8:ts=8:noexpandtab:tw=98: