项目作者: go-stuff

项目描述 :
Authenticate a username and password against LDAP using go-ldap.
高级语言: Go
项目地址: git://github.com/go-stuff/ldap.git
创建时间: 2019-06-02T13:16:25Z
项目社区:https://github.com/go-stuff/ldap

开源协议:MIT License

下载


ldap

GoDoc
Build Status
Go Report Card
codecov
License: MIT

Gopher Share

Using go-ldap.v3 to authenticate with LDAP and return the username and groups associated witht that user. An error is returned if authentication fails.

Packages Imported

Basic LDAP github.com/go-ldap/ldap

Installation

The recommended way to get started using github.com/go-stuff/ldap is by using ‘go get’ to install the dependency in your project.

  1. go get "github.com/go-stuff/ldap"

Usage

  1. import (
  2. "github.com/go-stuff/ldap"
  3. )

Example

This is an example of how it would be implemented. Of course the constants could be environment variables or in a configuration file, etc… this is just an example.
The reason there are so many variables is to allow for connecting to multiple environments, it has been tested against OpenLDAP and Active Directory, there are some minor differences in objectClass and attributes.

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/go-stuff/ldap"
  5. )
  6. // OpenLDAP
  7. const (
  8. LDAP_SERVER string = "192.168.1.100"
  9. LDAP_PORT string = "636"
  10. LDAP_BIND_DN string = "cn=admin,dc=go-stuff,dc=ca"
  11. LDAP_BIND_PASS string = "password"
  12. LDAP_USER_BASE_DN string = "ou=people,dc=go-stuff,dc=ca"
  13. LDAP_USER_SEARCH_ATTR string = "uid"
  14. LDAP_GROUP_BASE_DN string = "ou=group,dc=go-stuff,dc=ca"
  15. LDAP_GROUP_OBJECT_CLASS string = "posixGroup"
  16. LDAP_GROUP_SEARCH_ATTR string = "memberUid"
  17. LDAP_GROUP_SEARCH_FULL string = "false"
  18. )
  19. // Active Dreictory
  20. // const (
  21. // LDAP_SERVER string = "LDAPSSL"
  22. // LDAP_PORT string = "636"
  23. // LDAP_BIND_DN string = "CN=admin,OU=Users,DC=go-stuff,DC=ca"
  24. // LDAP_BIND_PASS string = "password"
  25. // LDAP_USER_BASE_DN string = "OU=Users,DC=go-stuff,DC=ca"
  26. // LDAP_USER_SEARCH_ATTR string = "CN"
  27. // LDAP_GROUP_BASE_DN string = "OU=Groups,DC=go-stuff,DC=ca"
  28. // LDAP_GROUP_OBJECT_CLASS string = "group"
  29. // LDAP_GROUP_SEARCH_ATTR string = "member"
  30. // LDAP_GROUP_SEARCH_FULL string = "true"
  31. // )
  32. func main() {
  33. username, groups, err := ldap.Auth(
  34. LDAP_SERVER,
  35. LDAP_PORT,
  36. LDAP_BIND_DN,
  37. LDAP_BIND_PASS,
  38. LDAP_USER_BASE_DN,
  39. LDAP_USER_OBJECT_CLASS,
  40. LDAP_USER_SEARCH_ATTR,
  41. LDAP_GROUP_BASE_DN,
  42. LDAP_GROUP_OBJECT_CLASS,
  43. LDAP_GROUP_SEARCH_ATTR,
  44. LDAP_AUTH_ATTR,
  45. "web-user",
  46. "password",
  47. )
  48. fmt.Printf("Username: %s\n", username)
  49. if err != nil {
  50. fmt.Println(err.Error())
  51. }
  52. for _, v := range groups {
  53. fmt.Printf(" Group: %s\n", v)
  54. }

Example Output

  1. Username: web-user
  2. Group: domain users
  3. Group: group-user
  4. Group: group-random1
  5. Group: group-random3

License

MIT License