File tree 6 files changed +114
-12
lines changed
6 files changed +114
-12
lines changed Original file line number Diff line number Diff line change @@ -10,9 +10,8 @@ import (
10
10
"math/rand"
11
11
"strings"
12
12
13
- ber "github.com/go-asn1-ber/asn1-ber"
14
13
"github.com/Azure/go-ntlmssp"
15
-
14
+ ber "github.com/go-asn1-ber/asn1-ber"
16
15
)
17
16
18
17
// SimpleBindRequest represents a username/password bind operation
@@ -395,7 +394,7 @@ func (l *Conn) ExternalBind() error {
395
394
// NTLMBindRequest represents an NTLMSSP bind operation
396
395
type NTLMBindRequest struct {
397
396
// Domain is the AD Domain to authenticate too. If not specified, it will be grabbed from the NTLMSSP Challenge
398
- Domain string
397
+ Domain string
399
398
// Username is the name of the Directory object that the client wishes to bind as
400
399
Username string
401
400
// Password is the credentials to bind with
Original file line number Diff line number Diff line change @@ -212,9 +212,9 @@ func GetLDAPError(packet *ber.Packet) error {
212
212
}
213
213
return & Error {
214
214
ResultCode : resultCode ,
215
- MatchedDN : response .Children [1 ].Value .(string ),
216
- Err : fmt .Errorf ("%s" , response .Children [2 ].Value .(string )),
217
- Packet : packet ,
215
+ MatchedDN : response .Children [1 ].Value .(string ),
216
+ Err : fmt .Errorf ("%s" , response .Children [2 ].Value .(string )),
217
+ Packet : packet ,
218
218
}
219
219
}
220
220
}
@@ -246,6 +246,7 @@ func IsErrorAnyOf(err error, codes ...uint16) bool {
246
246
247
247
return false
248
248
}
249
+
249
250
// IsErrorWithCode returns true if the given error is an LDAP error with the given result code
250
251
func IsErrorWithCode (err error , desiredResultCode uint16 ) bool {
251
252
return IsErrorAnyOf (err , desiredResultCode )
Original file line number Diff line number Diff line change @@ -2,7 +2,9 @@ package ldap
2
2
3
3
import (
4
4
"crypto/tls"
5
+ "crypto/x509"
5
6
"fmt"
7
+ "io/ioutil"
6
8
"log"
7
9
)
8
10
@@ -340,3 +342,52 @@ func ExampleControlPaging_manualPaging() {
340
342
break
341
343
}
342
344
}
345
+
346
+ // This example demonstrates how to use EXTERNAL SASL with TLS client certificates.
347
+ func ExampleConn_ExternalBind () {
348
+ var ldapCert = "/path/to/cert.pem"
349
+ var ldapKey = "/path/to/key.pem"
350
+ var ldapCAchain = "/path/to/ca_chain.pem"
351
+
352
+ // Load client cert and key
353
+ cert , err := tls .LoadX509KeyPair (ldapCert , ldapKey )
354
+ if err != nil {
355
+ log .Fatal (err )
356
+ }
357
+
358
+ // Load CA chain
359
+ caCert , err := ioutil .ReadFile (ldapCAchain )
360
+ if err != nil {
361
+ log .Fatal (err )
362
+ }
363
+ caCertPool := x509 .NewCertPool ()
364
+ caCertPool .AppendCertsFromPEM (caCert )
365
+
366
+ // Setup TLS with ldap client cert
367
+ tlsConfig := & tls.Config {
368
+ Certificates : []tls.Certificate {cert },
369
+ RootCAs : caCertPool ,
370
+ InsecureSkipVerify : true ,
371
+ }
372
+
373
+ // connect to ldap server
374
+ l , err := DialURL ("ldap://ldap.example.com:389" )
375
+ if err != nil {
376
+ log .Fatal (err )
377
+ }
378
+ defer l .Close ()
379
+
380
+ // reconnect using tls
381
+ err = l .StartTLS (tlsConfig )
382
+ if err != nil {
383
+ log .Fatal (err )
384
+ }
385
+
386
+ // sasl external bind
387
+ err = l .ExternalBind ()
388
+ if err != nil {
389
+ log .Fatal (err )
390
+ }
391
+
392
+ // Conduct ldap queries
393
+ }
Original file line number Diff line number Diff line change @@ -10,9 +10,8 @@ import (
10
10
"math/rand"
11
11
"strings"
12
12
13
- ber "github.com/go-asn1-ber/asn1-ber"
14
13
"github.com/Azure/go-ntlmssp"
15
-
14
+ ber "github.com/go-asn1-ber/asn1-ber"
16
15
)
17
16
18
17
// SimpleBindRequest represents a username/password bind operation
@@ -395,7 +394,7 @@ func (l *Conn) ExternalBind() error {
395
394
// NTLMBindRequest represents an NTLMSSP bind operation
396
395
type NTLMBindRequest struct {
397
396
// Domain is the AD Domain to authenticate too. If not specified, it will be grabbed from the NTLMSSP Challenge
398
- Domain string
397
+ Domain string
399
398
// Username is the name of the Directory object that the client wishes to bind as
400
399
Username string
401
400
// Password is the credentials to bind with
Original file line number Diff line number Diff line change @@ -212,9 +212,9 @@ func GetLDAPError(packet *ber.Packet) error {
212
212
}
213
213
return & Error {
214
214
ResultCode : resultCode ,
215
- MatchedDN : response .Children [1 ].Value .(string ),
216
- Err : fmt .Errorf ("%s" , response .Children [2 ].Value .(string )),
217
- Packet : packet ,
215
+ MatchedDN : response .Children [1 ].Value .(string ),
216
+ Err : fmt .Errorf ("%s" , response .Children [2 ].Value .(string )),
217
+ Packet : packet ,
218
218
}
219
219
}
220
220
}
@@ -246,6 +246,7 @@ func IsErrorAnyOf(err error, codes ...uint16) bool {
246
246
247
247
return false
248
248
}
249
+
249
250
// IsErrorWithCode returns true if the given error is an LDAP error with the given result code
250
251
func IsErrorWithCode (err error , desiredResultCode uint16 ) bool {
251
252
return IsErrorAnyOf (err , desiredResultCode )
Original file line number Diff line number Diff line change @@ -2,7 +2,9 @@ package ldap
2
2
3
3
import (
4
4
"crypto/tls"
5
+ "crypto/x509"
5
6
"fmt"
7
+ "io/ioutil"
6
8
"log"
7
9
)
8
10
@@ -340,3 +342,52 @@ func ExampleControlPaging_manualPaging() {
340
342
break
341
343
}
342
344
}
345
+
346
+ // This example demonstrates how to use EXTERNAL SASL with TLS client certificates.
347
+ func ExampleConn_ExternalBind () {
348
+ var ldapCert = "/path/to/cert.pem"
349
+ var ldapKey = "/path/to/key.pem"
350
+ var ldapCAchain = "/path/to/ca_chain.pem"
351
+
352
+ // Load client cert and key
353
+ cert , err := tls .LoadX509KeyPair (ldapCert , ldapKey )
354
+ if err != nil {
355
+ log .Fatal (err )
356
+ }
357
+
358
+ // Load CA chain
359
+ caCert , err := ioutil .ReadFile (ldapCAchain )
360
+ if err != nil {
361
+ log .Fatal (err )
362
+ }
363
+ caCertPool := x509 .NewCertPool ()
364
+ caCertPool .AppendCertsFromPEM (caCert )
365
+
366
+ // Setup TLS with ldap client cert
367
+ tlsConfig := & tls.Config {
368
+ Certificates : []tls.Certificate {cert },
369
+ RootCAs : caCertPool ,
370
+ InsecureSkipVerify : true ,
371
+ }
372
+
373
+ // connect to ldap server
374
+ l , err := DialURL ("ldap://ldap.example.com:389" )
375
+ if err != nil {
376
+ log .Fatal (err )
377
+ }
378
+ defer l .Close ()
379
+
380
+ // reconnect using tls
381
+ err = l .StartTLS (tlsConfig )
382
+ if err != nil {
383
+ log .Fatal (err )
384
+ }
385
+
386
+ // sasl external bind
387
+ err = l .ExternalBind ()
388
+ if err != nil {
389
+ log .Fatal (err )
390
+ }
391
+
392
+ // Conduct ldap queries
393
+ }
You can’t perform that action at this time.
0 commit comments