You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.6 KiB

10 years ago
  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. }