Skip to content

Commit ad9f2ea

Browse files
committed
Add command line utility for querying LDAP and manual testing
1 parent 993d307 commit ad9f2ea

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test.sh

cmd/main.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"log"
6+
7+
"github.com/jtblin/go-ldap-client"
8+
)
9+
10+
var base, bindDN, bindPassword, groupFilter, host, password, serverName, userFilter, username string
11+
var port int
12+
var useSSL bool
13+
14+
type server struct{}
15+
16+
func main() {
17+
flag.Parse()
18+
19+
client := &ldap.LDAPClient{
20+
Base: base,
21+
Host: host,
22+
Port: port,
23+
UseSSL: useSSL,
24+
BindDN: bindDN,
25+
BindPassword: bindPassword,
26+
UserFilter: userFilter,
27+
GroupFilter: groupFilter,
28+
Attributes: []string{"givenName", "sn", "mail", "uid"},
29+
ServerName: serverName,
30+
}
31+
defer client.Close()
32+
33+
ok, user, err := client.Authenticate(username, password)
34+
if err != nil {
35+
log.Fatalf("Error authenticating user %s: %+v", username, err)
36+
}
37+
if !ok {
38+
log.Fatalf("Authenticating failed for user %s", username)
39+
}
40+
log.Printf("User: %+v", user)
41+
42+
groups, err := client.GetGroupsOfUser(username)
43+
if err != nil {
44+
log.Fatalf("Error getting groups for user %s: %+v", username, err)
45+
}
46+
log.Printf("Groups: %+v", groups)
47+
}
48+
49+
func init() {
50+
flag.StringVar(&base, "base", "dc=example,dc=com", "Base LDAP")
51+
flag.StringVar(&bindDN, "bind-dn", "uid=readonlysuer,ou=People,dc=example,dc=com", "Bind DN")
52+
flag.StringVar(&bindPassword, "bind-pwd", "readonlypassword", "Bind password")
53+
flag.StringVar(&groupFilter, "group-filter", "(memberUid=%s)", "Group filter")
54+
flag.StringVar(&host, "host", "ldap.example.com", "LDAP host")
55+
flag.StringVar(&password, "password", "", "Password")
56+
flag.IntVar(&port, "port", 389, "LDAP port")
57+
flag.StringVar(&userFilter, "user-filter", "(uid=%s)", "User filter")
58+
flag.StringVar(&username, "username", "", "Username")
59+
flag.StringVar(&serverName, "server-name", "", "Server name for SSL (if use-ssl is set)")
60+
flag.BoolVar(&useSSL, "use-ssl", false, "Use SSL")
61+
}

0 commit comments

Comments
 (0)