forked from vadimi/go-http-ntlm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ntlmtransport.go
137 lines (113 loc) · 3.31 KB
/
ntlmtransport.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
package httpntlm
import (
"errors"
"io"
"io/ioutil"
"net/http"
"strings"
"github.com/vadimi/go-ntlm/ntlm"
)
var errEmptyNtlm = errors.New("empty NTLM challenge")
// NtlmTransport is implementation of http.RoundTripper interface
type NtlmTransport struct {
Domain string
User string
Password string
http.RoundTripper
Jar http.CookieJar
// Headers to copy from the original request onto the NTLM negotiation
// request, this can be useful when extra authentication is required,
// for example when you use Azure Relay.
CopyHeaders []string
}
// RoundTrip method send http request and tries to perform NTLM authentication
func (t NtlmTransport) RoundTrip(req *http.Request) (res *http.Response, err error) {
client := http.Client{}
if t.RoundTripper != nil {
client.Transport = t.RoundTripper
}
if t.Jar != nil {
client.Jar = t.Jar
}
resp, err := t.ntlmRoundTrip(client, req)
// retry once in case of an empty ntlm challenge
if err != nil && errors.Is(err, errEmptyNtlm) {
return t.ntlmRoundTrip(client, req)
}
return resp, err
}
func (t NtlmTransport) ntlmRoundTrip(client http.Client, req *http.Request) (*http.Response, error) {
// first send NTLM Negotiate header
r, _ := http.NewRequest("GET", req.URL.String(), strings.NewReader(""))
r.Header.Add("Authorization", "NTLM "+encBase64(negotiate()))
for i := range t.CopyHeaders {
values := req.Header.Values(t.CopyHeaders[i])
for _, v := range values {
r.Header.Add(t.CopyHeaders[i], v)
}
}
resp, err := client.Do(r)
if err != nil {
return nil, err
}
if err == nil && resp.StatusCode == http.StatusUnauthorized {
// it's necessary to reuse the same http connection
// in order to do that it's required to read Body and close it
_, err = io.Copy(ioutil.Discard, resp.Body)
if err != nil {
return nil, err
}
err = resp.Body.Close()
if err != nil {
return nil, err
}
// retrieve Www-Authenticate header from response
authHeaders := resp.Header.Values("WWW-Authenticate")
if len(authHeaders) == 0 {
return nil, errors.New("WWW-Authenticate header missing")
}
// there could be multiple WWW-Authenticate headers, so we need to pick the one that starts with NTLM
ntlmChallengeFound := false
var ntlmChallengeString string
for _, h := range authHeaders {
if strings.HasPrefix(h, "NTLM") {
ntlmChallengeFound = true
ntlmChallengeString = strings.TrimSpace(h[4:])
break
}
}
if ntlmChallengeString == "" {
if ntlmChallengeFound {
return nil, errEmptyNtlm
}
return nil, errors.New("wrong WWW-Authenticate header")
}
challengeBytes, err := decBase64(ntlmChallengeString)
if err != nil {
return nil, err
}
session, err := ntlm.CreateClientSession(ntlm.Version2, ntlm.ConnectionlessMode)
if err != nil {
return nil, err
}
session.SetUserInfo(t.User, t.Password, t.Domain)
// parse NTLM challenge
challenge, err := ntlm.ParseChallengeMessage(challengeBytes)
if err != nil {
return nil, err
}
err = session.ProcessChallengeMessage(challenge)
if err != nil {
return nil, err
}
// authenticate user
authenticate, err := session.GenerateAuthenticateMessage()
if err != nil {
return nil, err
}
// set NTLM Authorization header
req.Header.Set("Authorization", "NTLM "+encBase64(authenticate.Bytes()))
return client.Do(req)
}
return resp, err
}