bind.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. }