File: example_test.go

package info (click to toggle)
golang-github-msteinert-pam 0.0~git20170830.0.f4cd9f5-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 100 kB
  • sloc: ansic: 44; makefile: 2
file content (53 lines) | stat: -rw-r--r-- 1,404 bytes parent folder | download | duplicates (3)
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
package pam_test

import (
	"bufio"
	"errors"
	"fmt"
	"log"
	"os"

	"github.com/bgentry/speakeasy"
	"github.com/msteinert/pam"
)

// 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() {
	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')
			if err != nil {
				return "", err
			}
			return input[:len(input)-1], nil
		case pam.ErrorMsg:
			log.Print(msg)
			return "", nil
		case pam.TextInfo:
			fmt.Println(msg)
			return "", nil
		}
		return "", errors.New("Unrecognized message style")
	})
	if err != nil {
		log.Fatalf("Start: %s", err.Error())
	}
	err = t.Authenticate(0)
	if err != nil {
		log.Fatalf("Authenticate: %s", err.Error())
	}
	fmt.Println("Authentication succeeded!")
}