pam.go 774 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // +build pam
  2. // Copyright 2015 The Gogs Authors. All rights reserved.
  3. // Copyright 2018 Gitote. All rights reserved.
  4. //
  5. // This source code is licensed under the MIT license found in the
  6. // LICENSE file in the root directory of this source tree.
  7. package pam
  8. import (
  9. "errors"
  10. "github.com/msteinert/pam"
  11. )
  12. func PAMAuth(serviceName, userName, passwd string) error {
  13. t, err := pam.StartFunc(serviceName, userName, func(s pam.Style, msg string) (string, error) {
  14. switch s {
  15. case pam.PromptEchoOff:
  16. return passwd, nil
  17. case pam.PromptEchoOn, pam.ErrorMsg, pam.TextInfo:
  18. return "", nil
  19. }
  20. return "", errors.New("Unrecognized PAM message style")
  21. })
  22. if err != nil {
  23. return err
  24. }
  25. if err = t.Authenticate(0); err != nil {
  26. return err
  27. }
  28. return nil
  29. }