aboutsummaryrefslogtreecommitdiff
path: root/modules/ldap/bind.go
diff options
context:
space:
mode:
authorMichael Boke <michael@mbict.nl>2014-10-03 22:51:07 +0200
committerMichael Boke <michael@mbict.nl>2014-10-03 22:51:07 +0200
commitba1270df2d3d835b397317f133963e7b517242f1 (patch)
tree1265a142a1fd9951d30ae11648e7fbfb5806e594 /modules/ldap/bind.go
parentba0feadc34400cb91ff23f66096884d862651cdd (diff)
parent405ee14711ab946bd709ec28a526890c40cbc03b (diff)
Merge remote-tracking branch 'upstream/master'
Conflicts: conf/app.ini
Diffstat (limited to 'modules/ldap/bind.go')
-rw-r--r--modules/ldap/bind.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/modules/ldap/bind.go b/modules/ldap/bind.go
new file mode 100644
index 00000000..0561e611
--- /dev/null
+++ b/modules/ldap/bind.go
@@ -0,0 +1,55 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package ldap
+
+import (
+ "errors"
+
+ "github.com/gogits/gogs/modules/asn1-ber"
+)
+
+func (l *Conn) Bind(username, password string) error {
+ messageID := l.nextMessageID()
+
+ packet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request")
+ packet.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, messageID, "MessageID"))
+ bindRequest := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationBindRequest, nil, "Bind Request")
+ bindRequest.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, 3, "Version"))
+ bindRequest.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, username, "User Name"))
+ bindRequest.AppendChild(ber.NewString(ber.ClassContext, ber.TypePrimitive, 0, password, "Password"))
+ packet.AppendChild(bindRequest)
+
+ if l.Debug {
+ ber.PrintPacket(packet)
+ }
+
+ channel, err := l.sendMessage(packet)
+ if err != nil {
+ return err
+ }
+ if channel == nil {
+ return NewError(ErrorNetwork, errors.New("ldap: could not send message"))
+ }
+ defer l.finishMessage(messageID)
+
+ packet = <-channel
+ if packet == nil {
+ return NewError(ErrorNetwork, errors.New("ldap: could not retrieve response"))
+ }
+
+ if l.Debug {
+ if err := addLDAPDescriptions(packet); err != nil {
+ return err
+ }
+ ber.PrintPacket(packet)
+ }
+
+ resultCode, resultDescription := getLDAPResultCode(packet)
+ if resultCode != 0 {
+ return NewError(resultCode, errors.New(resultDescription))
+ }
+
+ return nil
+}