Advertisement
Guest User

Untitled

a guest
Jul 14th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.60 KB | None | 0 0
  1. package ldap_test
  2.  
  3. import (
  4.     "log"
  5.  
  6.     "github.com/simonjanss/go-ldap-client"
  7. )
  8.  
  9. // ExampleLDAPClient_Authenticate shows how a typical application can verify a login attempt
  10. func ExampleLDAPClient_Authenticate() {
  11.     client := &ldap.LDAPClient{
  12.         Base:         "dc=example,dc=com",
  13.         Host:         "ldap.example.com",
  14.         Port:         389,
  15.         UseSSL:       false,
  16.         BindDN:       "uid=readonlysuer,ou=People,dc=example,dc=com",
  17.         BindPassword: "readonlypassword",
  18.         UserFilter:   "(uid=%s)",
  19.         GroupFilter:  "(memberUid=%s)",
  20.         Attributes:   []string{"givenName", "sn", "mail", "uid"},
  21.     }
  22.     defer client.Close()
  23.  
  24.     ok, user, err := client.Authenticate("username", "password")
  25.     if err != nil {
  26.         log.Fatalf("Error authenticating user %s: %+v", "username", err)
  27.     }
  28.     if !ok {
  29.         log.Fatalf("Authenticating failed for user %s", "username")
  30.     }
  31.     log.Printf("User: %+v", user)
  32.  
  33. }
  34.  
  35. // ExampleLDAPClient_GetGroupsOfUser shows how to check if user is in the allowed groups
  36. func ExampleLDAPClient_GetGroupsOfUser() {
  37.     client := &ldap.LDAPClient{
  38.         Base:        "dc=example,dc=com",
  39.         Host:        "ldap.example.com",
  40.         Port:        389,
  41.         GroupFilter: "(memberUid=%s)",
  42.         Groups:       []string{"Webgroup", "Admingroup", "Testgroup"},
  43.     }
  44.     defer client.Close()
  45.    
  46.     groups, err := client.GetGroupsOfUser("username")
  47.     if err != nil {
  48.         log.Printf("Error getting groups for user %s: %+v", "username", err)
  49.     } else if groups == false {
  50.         log.Printf("Authentication failed: User %s is not in the allowed groups.", "username")
  51.     } else {
  52.         log.Printf("Authentication successful: User %s is logged in.", "username")
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement