forked from jcmturner/gokrb5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticator.go
163 lines (154 loc) · 5.06 KB
/
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package service
import (
"encoding/base64"
"errors"
"fmt"
goidentity "gopkg.in/jcmturner/goidentity.v1"
"gopkg.in/jcmturner/gokrb5.v4/client"
"gopkg.in/jcmturner/gokrb5.v4/config"
"gopkg.in/jcmturner/gokrb5.v4/credentials"
"gopkg.in/jcmturner/gokrb5.v4/gssapi"
"gopkg.in/jcmturner/gokrb5.v4/keytab"
"strings"
"time"
)
// SPNEGOAuthenticator implements gopkg.in/jcmturner/goidentity.v1.Authenticator interface
type SPNEGOAuthenticator struct {
SPNEGOHeaderValue string
Keytab *keytab.Keytab
ServiceAccount string
ClientAddr string
RequireHostAddr bool
}
// Authenticate and retrieve a goidentity.Identity. In this case it is a pointer to a credentials.Credentials
func (a SPNEGOAuthenticator) Authenticate() (i goidentity.Identity, ok bool, err error) {
b, err := base64.StdEncoding.DecodeString(a.SPNEGOHeaderValue)
if err != nil {
err = fmt.Errorf("SPNEGO error in base64 decoding negotiation header: %v", err)
return
}
var spnego gssapi.SPNEGO
err = spnego.Unmarshal(b)
if !spnego.Init {
err = fmt.Errorf("SPNEGO negotiation token is not a NegTokenInit: %v", err)
return
}
if !spnego.NegTokenInit.MechTypes[0].Equal(gssapi.MechTypeOIDKRB5) {
err = errors.New("SPNEGO OID of MechToken is not of type KRB5")
return
}
var mt gssapi.MechToken
err = mt.Unmarshal(spnego.NegTokenInit.MechToken)
if err != nil {
err = fmt.Errorf("SPNEGO error unmarshaling MechToken: %v", err)
return
}
if !mt.IsAPReq() {
err = errors.New("MechToken does not contain an AP_REQ - KRB_AP_ERR_MSG_TYPE")
return
}
ok, c, err := ValidateAPREQ(mt.APReq, *a.Keytab, a.ServiceAccount, a.ClientAddr, a.RequireHostAddr)
if err != nil {
err = fmt.Errorf("SPNEGO validation error: %v", err)
return
}
i = &c
return
}
// Mechanism returns the authentication mechanism.
func (a SPNEGOAuthenticator) Mechanism() string {
return "SPNEGO Kerberos"
}
// KRB5BasicAuthenticator implements gopkg.in/jcmturner/goidentity.v1.Authenticator interface.
// It takes username and password so can be used for basic authentication.
type KRB5BasicAuthenticator struct {
BasicHeaderValue string
realm string
username string
password string
ServiceKeytab *keytab.Keytab
ServiceAccount string
Config *config.Config
SPN string
}
// Authenticate and return the identity. The boolean indicates if the authentication was successful.
func (a KRB5BasicAuthenticator) Authenticate() (i goidentity.Identity, ok bool, err error) {
a.realm, a.username, a.password, err = parseBasicHeaderValue(a.BasicHeaderValue)
if err != nil {
err = fmt.Errorf("could not parse basic authentication header: %v", err)
return
}
cl := client.NewClientWithPassword(a.username, a.realm, a.password)
cl.WithConfig(a.Config)
err = cl.Login()
if err != nil {
// Username and/or password could be wrong
err = fmt.Errorf("Error with user credentials during login: %v", err)
return
}
tkt, _, err := cl.GetServiceTicket(a.SPN)
if err != nil {
err = fmt.Errorf("Could not get service ticket: %v", err)
return
}
err = tkt.DecryptEncPart(*a.ServiceKeytab, a.ServiceAccount)
if err != nil {
err = fmt.Errorf("Could not decrypt service ticket: %v", err)
return
}
cl.Credentials.SetAuthTime(time.Now().UTC())
cl.Credentials.SetAuthenticated(true)
isPAC, pac, err := tkt.GetPACType(*a.ServiceKeytab, a.ServiceAccount)
if isPAC && err != nil {
err = fmt.Errorf("Error processing PAC: %v", err)
return
}
if isPAC {
// There is a valid PAC. Adding attributes to creds
cl.Credentials.SetADCredentials(credentials.ADCredentials{
GroupMembershipSIDs: pac.KerbValidationInfo.GetGroupMembershipSIDs(),
LogOnTime: pac.KerbValidationInfo.LogOnTime.Time(),
LogOffTime: pac.KerbValidationInfo.LogOffTime.Time(),
PasswordLastSet: pac.KerbValidationInfo.PasswordLastSet.Time(),
EffectiveName: pac.KerbValidationInfo.EffectiveName.Value,
FullName: pac.KerbValidationInfo.FullName.Value,
UserID: int(pac.KerbValidationInfo.UserID),
PrimaryGroupID: int(pac.KerbValidationInfo.PrimaryGroupID),
LogonServer: pac.KerbValidationInfo.LogonServer.Value,
LogonDomainName: pac.KerbValidationInfo.LogonDomainName.Value,
LogonDomainID: pac.KerbValidationInfo.LogonDomainID.ToString(),
})
}
ok = true
i = cl.Credentials
return
}
// Mechanism returns the authentication mechanism.
func (a KRB5BasicAuthenticator) Mechanism() string {
return "Kerberos Basic"
}
func parseBasicHeaderValue(s string) (domain, username, password string, err error) {
b, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return
}
v := string(b)
vc := strings.SplitN(v, ":", 2)
password = vc[1]
// Domain and username can be specified in 2 formats:
// <Username> - no domain specified
// <Domain>\<Username>
// <Username>@<Domain>
if strings.Contains(vc[0], `\`) {
u := strings.SplitN(vc[0], `\`, 2)
domain = u[0]
username = u[1]
} else if strings.Contains(vc[0], `@`) {
u := strings.SplitN(vc[0], `@`, 2)
domain = u[1]
username = u[0]
} else {
username = vc[0]
}
return
}