-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathauthenticator_test.go
More file actions
151 lines (142 loc) · 4.41 KB
/
Copy pathauthenticator_test.go
File metadata and controls
151 lines (142 loc) · 4.41 KB
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
// Copyright 2019, 2021 The Alpaca Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"encoding/base64"
"encoding/binary"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/samuong/go-ntlmssp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type ntlmServer struct {
t *testing.T
}
func (s ntlmServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
hdr := req.Header.Get("Proxy-Authorization")
if !strings.HasPrefix(hdr, "NTLM ") {
sendProxyAuthRequired(w)
return
}
msg, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(hdr, "NTLM "))
require.NoError(s.t, err)
require.True(s.t, bytes.Equal(msg[0:8], []byte("NTLMSSP\x00")), "Missing NTLMSSP signature")
msgType := binary.LittleEndian.Uint32(msg[8:12])
switch msgType {
case 1:
sendChallengeResponse(w)
case 3:
req.Header.Del("Proxy-Authenticate")
_, err := w.Write([]byte("Access granted"))
require.NoError(s.t, err)
default:
s.t.Fatalf("Unexpected NTLM message type: %x", msgType)
}
}
func sendProxyAuthRequired(w http.ResponseWriter) {
w.Header().Set("Proxy-Authenticate", "NTLM")
w.Header().Set("Connection", "close")
w.WriteHeader(http.StatusProxyAuthRequired)
_, _ = fmt.Fprintf(w, "<html><body>oh noes!</body></html>")
}
func sendChallengeResponse(w http.ResponseWriter) {
w.Header().Set("Proxy-Authenticate", "NTLM TlRMTVNTUAACAAAADAAMADgAAAAFgomi+Rp9UDbAycMAAAAAAAAAAKIAogBEAAAABgEAAAAAAA9HAEwATwBCAEEATAACAAwARwBMAE8AQgBBAEwAAQAeAFAAWABZAEEAVQAwADAAMgBNAEUATAAwADEAMAAzAAQAHABnAGwAbwBiAGEAbAAuAGEAbgB6AC4AYwBvAG0AAwA8AHAAeAB5AGEAdQAwADAAMgBtAGUAbAAwADEAMAAzAC4AZwBsAG8AYgBhAGwALgBhAG4AegAuAGMAbwBtAAcACABQ7ZOkOQbVAQAAAAA=")
w.WriteHeader(http.StatusProxyAuthRequired)
}
func TestNtlmAuth(t *testing.T) {
server := httptest.NewServer(ntlmServer{t})
defer server.Close()
serverAddr := server.Listener.Addr().String()
tr := &http.Transport{Proxy: http.ProxyURL(&url.URL{Host: serverAddr})}
req, err := http.NewRequest(http.MethodGet, "http://"+serverAddr, nil)
require.NoError(t, err)
resp, err := tr.RoundTrip(req)
require.NoError(t, err)
require.NoError(t, resp.Body.Close())
require.Equal(t, http.StatusProxyAuthRequired, resp.StatusCode)
auth := &authenticator{"isis", "malory", ntlmssp.GetNtlmHash("guest")}
resp, err = auth.do(req, tr)
require.NoError(t, err)
defer resp.Body.Close() //nolint:errcheck
assert.Equal(t, http.StatusOK, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, "Access granted", string(body))
}
func TestFindNTLMChallenge(t *testing.T) {
tests := []struct {
name string
headers []string
want string
}{
{
name: "single NTLM challenge",
headers: []string{"NTLM TlRMTVNTUAACAAAA"},
want: "TlRMTVNTUAACAAAA",
},
{
name: "NTLM follows Negotiate (multi-auth proxy)",
// Real-world ordering from squid+kerberos+ntlm: Negotiate
// is advertised first, then NTLM with the Type 2 token,
// then Basic. A naive Header.Get() would return the
// Negotiate value, miss the NTLM token, and break the
// challenge-response.
headers: []string{
"Negotiate",
"NTLM TlRMTVNTUAACAAAA",
"Basic realm=\"proxy\"",
},
want: "TlRMTVNTUAACAAAA",
},
{
name: "case-insensitive scheme prefix",
headers: []string{
"Negotiate",
"ntlm tOkEnFoo",
},
want: "tOkEnFoo",
},
{
name: "no NTLM challenge present",
headers: []string{"Negotiate", "Basic realm=\"proxy\""},
want: "",
},
{
name: "bare NTLM (re-advertisement, no token)",
headers: []string{"NTLM"},
want: "",
},
{
name: "no Proxy-Authenticate header at all",
headers: nil,
want: "",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
h := make(http.Header)
for _, v := range tc.headers {
h.Add("Proxy-Authenticate", v)
}
assert.Equal(t, tc.want, findNTLMChallenge(h))
})
}
}