-
Notifications
You must be signed in to change notification settings - Fork 29
/
example_test.go
82 lines (64 loc) · 1.55 KB
/
example_test.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package auth_test
import (
"fmt"
auth "github.com/korylprince/go-ad-auth/v3"
)
func ExampleAuthenticate() {
config := &auth.Config{
Server: "ldap.example.com",
Port: 389,
BaseDN: "OU=Users,DC=example,DC=com",
Security: auth.SecurityStartTLS,
}
username := "user"
password := "pass"
status, err := auth.Authenticate(config, username, password)
if err != nil {
//handle err
return
}
if !status {
//handle failed authentication
return
}
}
func ExampleAuthenticateExtended() {
config := &auth.Config{
Server: "ldap.example.com",
Port: 389,
BaseDN: "OU=Users,DC=example,DC=com", //make sure BaseDN includes any groups you'll be referencing
Security: auth.SecurityStartTLS,
}
username := "user"
password := "pass"
status, entry, groups, err := auth.AuthenticateExtended(config, username, password, []string{"cn"}, []string{"Domain Admins"})
if err != nil {
//handle err
return
}
if !status {
//handle failed authentication
return
}
if len(groups) == 0 {
//handle user not being in any groups
return
}
//get attributes
cn := entry.GetAttributeValue("cn")
fmt.Println(cn)
}
func ExampleUpdatePassword() {
config := &auth.Config{
Server: "ldap.example.com",
Port: 389,
BaseDN: "OU=Users,DC=example,DC=com",
Security: auth.SecurityStartTLS, // Active Directory requires a secure connection to reset passwords
}
username := "user"
password := "pass"
newPassword := "Super$ecret"
if err := auth.UpdatePassword(config, username, password, newPassword); err != nil {
//handle err
}
}