This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_authenticator.go
90 lines (76 loc) · 2.43 KB
/
test_authenticator.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package sshserver
import (
"context"
"net"
)
// NewTestAuthenticationHandler creates a new backend that authenticates a user based on the users variable and passes
// all further calls to the backend.
func NewTestAuthenticationHandler(
backend Handler,
users ...*TestUser,
) Handler {
return &testAuthenticationHandler{
users: users,
backend: backend,
}
}
// testAuthenticationHandler is a handler that authenticates and passes authentication to the configured backend.
type testAuthenticationHandler struct {
users []*TestUser
backend Handler
}
func (t *testAuthenticationHandler) OnReady() error {
return t.backend.OnReady()
}
func (t *testAuthenticationHandler) OnShutdown(ctx context.Context) {
t.backend.OnShutdown(ctx)
}
func (t *testAuthenticationHandler) OnNetworkConnection(client net.TCPAddr, connectionID string) (NetworkConnectionHandler, error) {
backend, err := t.backend.OnNetworkConnection(client, connectionID)
if err != nil {
return nil, err
}
return &testAuthenticationNetworkHandler{
rootHandler: t,
backend: backend,
}, nil
}
type testAuthenticationNetworkHandler struct {
rootHandler *testAuthenticationHandler
backend NetworkConnectionHandler
}
func (t *testAuthenticationNetworkHandler) OnDisconnect() {
t.backend.OnDisconnect()
}
func (t *testAuthenticationNetworkHandler) OnShutdown(shutdownContext context.Context) {
t.backend.OnShutdown(shutdownContext)
}
func (t *testAuthenticationNetworkHandler) OnAuthPassword(username string, password []byte) (response AuthResponse, reason error) {
for _, user := range t.rootHandler.users {
if user.username == username && user.password == string(password) {
return AuthResponseSuccess, nil
}
}
return AuthResponseFailure, ErrAuthenticationFailed
}
func (t *testAuthenticationNetworkHandler) OnAuthPubKey(username string, pubKey string) (response AuthResponse, reason error) {
for _, user := range t.rootHandler.users {
if user.username == username {
for _, authorizedKey := range user.authorizedKeys {
if pubKey == authorizedKey {
return AuthResponseSuccess, nil
}
}
}
}
return AuthResponseFailure, ErrAuthenticationFailed
}
func (t *testAuthenticationNetworkHandler) OnHandshakeFailed(err error) {
t.backend.OnHandshakeFailed(err)
}
func (t *testAuthenticationNetworkHandler) OnHandshakeSuccess(username string) (
connection SSHConnectionHandler,
failureReason error,
) {
return t.backend.OnHandshakeSuccess(username)
}