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
package auth
import (
"strings"
"github.com/ibuler/ssh"
gossh "golang.org/x/crypto/ssh"
"cocogo/pkg/cctx"
"cocogo/pkg/common"
"cocogo/pkg/logger"
"cocogo/pkg/service"
)
func checkAuth(ctx ssh.Context, password, publicKey string) (ok bool) {
username := ctx.User()
remoteAddr := strings.Split(ctx.RemoteAddr().String(), ":")[0]
user := service.Authenticate(username, password, publicKey, remoteAddr, "T")
authMethod := "publickey"
action := "Accepted"
if password != "" {
authMethod = "password"
}
if user.Id == "" {
action = "Failed"
} else {
ctx.SetValue(cctx.ContextKeyUser, user)
ok = true
}
logger.Infof("%s %s for %s from %s", action, authMethod, username, remoteAddr)
return ok
}
func CheckUserPassword(ctx ssh.Context, password string) bool {
ok := checkAuth(ctx, password, "")
return ok
}
func CheckUserPublicKey(ctx ssh.Context, key ssh.PublicKey) bool {
b := key.Marshal()
publicKey := common.Base64Encode(string(b))
return checkAuth(ctx, "", publicKey)
}
func CheckMFA(ctx ssh.Context, challenger gossh.KeyboardInteractiveChallenge) bool {
return false
}