auth.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Package auth provides authentication and authorization functionality for the SFTP server
  2. package auth
  3. import (
  4. "github.com/seaweedfs/seaweedfs/weed/sftpd/user"
  5. "golang.org/x/crypto/ssh"
  6. )
  7. // Provider defines the interface for authentication providers
  8. type Provider interface {
  9. // GetAuthMethods returns the SSH server auth methods
  10. GetAuthMethods() []ssh.AuthMethod
  11. }
  12. // Manager handles authentication and authorization
  13. type Manager struct {
  14. userStore user.Store
  15. passwordAuth *PasswordAuthenticator
  16. publicKeyAuth *PublicKeyAuthenticator
  17. enabledAuthMethods []string
  18. }
  19. // NewManager creates a new authentication manager
  20. func NewManager(userStore user.Store, enabledAuthMethods []string) *Manager {
  21. manager := &Manager{
  22. userStore: userStore,
  23. enabledAuthMethods: enabledAuthMethods,
  24. }
  25. // Initialize authenticators based on enabled methods
  26. passwordEnabled := false
  27. publicKeyEnabled := false
  28. for _, method := range enabledAuthMethods {
  29. switch method {
  30. case "password":
  31. passwordEnabled = true
  32. case "publickey":
  33. publicKeyEnabled = true
  34. }
  35. }
  36. manager.passwordAuth = NewPasswordAuthenticator(userStore, passwordEnabled)
  37. manager.publicKeyAuth = NewPublicKeyAuthenticator(userStore, publicKeyEnabled)
  38. return manager
  39. }
  40. // GetSSHServerConfig returns an SSH server config with the appropriate authentication methods
  41. func (m *Manager) GetSSHServerConfig() *ssh.ServerConfig {
  42. config := &ssh.ServerConfig{}
  43. // Add password authentication if enabled
  44. if m.passwordAuth.Enabled() {
  45. config.PasswordCallback = m.passwordAuth.Authenticate
  46. }
  47. // Add public key authentication if enabled
  48. if m.publicKeyAuth.Enabled() {
  49. config.PublicKeyCallback = m.publicKeyAuth.Authenticate
  50. }
  51. return config
  52. }
  53. // GetUser retrieves a user from the user store
  54. func (m *Manager) GetUser(username string) (*user.User, error) {
  55. return m.userStore.GetUser(username)
  56. }