forked from vadimi/go-http-ntlm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpntlm_test.go
69 lines (58 loc) · 1.48 KB
/
httpntlm_test.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
package httpntlm
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/vadimi/go-ntlm/ntlm"
)
func Test_AuthenticationSuccess(t *testing.T) {
client := http.Client{
Transport: &NtlmTransport{
Domain: "dt",
User: "testuser",
Password: "fish",
},
}
session, _ := ntlm.CreateServerSession(ntlm.Version2, ntlm.ConnectionlessMode)
session.SetUserInfo("testuser", "fish", "dt")
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h := r.Header.Get("Authorization")
if h == "" {
w.WriteHeader(401)
return
}
ntlmChallengeString := strings.Replace(h, "NTLM ", "", -1)
authenticateBytes, _ := decBase64(ntlmChallengeString)
auth, err := ntlm.ParseAuthenticateMessage(authenticateBytes, 2)
if err == nil {
err = session.ProcessAuthenticateMessage(auth)
if err != nil {
t.Errorf("Could not process authenticate message: %s\n", err)
return
}
} else {
challenge, _ := session.GenerateChallengeMessage()
w.Header().Add("WWW-Authenticate", `Basic realm="test"`)
w.Header().Add("WWW-Authenticate", "NTLM "+encBase64(challenge.Bytes()))
w.WriteHeader(401)
}
}))
defer ts.Close()
req, err := http.NewRequest("GET", ts.URL, strings.NewReader(""))
resp, err := client.Do(req)
if err != nil {
t.Error(err)
}
defer func() {
err := resp.Body.Close()
if err != nil {
t.Error(err)
}
}()
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
t.Error(err)
}
}