forked from ContainerSSH/auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_impl.go
86 lines (78 loc) · 1.89 KB
/
handler_impl.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
package auth
import (
goHttp "net/http"
"github.com/containerssh/http"
"github.com/containerssh/log"
)
type handler struct {
passwordHandler goHttp.Handler
pubkeyHandler goHttp.Handler
}
func (h handler) ServeHTTP(writer goHttp.ResponseWriter, request *goHttp.Request) {
switch request.URL.Path {
case "/password":
h.passwordHandler.ServeHTTP(writer, request)
case "/pubkey":
h.pubkeyHandler.ServeHTTP(writer, request)
default:
writer.WriteHeader(404)
}
}
type passwordHandler struct {
backend Handler
logger log.Logger
}
func (p *passwordHandler) OnRequest(request http.ServerRequest, response http.ServerResponse) error {
requestObject := PasswordAuthRequest{}
if err := request.Decode(&requestObject); err != nil {
return err
}
success, err := p.backend.OnPassword(
requestObject.Username,
requestObject.Password,
requestObject.RemoteAddress,
requestObject.ConnectionID,
)
if err != nil {
p.logger.Debug(log.Wrap(err, ERequestDecodeFailed, "failed to execute password request"))
response.SetStatus(500)
response.SetBody(ResponseBody{
Success: false,
})
return nil
} else {
response.SetBody(ResponseBody{
Success: success,
})
}
return nil
}
type pubKeyHandler struct {
backend Handler
logger log.Logger
}
func (p *pubKeyHandler) OnRequest(request http.ServerRequest, response http.ServerResponse) error {
requestObject := PublicKeyAuthRequest{}
if err := request.Decode(&requestObject); err != nil {
return err
}
success, err := p.backend.OnPubKey(
requestObject.Username,
requestObject.PublicKey,
requestObject.RemoteAddress,
requestObject.ConnectionID,
)
if err != nil {
p.logger.Debug(log.Wrap(err, ERequestDecodeFailed, "failed to execute public key request"))
response.SetStatus(500)
response.SetBody(ResponseBody{
Success: false,
})
return nil
} else {
response.SetBody(ResponseBody{
Success: success,
})
}
return nil
}