-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathauthentication.go
71 lines (55 loc) · 1.72 KB
/
authentication.go
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package socks5
import "context"
func (s *Server) choiceAuthenticationMethod(methods []byte) byte {
for _, method := range methods {
if _, ok := s.config.authMethods[method]; ok {
return method
}
}
return noAcceptableMethods
}
func (s *Server) usernamePasswordAuthenticate(ctx context.Context, conn *connection) {
version, err := conn.readByte()
if err != nil {
s.logger.Error(ctx, "failed to read authentication version: "+err.Error())
return
}
if version != usernamePasswordVersion {
return
}
usernameLen, err := conn.readByte()
if err != nil {
s.logger.Error(ctx, "failed to read username length: "+err.Error())
return
}
username := make([]byte, usernameLen)
if _, err := conn.read(username); err != nil {
s.logger.Error(ctx, "failed to read username: "+err.Error())
return
}
ctx = contextWithUsername(ctx, string(username))
passwordLen, err := conn.readByte()
if err != nil {
s.logger.Error(ctx, "failed to read password length: "+err.Error())
return
}
password := make([]byte, passwordLen)
if _, err := conn.read(password); err != nil {
s.logger.Error(ctx, "failed to read password: "+err.Error())
return
}
ctxTimeout, cancel := context.WithTimeout(ctx, s.config.getPasswordTimeout)
defer cancel()
passwordFromStore, err := s.store.GetPassword(ctxTimeout, string(username))
if err != nil {
s.logger.Error(ctx, "failed to get user password from store: "+err.Error())
return
}
if string(password) != passwordFromStore {
s.logger.Warn(ctx, "failed to authenticate user ["+string(username)+"]")
s.response(ctx, conn, usernamePasswordVersion, usernamePasswordFailure)
return
}
s.response(ctx, conn, usernamePasswordVersion, usernamePasswordSuccess)
s.acceptRequest(ctx, conn)
}