-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.go
43 lines (36 loc) · 860 Bytes
/
auth.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
package main
import (
"fmt"
"golang.org/x/crypto/ssh/terminal"
"log"
"os"
"os/signal"
)
// https://gist.github.com/jlinoff/e8e26b4ffa38d379c7f1891fd174a6d0#file-getpassword2-go
// GetPassword provides a prompt to input a password in hidden form
func GetPassword(prompt string) (string, error) {
// Get the initial state of the terminal.
fd := int(os.Stdin.Fd())
initialTermState, e1 := terminal.GetState(fd)
if e1 != nil {
return "", e1
}
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, os.Kill)
go func() {
<-c
_ = terminal.Restore(fd, initialTermState)
log.Println("Connection Closed")
os.Exit(1)
}()
// Now get the password.
fmt.Print(prompt)
p, err := terminal.ReadPassword(fd)
fmt.Println("")
if err != nil {
return "", nil
}
// Stop looking for ^C on the channel.
signal.Stop(c)
return string(p), nil
}