1 // Copyright (c) 2012, Sean Treadway, SoundCloud Ltd.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4 // Source code and contact info at http://github.com/streadway/amqp
12 // Authentication interface provides a means for different SASL authentication
13 // mechanisms to be used during connection tuning.
14 type Authentication interface {
19 // PlainAuth is a similar to Basic Auth in HTTP.
20 type PlainAuth struct {
25 // Mechanism returns "PLAIN"
26 func (auth *PlainAuth) Mechanism() string {
30 // Response returns the null character delimited encoding for the SASL PLAIN Mechanism.
31 func (auth *PlainAuth) Response() string {
32 return fmt.Sprintf("\000%s\000%s", auth.Username, auth.Password)
35 // AMQPlainAuth is similar to PlainAuth
36 type AMQPlainAuth struct {
41 // Mechanism returns "AMQPLAIN"
42 func (auth *AMQPlainAuth) Mechanism() string {
46 // Response returns the null character delimited encoding for the SASL PLAIN Mechanism.
47 func (auth *AMQPlainAuth) Response() string {
48 return fmt.Sprintf("LOGIN:%sPASSWORD:%s", auth.Username, auth.Password)
51 // Finds the first mechanism preferred by the client that the server supports.
52 func pickSASLMechanism(client []Authentication, serverMechanisms []string) (auth Authentication, ok bool) {
53 for _, auth = range client {
54 for _, mech := range serverMechanisms {
55 if auth.Mechanism() == mech {