summaryrefslogtreecommitdiff
path: root/example_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'example_test.go')
-rw-r--r--example_test.go45
1 files changed, 22 insertions, 23 deletions
diff --git a/example_test.go b/example_test.go
index aca24a1..8a347a6 100644
--- a/example_test.go
+++ b/example_test.go
@@ -4,50 +4,49 @@ import (
"bufio"
"errors"
"fmt"
- "log"
"os"
- "github.com/bgentry/speakeasy"
"github.com/msteinert/pam"
+ "golang.org/x/term"
)
-// This example uses whatever default PAM service configuration is available
-// on the system, and tries to authenticate any user. This should cause PAM
-// to ask its conversation handler for a username and password, in sequence.
-//
-// This application will handle those requests by displaying the
-// PAM-provided prompt and sending back the first line of stdin input
-// it can read for each.
-//
-// Keep in mind that unless run as root (or setuid root), the only
-// user's authentication that can succeed is that of the process owner.
-func Example_authenticate() {
+// This example uses the default PAM service to authenticate any users. This
+// should cause PAM to ask its conversation handler for a username and password
+// in sequence.
+func Example() {
t, err := pam.StartFunc("", "", func(s pam.Style, msg string) (string, error) {
switch s {
case pam.PromptEchoOff:
- return speakeasy.Ask(msg)
- case pam.PromptEchoOn:
- fmt.Print(msg + " ")
- input, err := bufio.NewReader(os.Stdin).ReadString('\n')
+ fmt.Print(msg)
+ pw, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", err
}
- return input[:len(input)-1], nil
+ fmt.Println()
+ return string(pw), nil
+ case pam.PromptEchoOn:
+ fmt.Print(msg)
+ s := bufio.NewScanner(os.Stdin)
+ s.Scan()
+ return s.Text(), nil
case pam.ErrorMsg:
- log.Print(msg)
+ fmt.Fprintf(os.Stderr, "%s\n", msg)
return "", nil
case pam.TextInfo:
fmt.Println(msg)
return "", nil
+ default:
+ return "", errors.New("unrecognized message style")
}
- return "", errors.New("Unrecognized message style")
})
if err != nil {
- log.Fatalf("Start: %s", err.Error())
+ fmt.Fprintf(os.Stderr, "start: %s\n", err.Error())
+ os.Exit(1)
}
err = t.Authenticate(0)
if err != nil {
- log.Fatalf("Authenticate: %s", err.Error())
+ fmt.Fprintf(os.Stderr, "authenticate: %s\n", err.Error())
+ os.Exit(1)
}
- fmt.Println("Authentication succeeded!")
+ fmt.Println("authentication succeeded!")
}