forked from Azure/go-amqp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sasl_test.go
280 lines (254 loc) · 7.05 KB
/
sasl_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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package amqp
import (
"bytes"
"encoding/base64"
"fmt"
"strings"
"testing"
"time"
"github.com/Azure/go-amqp/internal/buffer"
"github.com/Azure/go-amqp/internal/encoding"
"github.com/Azure/go-amqp/internal/frames"
"github.com/Azure/go-amqp/internal/testconn"
)
// Known good challenges/responses taken following specification:
// https://developers.google.com/gmail/imap/xoauth2-protocol#the_sasl_xoauth2_mechanism
func TestSaslXOAUTH2InitialResponse(t *testing.T) {
wantedRespBase64 := "dXNlcj1zb21ldXNlckBleGFtcGxlLmNvbQFhdXRoPUJlYXJlciB5YTI5LnZGOWRmdDRxbVRjMk52YjNSbGNrQmhkSFJoZG1semRHRXVZMjl0Q2cBAQ=="
wantedResp, err := base64.StdEncoding.DecodeString(wantedRespBase64)
if err != nil {
t.Fatal(err)
}
gotResp, err := saslXOAUTH2InitialResponse("[email protected]", "ya29.vF9dft4qmTc2Nvb3RlckBhdHRhdmlzdGEuY29tCg")
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(wantedResp, gotResp) {
t.Errorf("Initial response does not match expected:\n %s", testDiff(gotResp, wantedResp))
}
}
//RFC6749 defines the OAUTH2 as comprising VSCHAR elements (\x20-7E)
func TestSaslXOAUTH2InvalidBearer(t *testing.T) {
tests := []struct {
label string
illegal string
}{
{
label: "char outside range",
illegal: "illegalChar\x00",
},
{
label: "empty bearer",
illegal: "",
},
}
for _, tt := range tests {
t.Run(tt.label, func(t *testing.T) {
_, err := saslXOAUTH2InitialResponse("[email protected]", tt.illegal)
if err == nil {
t.Errorf("Expected invalid bearer to be rejected")
}
})
}
}
// https://developers.google.com/gmail/imap/xoauth2-protocol gives no guidance on the formation of the username field
// or even if the username is actually required. However, disallowing \x01 seems reasonable as this would interfere
// with the encoding.
func TestSaslXOAUTH2InvalidUsername(t *testing.T) {
_, err := saslXOAUTH2InitialResponse("illegalChar\x01Within", "ya29.vF9dft4qmTc2Nvb3RlckBhdHRhdmlzdGEuY29tCg")
if err == nil {
t.Errorf("Expected invalid username to be rejected")
}
}
func TestSaslXOAUTH2EmptyUsername(t *testing.T) {
_, err := saslXOAUTH2InitialResponse("", "ya29.vF9dft4qmTc2Nvb3RlckBhdHRhdmlzdGEuY29tCg")
if err != nil {
t.Errorf("Expected empty username to be accepted")
}
}
func TestConnSASLXOAUTH2AuthSuccess(t *testing.T) {
buf, err := peerResponse(
[]byte("AMQP\x03\x01\x00\x00"),
frames.Frame{
Type: frameTypeSASL,
Channel: 0,
Body: &frames.SASLMechanisms{Mechanisms: []encoding.Symbol{saslMechanismXOAUTH2}},
},
frames.Frame{
Type: frameTypeSASL,
Channel: 0,
Body: &frames.SASLOutcome{Code: encoding.CodeSASLOK},
},
[]byte("AMQP\x00\x01\x00\x00"),
frames.Frame{
Type: frameTypeAMQP,
Channel: 0,
Body: &frames.PerformOpen{},
},
)
if err != nil {
t.Fatal(err)
}
c := testconn.New(buf)
client, err := New(c,
ConnSASLXOAUTH2("[email protected]", "ya29.vF9dft4qmTc2Nvb3RlckBhdHRhdmlzdGEuY29tCg", 512),
ConnIdleTimeout(10*time.Minute))
if err != nil {
t.Fatal(err)
}
defer client.Close()
}
func TestConnSASLXOAUTH2AuthFail(t *testing.T) {
buf, err := peerResponse(
[]byte("AMQP\x03\x01\x00\x00"),
frames.Frame{
Type: frameTypeSASL,
Channel: 0,
Body: &frames.SASLMechanisms{Mechanisms: []encoding.Symbol{saslMechanismXOAUTH2}},
},
frames.Frame{
Type: frameTypeSASL,
Channel: 0,
Body: &frames.SASLOutcome{Code: encoding.CodeSASLAuth},
},
)
if err != nil {
t.Fatal(err)
}
c := testconn.New(buf)
client, err := New(c,
ConnSASLXOAUTH2("[email protected]", "ya29.vF9dft4qmTc2Nvb3RlckBhdHRhdmlzdGEuY29tCg", 512),
ConnIdleTimeout(10*time.Minute))
if err != nil {
defer client.Close()
}
switch {
case err == nil:
t.Errorf("authentication is expected to fail ")
case !strings.Contains(err.Error(), fmt.Sprintf("code %#00x", encoding.CodeSASLAuth)):
t.Errorf("unexpected connection failure : %s", err)
}
}
func TestConnSASLXOAUTH2AuthFailWithErrorResponse(t *testing.T) {
buf, err := peerResponse(
[]byte("AMQP\x03\x01\x00\x00"),
frames.Frame{
Type: frameTypeSASL,
Channel: 0,
Body: &frames.SASLMechanisms{Mechanisms: []encoding.Symbol{saslMechanismXOAUTH2}},
},
frames.Frame{
Type: frameTypeSASL,
Channel: 0,
Body: &frames.SASLChallenge{Challenge: []byte("{ \"status\":\"401\", \"schemes\":\"bearer\", \"scope\":\"https://mail.google.com/\" }")},
},
frames.Frame{
Type: frameTypeSASL,
Channel: 0,
Body: &frames.SASLOutcome{Code: encoding.CodeSASLAuth},
},
)
if err != nil {
t.Fatal(err)
}
c := testconn.New(buf)
client, err := New(c,
ConnSASLXOAUTH2("[email protected]", "ya29.vF9dft4qmTc2Nvb3RlckBhdHRhdmlzdGEuY29tCg", 512),
ConnIdleTimeout(10*time.Minute))
if err != nil {
defer client.Close()
}
switch {
case err == nil:
t.Errorf("authentication is expected to fail ")
case !strings.Contains(err.Error(), fmt.Sprintf("code %#00x", encoding.CodeSASLAuth)):
t.Errorf("unexpected connection failure : %s", err)
}
}
func TestConnSASLXOAUTH2AuthFailsAdditionalErrorResponse(t *testing.T) {
buf, err := peerResponse(
[]byte("AMQP\x03\x01\x00\x00"),
frames.Frame{
Type: frameTypeSASL,
Channel: 0,
Body: &frames.SASLMechanisms{Mechanisms: []encoding.Symbol{saslMechanismXOAUTH2}},
},
frames.Frame{
Type: frameTypeSASL,
Channel: 0,
Body: &frames.SASLChallenge{Challenge: []byte("fail1")},
},
frames.Frame{
Type: frameTypeSASL,
Channel: 0,
Body: &frames.SASLChallenge{Challenge: []byte("fail2")},
},
)
if err != nil {
t.Fatal(err)
}
c := testconn.New(buf)
client, err := New(c,
ConnSASLXOAUTH2("[email protected]", "ya29.vF9dft4qmTc2Nvb3RlckBhdHRhdmlzdGEuY29tCg", 512),
ConnIdleTimeout(10*time.Minute))
if err != nil {
defer client.Close()
}
switch {
case err == nil:
t.Errorf("authentication is expected to fail ")
case !strings.Contains(err.Error(), "Initial error response: fail1, additional response: fail2"):
t.Errorf("unexpected connection failure : %s", err)
}
}
func TestConnSASLExternal(t *testing.T) {
buf, err := peerResponse(
[]byte("AMQP\x03\x01\x00\x00"),
frames.Frame{
Type: frameTypeSASL,
Channel: 0,
Body: &frames.SASLMechanisms{Mechanisms: []encoding.Symbol{saslMechanismEXTERNAL}},
},
frames.Frame{
Type: frameTypeSASL,
Channel: 0,
Body: &frames.SASLOutcome{Code: encoding.CodeSASLOK},
},
[]byte("AMQP\x00\x01\x00\x00"),
frames.Frame{
Type: frameTypeAMQP,
Channel: 0,
Body: &frames.PerformOpen{},
},
)
if err != nil {
t.Fatal(err)
}
c := testconn.New(buf)
client, err := New(c,
ConnSASLExternal(""),
ConnIdleTimeout(10*time.Minute))
if err != nil {
t.Fatal(err)
}
defer client.Close()
}
func peerResponse(items ...interface{}) ([]byte, error) {
buf := make([]byte, 0)
for _, item := range items {
switch v := item.(type) {
case frames.Frame:
b := &buffer.Buffer{}
e := writeFrame(b, v)
if e != nil {
return buf, e
}
buf = append(buf, b.Bytes()...)
case []byte:
buf = append(buf, v...)
default:
return buf, fmt.Errorf("unrecongized type %T", item)
}
}
return buf, nil
}