Debian Codegraph @

service/authenticator.go

raw symbols SCIP
1 package service
2
3 import (
4 "encoding/base64"
5 "errors"
6 "fmt"
7 "strings"
8 "time"
9
10 goidentity "gopkg.in/jcmturner/goidentity.v2"
11 "gopkg.in/jcmturner/gokrb5.v5/client"
12 "gopkg.in/jcmturner/gokrb5.v5/config"
13 "gopkg.in/jcmturner/gokrb5.v5/credentials"
14 "gopkg.in/jcmturner/gokrb5.v5/gssapi"
15 "gopkg.in/jcmturner/gokrb5.v5/keytab"
16 )
17
18 // SPNEGOAuthenticator implements gopkg.in/jcmturner/goidentity.v2.Authenticator interface
19 type SPNEGOAuthenticator struct {
20 SPNEGOHeaderValue string
21 Keytab *keytab.Keytab
22 ServiceAccount string
23 ClientAddr string
24 RequireHostAddr bool
25 }
26
27 // Authenticate and retrieve a goidentity.Identity. In this case it is a pointer to a credentials.Credentials
28 func (a SPNEGOAuthenticator) Authenticate() (i goidentity.Identity, ok bool, err error) {
29 b, err := base64.StdEncoding.DecodeString(a.SPNEGOHeaderValue)
30 if err != nil {
31 err = fmt.Errorf("SPNEGO error in base64 decoding negotiation header: %v", err)
32 return
33 }
34 var spnego gssapi.SPNEGO
35 err = spnego.Unmarshal(b)
36 if !spnego.Init {
37 err = fmt.Errorf("SPNEGO negotiation token is not a NegTokenInit: %v", err)
38 return
39 }
40 if !spnego.NegTokenInit.MechTypes[0].Equal(gssapi.MechTypeOIDKRB5) {
41 err = errors.New("SPNEGO OID of MechToken is not of type KRB5")
42 return
43 }
44 var mt gssapi.MechToken
45 err = mt.Unmarshal(spnego.NegTokenInit.MechToken)
46 if err != nil {
47 err = fmt.Errorf("SPNEGO error unmarshaling MechToken: %v", err)
48 return
49 }
50 if !mt.IsAPReq() {
51 err = errors.New("MechToken does not contain an AP_REQ - KRB_AP_ERR_MSG_TYPE")
52 return
53 }
54
55 ok, c, err := ValidateAPREQ(mt.APReq, *a.Keytab, a.ServiceAccount, a.ClientAddr, a.RequireHostAddr)
56 if err != nil {
57 err = fmt.Errorf("SPNEGO validation error: %v", err)
58 return
59 }
60 i = &c
61 return
62 }
63
64 // Mechanism returns the authentication mechanism.
65 func (a SPNEGOAuthenticator) Mechanism() string {
66 return "SPNEGO Kerberos"
67 }
68
69 // KRB5BasicAuthenticator implements gopkg.in/jcmturner/goidentity.v2.Authenticator interface.
70 // It takes username and password so can be used for basic authentication.
71 type KRB5BasicAuthenticator struct {
72 BasicHeaderValue string
73 realm string
74 username string
75 password string
76 ServiceKeytab *keytab.Keytab
77 ServiceAccount string
78 Config *config.Config
79 SPN string
80 }
81
82 // Authenticate and return the identity. The boolean indicates if the authentication was successful.
83 func (a KRB5BasicAuthenticator) Authenticate() (i goidentity.Identity, ok bool, err error) {
84 a.realm, a.username, a.password, err = parseBasicHeaderValue(a.BasicHeaderValue)
85 if err != nil {
86 err = fmt.Errorf("could not parse basic authentication header: %v", err)
87 return
88 }
89 cl := client.NewClientWithPassword(a.username, a.realm, a.password)
90 cl.WithConfig(a.Config)
91 err = cl.Login()
92 if err != nil {
93 // Username and/or password could be wrong
94 err = fmt.Errorf("error with user credentials during login: %v", err)
95 return
96 }
97 tkt, _, err := cl.GetServiceTicket(a.SPN)
98 if err != nil {
99 err = fmt.Errorf("could not get service ticket: %v", err)
100 return
101 }
102 err = tkt.DecryptEncPart(*a.ServiceKeytab, a.ServiceAccount)
103 if err != nil {
104 err = fmt.Errorf("could not decrypt service ticket: %v", err)
105 return
106 }
107 cl.Credentials.SetAuthTime(time.Now().UTC())
108 cl.Credentials.SetAuthenticated(true)
109 isPAC, pac, err := tkt.GetPACType(*a.ServiceKeytab, a.ServiceAccount)
110 if isPAC && err != nil {
111 err = fmt.Errorf("error processing PAC: %v", err)
112 return
113 }
114 if isPAC {
115 // There is a valid PAC. Adding attributes to creds
116 cl.Credentials.SetADCredentials(credentials.ADCredentials{
117 GroupMembershipSIDs: pac.KerbValidationInfo.GetGroupMembershipSIDs(),
118 LogOnTime: pac.KerbValidationInfo.LogOnTime.Time(),
119 LogOffTime: pac.KerbValidationInfo.LogOffTime.Time(),
120 PasswordLastSet: pac.KerbValidationInfo.PasswordLastSet.Time(),
121 EffectiveName: pac.KerbValidationInfo.EffectiveName.Value,
122 FullName: pac.KerbValidationInfo.FullName.Value,
123 UserID: int(pac.KerbValidationInfo.UserID),
124 PrimaryGroupID: int(pac.KerbValidationInfo.PrimaryGroupID),
125 LogonServer: pac.KerbValidationInfo.LogonServer.Value,
126 LogonDomainName: pac.KerbValidationInfo.LogonDomainName.Value,
127 LogonDomainID: pac.KerbValidationInfo.LogonDomainID.ToString(),
128 })
129 }
130 ok = true
131 i = cl.Credentials
132 return
133 }
134
135 // Mechanism returns the authentication mechanism.
136 func (a KRB5BasicAuthenticator) Mechanism() string {
137 return "Kerberos Basic"
138 }
139
140 func parseBasicHeaderValue(s string) (domain, username, password string, err error) {
141 b, err := base64.StdEncoding.DecodeString(s)
142 if err != nil {
143 return
144 }
145 v := string(b)
146 vc := strings.SplitN(v, ":", 2)
147 password = vc[1]
148 // Domain and username can be specified in 2 formats:
149 // <Username> - no domain specified
150 // <Domain>\<Username>
151 // <Username>@<Domain>
152 if strings.Contains(vc[0], `\`) {
153 u := strings.SplitN(vc[0], `\`, 2)
154 domain = u[0]
155 username = u[1]
156 } else if strings.Contains(vc[0], `@`) {
157 u := strings.SplitN(vc[0], `@`, 2)
158 domain = u[1]
159 username = u[0]
160 } else {
161 username = vc[0]
162 }
163 return
164 }