Skip to content

Repository files navigation

ldap

GoDocBuild StatusGo Report CardcodecovLicense: 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.

goget"github.com/go-stuff/ldap"

Usage

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

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.

package main
import (
"fmt""github.com/go-stuff/ldap"
)
// OpenLDAPconst (
LDAP_SERVERstring="192.168.1.100"LDAP_PORTstring="636"LDAP_BIND_DNstring="cn=admin,dc=go-stuff,dc=ca"LDAP_BIND_PASSstring="password"LDAP_USER_BASE_DNstring="ou=people,dc=go-stuff,dc=ca"LDAP_USER_SEARCH_ATTRstring="uid"LDAP_GROUP_BASE_DNstring="ou=group,dc=go-stuff,dc=ca"LDAP_GROUP_OBJECT_CLASSstring="posixGroup"LDAP_GROUP_SEARCH_ATTRstring="memberUid"LDAP_GROUP_SEARCH_FULLstring="false"
)
// Active Dreictory// const (// LDAP_SERVER string = "LDAPSSL"// LDAP_PORT string = "636"// LDAP_BIND_DN string = "CN=admin,OU=Users,DC=go-stuff,DC=ca"// LDAP_BIND_PASS string = "password"// LDAP_USER_BASE_DN string = "OU=Users,DC=go-stuff,DC=ca"// LDAP_USER_SEARCH_ATTR string = "CN"// LDAP_GROUP_BASE_DN string = "OU=Groups,DC=go-stuff,DC=ca"// LDAP_GROUP_OBJECT_CLASS string = "group"// LDAP_GROUP_SEARCH_ATTR string = "member"// LDAP_GROUP_SEARCH_FULL string = "true"// )funcmain() {
username, groups, err:=ldap.Auth(
LDAP_SERVER,
LDAP_PORT,
LDAP_BIND_DN,
LDAP_BIND_PASS,
LDAP_USER_BASE_DN,
LDAP_USER_OBJECT_CLASS,
LDAP_USER_SEARCH_ATTR,
LDAP_GROUP_BASE_DN,
LDAP_GROUP_OBJECT_CLASS,
LDAP_GROUP_SEARCH_ATTR,
LDAP_AUTH_ATTR,
"web-user",
"password",
)
fmt.Printf("Username: %s\n", username)
iferr!=nil {
fmt.Println(err.Error())
}
for_, v:=rangegroups {
fmt.Printf(" Group: %s\n", v)
}

Example Output

Username: web-user
Group: domain users
Group: group-user
Group: group-random1
Group: group-random3

License

MIT License

About

Authenticate a username and password against LDAP using go-ldap.

Topics

Resources

Stars

2 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages