SimplePasswordMgr.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package zsshrpc_server
  2. import (
  3. "errors"
  4. "golang.org/x/crypto/bcrypt"
  5. "golang.org/x/crypto/ssh"
  6. )
  7. type SimplePasswordMgr struct {
  8. authlist map[string][]byte
  9. }
  10. func NewSimplePasswordMgr() *SimplePasswordMgr {
  11. o := &SimplePasswordMgr{
  12. authlist: make(map[string][]byte),
  13. }
  14. return o
  15. }
  16. func (this *SimplePasswordMgr) AddUserWithBytesPassword(username string, password []byte) error {
  17. p, err := bcrypt.GenerateFromPassword(password, 10)
  18. if err != nil {
  19. return err
  20. }
  21. this.authlist[username] = p
  22. return nil
  23. }
  24. func (this *SimplePasswordMgr) AddUserWithStringPassword(username string, password string) error {
  25. return this.AddUserWithBytesPassword(username, []byte(password))
  26. }
  27. func (this *SimplePasswordMgr) AddUserWithBcryptPassword(username string, password []byte) {
  28. this.authlist[username] = password
  29. }
  30. func (this *SimplePasswordMgr) PasswordCheckCallback(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) {
  31. v, ok := this.authlist[conn.User()]
  32. if !ok {
  33. return nil, errors.New("auth failed with incorrected password")
  34. }
  35. err := bcrypt.CompareHashAndPassword(v, password)
  36. if err != nil {
  37. return nil, errors.New("auth failed with incorrected password")
  38. }
  39. return nil, nil
  40. }