-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathconfig.go
54 lines (45 loc) · 1.14 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package ad
import (
"fmt"
"log"
"gopkg.in/ldap.v3"
)
type Config struct {
Domain string
IP string
URL string
Username string
Password string
}
// Client() returns a connection for accessing AD services.
func (c *Config) Client() (*ldap.Conn, error) {
var username string
var url string
username = c.Username + "@" + c.Domain
// stay downwards compatible
switch {
case c.URL != "":
url = c.URL
case c.IP != "":
url = fmt.Sprintf("ldap://%s:389", c.IP)
default:
return nil, fmt.Errorf("Need either an IP or LDAP URL to connect to AD, check provider configuration")
}
adConn, err := clientConnect(url, username, c.Password)
if err != nil {
return nil, fmt.Errorf("Error while trying to connect active directory server, Check server IP address, username or password: %s", err)
}
log.Printf("[DEBUG] AD connection successful for user: %s", c.Username)
return adConn, nil
}
func clientConnect(url, username, password string) (*ldap.Conn, error) {
adConn, err := ldap.DialURL(url)
if err != nil {
return nil, err
}
err = adConn.Bind(username, password)
if err != nil {
return nil, err
}
return adConn, nil
}