bind.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package ldap
  5. import (
  6. "errors"
  7. "github.com/gogits/gogs/modules/asn1-ber"
  8. )
  9. func (l *Conn) Bind(username, password string) error {
  10. messageID := l.nextMessageID()
  11. packet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request")
  12. packet.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, messageID, "MessageID"))
  13. bindRequest := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationBindRequest, nil, "Bind Request")
  14. bindRequest.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, 3, "Version"))
  15. bindRequest.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, username, "User Name"))
  16. bindRequest.AppendChild(ber.NewString(ber.ClassContext, ber.TypePrimitive, 0, password, "Password"))
  17. packet.AppendChild(bindRequest)
  18. if l.Debug {
  19. ber.PrintPacket(packet)
  20. }
  21. channel, err := l.sendMessage(packet)
  22. if err != nil {
  23. return err
  24. }
  25. if channel == nil {
  26. return NewError(ErrorNetwork, errors.New("ldap: could not send message"))
  27. }
  28. defer l.finishMessage(messageID)
  29. packet = <-channel
  30. if packet == nil {
  31. return NewError(ErrorNetwork, errors.New("ldap: could not retrieve response"))
  32. }
  33. if l.Debug {
  34. if err := addLDAPDescriptions(packet); err != nil {
  35. return err
  36. }
  37. ber.PrintPacket(packet)
  38. }
  39. resultCode, resultDescription := getLDAPResultCode(packet)
  40. if resultCode != 0 {
  41. return NewError(resultCode, errors.New(resultDescription))
  42. }
  43. return nil
  44. }