-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkerberos_native_win.go
67 lines (60 loc) · 1.69 KB
/
kerberos_native_win.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
//go:build windows
package kpx
import (
"encoding/base64"
"github.com/alexbrainman/sspi"
"github.com/alexbrainman/sspi/negotiate"
"github.com/palantir/stacktrace"
"net"
"strings"
"sync"
)
var NativeKerberos = &WindowsKerberos{}
type WindowsKerberos struct {
done bool
mutex sync.Mutex
}
func (k *WindowsKerberos) SafeTryLogin() error {
k.mutex.Lock()
defer k.mutex.Unlock()
if k.done {
return nil
}
k.done = true
logInfo("[-] Authenticating user with Windows native kerberos")
_, err := negotiate.AcquireCurrentUserCredentials()
if err != nil {
return stacktrace.Propagate(err, "unable to acquire kerberos credentials from Windows")
}
return nil
}
func (k *WindowsKerberos) SafeGetToken(protocol string, host string) (*string, error) {
cred, err := negotiate.AcquireCurrentUserCredentials()
if err != nil {
return nil, stacktrace.Propagate(err, "unable to acquire kerberos credentials from Windows")
}
defer func(cred *sspi.Credentials) {
_ = cred.Release()
}(cred)
cname, err := k.getCanonicalHostname(host)
if err != nil {
return nil, stacktrace.Propagate(err, "unable to resolve host from hostname: %s", host)
}
spn := protocol + "/" + cname
sec, token, err := negotiate.NewClientContext(cred, spn)
if err != nil {
return nil, stacktrace.Propagate(err, "unable to acquire kerberos client context from Windows for spn: %s", spn)
}
defer func(sec *negotiate.ClientContext) {
_ = sec.Release()
}(sec)
hs := base64.StdEncoding.EncodeToString(token)
return &hs, nil
}
func (k *WindowsKerberos) getCanonicalHostname(hostname string) (string, error) {
cname, err := net.LookupCNAME(hostname)
if err != nil {
return "", err
}
return strings.TrimRight(cname, "."), nil
}