password.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package auth
  2. import (
  3. "fmt"
  4. "math/rand/v2"
  5. "time"
  6. "github.com/seaweedfs/seaweedfs/weed/sftpd/user"
  7. "golang.org/x/crypto/ssh"
  8. )
  9. // PasswordAuthenticator handles password-based authentication
  10. type PasswordAuthenticator struct {
  11. userStore user.Store
  12. enabled bool
  13. }
  14. // NewPasswordAuthenticator creates a new password authenticator
  15. func NewPasswordAuthenticator(userStore user.Store, enabled bool) *PasswordAuthenticator {
  16. return &PasswordAuthenticator{
  17. userStore: userStore,
  18. enabled: enabled,
  19. }
  20. }
  21. // Enabled returns whether password authentication is enabled
  22. func (a *PasswordAuthenticator) Enabled() bool {
  23. return a.enabled
  24. }
  25. // Authenticate validates a password for a user
  26. func (a *PasswordAuthenticator) Authenticate(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) {
  27. username := conn.User()
  28. // Check if password auth is enabled
  29. if !a.enabled {
  30. return nil, fmt.Errorf("password authentication disabled")
  31. }
  32. // Validate password against user store
  33. if a.userStore.ValidatePassword(username, password) {
  34. return &ssh.Permissions{
  35. Extensions: map[string]string{
  36. "username": username,
  37. },
  38. }, nil
  39. }
  40. // Add delay to prevent brute force attacks
  41. time.Sleep(time.Duration(100+rand.IntN(100)) * time.Millisecond)
  42. return nil, fmt.Errorf("authentication failed")
  43. }