publickey.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package auth
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/sftpd/user"
  5. "golang.org/x/crypto/ssh"
  6. )
  7. // PublicKeyAuthenticator handles public key-based authentication
  8. type PublicKeyAuthenticator struct {
  9. userStore user.Store
  10. enabled bool
  11. }
  12. // NewPublicKeyAuthenticator creates a new public key authenticator
  13. func NewPublicKeyAuthenticator(userStore user.Store, enabled bool) *PublicKeyAuthenticator {
  14. return &PublicKeyAuthenticator{
  15. userStore: userStore,
  16. enabled: enabled,
  17. }
  18. }
  19. // Enabled returns whether public key authentication is enabled
  20. func (a *PublicKeyAuthenticator) Enabled() bool {
  21. return a.enabled
  22. }
  23. // Authenticate validates a public key for a user
  24. func (a *PublicKeyAuthenticator) Authenticate(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
  25. username := conn.User()
  26. // Check if public key auth is enabled
  27. if !a.enabled {
  28. return nil, fmt.Errorf("public key authentication disabled")
  29. }
  30. // Convert key to string format for comparison
  31. keyData := string(key.Marshal())
  32. // Validate public key
  33. if a.userStore.ValidatePublicKey(username, keyData) {
  34. return &ssh.Permissions{
  35. Extensions: map[string]string{
  36. "username": username,
  37. },
  38. }, nil
  39. }
  40. return nil, fmt.Errorf("authentication failed")
  41. }