barometer: update DMA's vendoring packages
[barometer.git] / src / dma / vendor / github.com / streadway / amqp / auth.go
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
5
6 package amqp
7
8 import (
9         "fmt"
10 )
11
12 // Authentication interface provides a means for different SASL authentication
13 // mechanisms to be used during connection tuning.
14 type Authentication interface {
15         Mechanism() string
16         Response() string
17 }
18
19 // PlainAuth is a similar to Basic Auth in HTTP.
20 type PlainAuth struct {
21         Username string
22         Password string
23 }
24
25 // Mechanism returns "PLAIN"
26 func (auth *PlainAuth) Mechanism() string {
27         return "PLAIN"
28 }
29
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)
33 }
34
35 // AMQPlainAuth is similar to PlainAuth
36 type AMQPlainAuth struct {
37         Username string
38         Password string
39 }
40
41 // Mechanism returns "AMQPLAIN"
42 func (auth *AMQPlainAuth) Mechanism() string {
43         return "AMQPLAIN"
44 }
45
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)
49 }
50
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 {
56                                 return auth, true
57                         }
58                 }
59         }
60
61         return
62 }