pam.go 818 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // +build pam
  2. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  3. // Copyright 2018 - Present, 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. // Auth pam auth service
  13. func Auth(serviceName, userName, passwd string) error {
  14. t, err := pam.StartFunc(serviceName, userName, func(s pam.Style, msg string) (string, error) {
  15. switch s {
  16. case pam.PromptEchoOff:
  17. return passwd, nil
  18. case pam.PromptEchoOn, pam.ErrorMsg, pam.TextInfo:
  19. return "", nil
  20. }
  21. return "", errors.New("Unrecognized PAM message style")
  22. })
  23. if err != nil {
  24. return err
  25. }
  26. if err = t.Authenticate(0); err != nil {
  27. return err
  28. }
  29. return nil
  30. }