Skip to content

Commit

Permalink
Update example code to call Transaction.End
Browse files Browse the repository at this point in the history
  • Loading branch information
msteinert committed Nov 30, 2023
1 parent 4ce1d8a commit eb52470
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ import (
// 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) {
t, err := pam.StartFunc("passwd", "", func(s pam.Style, msg string) (string, error) {
switch s {
case pam.PromptEchoOff:
fmt.Print(msg)
fmt.Printf("%s", msg)
pw, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", err
}
fmt.Println()
return string(pw), nil
case pam.PromptEchoOn:
fmt.Print(msg)
fmt.Printf("%s ", msg)
s := bufio.NewScanner(os.Stdin)
s.Scan()
return s.Text(), nil
Expand All @@ -40,12 +40,19 @@ func Example() {
}
})
if err != nil {
fmt.Fprintf(os.Stderr, "start: %s\n", err.Error())
fmt.Fprintf(os.Stderr, "start: %v\n", err)
os.Exit(1)
}
defer func() {
err := t.End()
if err != nil {
fmt.Fprintf(os.Stderr, "end: %v\n", err)
os.Exit(1)
}
}()
err = t.Authenticate(0)
if err != nil {
fmt.Fprintf(os.Stderr, "authenticate: %s\n", err.Error())
fmt.Fprintf(os.Stderr, "authenticate: %v\n", err)
os.Exit(1)
}
fmt.Println("authentication succeeded!")
Expand Down

0 comments on commit eb52470

Please sign in to comment.