ldap

package module
v0.0.0-...-1ffced8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 3, 2021 License: BSD-2-Clause Imports: 6 Imported by: 0

README

go-ldap-client

Simple ldap client to authenticate, retrieve basic information and groups for a user.

Usage

Go Doc

See example. The only external dependency is gopkg.in/ldap.v2.

package main

import (
	"log"

	"github.com/jtblin/go-ldap-client"
)

func main() {
	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"},
	}
	# It is the responsibility of the caller to close the connection
	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)
	
	groups, err := client.GetGroupsOfUser("username")
	if err != nil {
		log.Fatalf("Error getting groups for user %s: %+v", "username", err)
	}
	log.Printf("Groups: %+v", groups) 
}

Why?

There are already tons of ldap libraries for golang but most of them are just forks of another one, most of them are too low level or too limited (e.g. do not return errors which make it hard to troubleshoot issues).

Documentation

Overview

Package ldap provides a simple ldap client to authenticate, retrieve basic information and groups for a user.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LDAPClient

type LDAPClient struct {
	Conn         *ldap.Conn
	Host         string
	Port         int
	UseSSL       bool
	BindDN       string
	BindPassword string
	GroupFilter  string // e.g. "(memberUid=%s)"
	UserFilter   string // e.g. "(uid=%s)"
	Base         string
	Attributes   []string
	ServerName   string
}

func (*LDAPClient) Authenticate

func (lc *LDAPClient) Authenticate(username, password string) (bool, map[string]string, error)

Authenticate authenticates the user against the ldap backend

Example

ExampleLDAPClient_Authenticate shows how a typical application can verify a login attempt

package main

import (
	"log"

	"github.com/jtblin/go-ldap-client"
)

func main() {
	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)

}

func (*LDAPClient) Close

func (lc *LDAPClient) Close()

Close closes the ldap backend connection

func (*LDAPClient) Connect

func (lc *LDAPClient) Connect() error

Connect connects to the ldap backend

func (*LDAPClient) FindUsers

func (lc *LDAPClient) FindUsers(search string) ([]map[string]string, error)

func (*LDAPClient) GetGroupsOfUser

func (lc *LDAPClient) GetGroupsOfUser(username string) ([]string, error)

GetGroupsOfUser returns the group for a user

Example

ExampleLDAPClient_GetGroupsOfUser shows how to retrieve user groups

package main

import (
	"log"

	"github.com/jtblin/go-ldap-client"
)

func main() {
	client := &ldap.LDAPClient{
		Base:        "dc=example,dc=com",
		Host:        "ldap.example.com",
		Port:        389,
		GroupFilter: "(memberUid=%s)",
	}
	defer client.Close()
	groups, err := client.GetGroupsOfUser("username")
	if err != nil {
		log.Fatalf("Error getting groups for user %s: %+v", "username", err)
	}
	log.Printf("Groups: %+v", groups)
}

func (*LDAPClient) SearchUser

func (lc *LDAPClient) SearchUser(username string) (map[string]string, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL