Skip to content

Commit 29645df

Browse files
authored
Merge pull request #16 from hodbn/bugfix-amqplain
Fix AMQPLAIN authentication mechanism
2 parents d336670 + e306e34 commit 29645df

File tree

2 files changed

+64
-14
lines changed

2 files changed

+64
-14
lines changed

auth.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package amqp091
66

77
import (
8+
"bytes"
89
"fmt"
910
)
1011

@@ -42,9 +43,14 @@ func (auth *AMQPlainAuth) Mechanism() string {
4243
return "AMQPLAIN"
4344
}
4445

45-
// Response returns the null character delimited encoding for the SASL PLAIN Mechanism.
46+
// Response returns an AMQP encoded credentials table, without the field table size.
4647
func (auth *AMQPlainAuth) Response() string {
47-
return fmt.Sprintf("LOGIN:%sPASSWORD:%s", auth.Username, auth.Password)
48+
var buf bytes.Buffer
49+
table := Table{"LOGIN": auth.Username, "PASSWORD": auth.Password}
50+
if err := writeTable(&buf, table); err != nil {
51+
return ""
52+
}
53+
return buf.String()[4:]
4854
}
4955

5056
// Finds the first mechanism preferred by the client that the server supports.

client_test.go

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,27 @@ type server struct {
2424
tune connectionTuneOk
2525
}
2626

27-
func defaultConfig() Config {
27+
var defaultLogin = "guest"
28+
var defaultPassword = "guest"
29+
var defaultPlainAuth = &PlainAuth{defaultLogin, defaultPassword}
30+
var defaultAMQPlainAuth = &AMQPlainAuth{defaultLogin, defaultPassword}
31+
32+
func defaultConfigWithAuth(auth Authentication) Config {
2833
return Config{
29-
SASL: []Authentication{&PlainAuth{"guest", "guest"}},
34+
SASL: []Authentication{auth},
3035
Vhost: "/",
3136
Locale: defaultLocale,
3237
}
3338
}
3439

40+
func defaultConfig() Config {
41+
return defaultConfigWithAuth(defaultPlainAuth)
42+
}
43+
44+
func amqplainConfig() Config {
45+
return defaultConfigWithAuth(defaultAMQPlainAuth)
46+
}
47+
3548
func newServer(t *testing.T, serverIO, clientIO io.ReadWriteCloser) *server {
3649
return &server{
3750
T: t,
@@ -153,15 +166,21 @@ func (t *server) expectAMQP() {
153166
t.expectBytes([]byte{'A', 'M', 'Q', 'P', 0, 0, 9, 1})
154167
}
155168

156-
func (t *server) connectionStart() {
169+
func (t *server) connectionStartWithMechanisms(mechs string, recv bool) {
157170
t.send(0, &connectionStart{
158171
VersionMajor: 0,
159172
VersionMinor: 9,
160-
Mechanisms: "PLAIN",
161-
Locales: "en_US",
173+
Mechanisms: mechs,
174+
Locales: defaultLocale,
162175
})
163176

164-
t.recv(0, &t.start)
177+
if recv {
178+
t.recv(0, &t.start)
179+
}
180+
}
181+
182+
func (t *server) connectionStart() {
183+
t.connectionStartWithMechanisms("PLAIN", true)
165184
}
166185

167186
func (t *server) connectionTune() {
@@ -283,12 +302,7 @@ func TestOpenFailedSASLUnsupportedMechanisms(t *testing.T) {
283302

284303
go func() {
285304
srv.expectAMQP()
286-
srv.send(0, &connectionStart{
287-
VersionMajor: 0,
288-
VersionMinor: 9,
289-
Mechanisms: "KERBEROS NTLM",
290-
Locales: "en_US",
291-
})
305+
srv.connectionStartWithMechanisms("KERBEROS NTLM", false)
292306
}()
293307

294308
c, err := Open(rwc, defaultConfig())
@@ -297,6 +311,36 @@ func TestOpenFailedSASLUnsupportedMechanisms(t *testing.T) {
297311
}
298312
}
299313

314+
func TestOpenAMQPlainAuth(t *testing.T) {
315+
auth := make(chan Table)
316+
rwc, srv := newSession(t)
317+
318+
go func() {
319+
srv.expectAMQP()
320+
srv.connectionStartWithMechanisms("AMQPLAIN", true)
321+
var authresp bytes.Buffer
322+
_ = writeLongstr(&authresp, srv.start.Response)
323+
table, _ := readTable(&authresp)
324+
srv.connectionTune()
325+
326+
srv.recv(0, &connectionOpen{})
327+
srv.send(0, &connectionOpenOk{})
328+
rwc.Close()
329+
auth <- table
330+
}()
331+
332+
if c, err := Open(rwc, amqplainConfig()); err != nil {
333+
t.Fatalf("could not create connection: %v (%s)", c, err)
334+
}
335+
table := <-auth
336+
if table["LOGIN"] != defaultLogin {
337+
t.Fatalf("unexpected login: want: %s, got: %s", defaultLogin, table["LOGIN"])
338+
}
339+
if table["PASSWORD"] != defaultPassword {
340+
t.Fatalf("unexpected password: want: %s, got: %s", defaultPassword, table["PASSWORD"])
341+
}
342+
}
343+
300344
func TestOpenFailedCredentials(t *testing.T) {
301345
rwc, srv := newSession(t)
302346

0 commit comments

Comments
 (0)