auth.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. package amqp
  6. import (
  7. "fmt"
  8. )
  9. // Authentication interface provides a means for different SASL authentication
  10. // mechanisms to be used during connection tuning.
  11. type Authentication interface {
  12. Mechanism() string
  13. Response() string
  14. }
  15. // PlainAuth is a similar to Basic Auth in HTTP.
  16. type PlainAuth struct {
  17. Username string
  18. Password string
  19. }
  20. func (me *PlainAuth) Mechanism() string {
  21. return "PLAIN"
  22. }
  23. func (me *PlainAuth) Response() string {
  24. return fmt.Sprintf("\000%s\000%s", me.Username, me.Password)
  25. }
  26. // Finds the first mechanism preferred by the client that the server supports.
  27. func pickSASLMechanism(client []Authentication, serverMechanisms []string) (auth Authentication, ok bool) {
  28. for _, auth = range client {
  29. for _, mech := range serverMechanisms {
  30. if auth.Mechanism() == mech {
  31. return auth, true
  32. }
  33. }
  34. }
  35. return
  36. }