aboutsummaryrefslogtreecommitdiff
path: root/weed/sftpd/auth/password.go
blob: a42c3f5b8812f85cb3f473b363d6f2ab4da051ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package auth

import (
	"fmt"
	"math/rand"
	"time"

	"github.com/seaweedfs/seaweedfs/weed/sftpd/user"
	"golang.org/x/crypto/ssh"
)

// PasswordAuthenticator handles password-based authentication
type PasswordAuthenticator struct {
	userStore user.Store
	enabled   bool
}

// NewPasswordAuthenticator creates a new password authenticator
func NewPasswordAuthenticator(userStore user.Store, enabled bool) *PasswordAuthenticator {
	return &PasswordAuthenticator{
		userStore: userStore,
		enabled:   enabled,
	}
}

// Enabled returns whether password authentication is enabled
func (a *PasswordAuthenticator) Enabled() bool {
	return a.enabled
}

// Authenticate validates a password for a user
func (a *PasswordAuthenticator) Authenticate(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) {
	username := conn.User()

	// Check if password auth is enabled
	if !a.enabled {
		return nil, fmt.Errorf("password authentication disabled")
	}

	// Validate password against user store
	if a.userStore.ValidatePassword(username, password) {
		return &ssh.Permissions{
			Extensions: map[string]string{
				"username": username,
			},
		}, nil
	}

	// Add delay to prevent brute force attacks
	time.Sleep(time.Duration(100+rand.Intn(100)) * time.Millisecond)

	return nil, fmt.Errorf("authentication failed")
}