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
/
Copy pathtestAuthenticationNetworkHandler.go
96 lines (85 loc) · 2.8 KB
/
testAuthenticationNetworkHandler.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
91
92
93
94
95
96
package sshserver
import (
"context"
"fmt"
)
type testAuthenticationNetworkHandler struct {
rootHandler *testAuthenticationHandler
backend NetworkConnectionHandler
}
func (t *testAuthenticationNetworkHandler) OnAuthKeyboardInteractive(
username string,
challenge func(
instruction string,
questions KeyboardInteractiveQuestions,
) (answers KeyboardInteractiveAnswers, err error),
_ string,
) (response AuthResponse, metadata map[string]string, reason error) {
var foundUser *TestUser
for _, user := range t.rootHandler.users {
if user.Username() == username {
foundUser = user
break
}
}
if foundUser == nil {
return AuthResponseFailure, nil, fmt.Errorf("user not found")
}
var questions []KeyboardInteractiveQuestion
for question := range foundUser.keyboardInteractive {
questions = append(questions, KeyboardInteractiveQuestion{
ID: question,
Question: question,
EchoResponse: false,
})
}
answers, err := challenge("", questions)
if err != nil {
return AuthResponseFailure, nil, err
}
for question, expectedAnswer := range foundUser.keyboardInteractive {
answerText, err := answers.GetByQuestionText(question)
if err != nil {
return AuthResponseFailure, nil, err
}
if answerText != expectedAnswer {
return AuthResponseFailure, nil, fmt.Errorf("invalid response")
}
}
return AuthResponseSuccess, nil, nil
}
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, clientVersion string) (response AuthResponse, metadata map[string]string, reason error) {
for _, user := range t.rootHandler.users {
if user.username == username && user.password == string(password) {
return AuthResponseSuccess, nil, nil
}
}
return AuthResponseFailure, nil, ErrAuthenticationFailed
}
func (t *testAuthenticationNetworkHandler) OnAuthPubKey(username string, pubKey string, clientVersion string) (response AuthResponse, metadata map[string]string, reason error) {
for _, user := range t.rootHandler.users {
if user.username == username {
for _, authorizedKey := range user.authorizedKeys {
if pubKey == authorizedKey {
return AuthResponseSuccess, nil,nil
}
}
}
}
return AuthResponseFailure, nil, ErrAuthenticationFailed
}
func (t *testAuthenticationNetworkHandler) OnHandshakeFailed(err error) {
t.backend.OnHandshakeFailed(err)
}
func (t *testAuthenticationNetworkHandler) OnHandshakeSuccess(username string, clientVersion string, metadata map[string]string) (
connection SSHConnectionHandler,
failureReason error,
) {
return t.backend.OnHandshakeSuccess(username, clientVersion, metadata)
}