Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ldap_test
- import (
- "log"
- "github.com/simonjanss/go-ldap-client"
- )
- // ExampleLDAPClient_Authenticate shows how a typical application can verify a login attempt
- func ExampleLDAPClient_Authenticate() {
- client := &ldap.LDAPClient{
- Base: "dc=example,dc=com",
- Host: "ldap.example.com",
- Port: 389,
- UseSSL: false,
- BindDN: "uid=readonlysuer,ou=People,dc=example,dc=com",
- BindPassword: "readonlypassword",
- UserFilter: "(uid=%s)",
- GroupFilter: "(memberUid=%s)",
- Attributes: []string{"givenName", "sn", "mail", "uid"},
- }
- defer client.Close()
- ok, user, err := client.Authenticate("username", "password")
- if err != nil {
- log.Fatalf("Error authenticating user %s: %+v", "username", err)
- }
- if !ok {
- log.Fatalf("Authenticating failed for user %s", "username")
- }
- log.Printf("User: %+v", user)
- }
- // ExampleLDAPClient_GetGroupsOfUser shows how to check if user is in the allowed groups
- func ExampleLDAPClient_GetGroupsOfUser() {
- client := &ldap.LDAPClient{
- Base: "dc=example,dc=com",
- Host: "ldap.example.com",
- Port: 389,
- GroupFilter: "(memberUid=%s)",
- Groups: []string{"Webgroup", "Admingroup", "Testgroup"},
- }
- defer client.Close()
- groups, err := client.GetGroupsOfUser("username")
- if err != nil {
- log.Printf("Error getting groups for user %s: %+v", "username", err)
- } else if groups == false {
- log.Printf("Authentication failed: User %s is not in the allowed groups.", "username")
- } else {
- log.Printf("Authentication successful: User %s is logged in.", "username")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement