ldap

package module
v0.0.0-...-ed30882 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2020 License: BSD-3-Clause Imports: 4 Imported by: 0

README

go-ldap-client

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

Usage

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

package main

import (
	"log"

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

func main() {
	client := &ldap.Client{
		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) 
}

SSL(LDAPS)

If you use SSL, you will need to pass the server name for certificate verification or skip domain name verification e.g.client.ServerName = "ldap.example.com".

Why?

Because go-ldap-client been a long time didn't maintenance from 2017 to now. So re-create it, make it better for everyone to use and maintain.

Later

we plan to create go-ldap-client base on gopkg.in/ldap.v3.

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Attributes         []string
	Base               string
	BindDN             string
	BindPassword       string
	GroupFilter        string // e.g. "(memberUid=%s)"
	Host               string
	ServerName         string // server name for SSL (if use-ssl is set)
	UserFilter         string // e.g. "(uid=%s)"
	Conn               *ldap.Conn
	Port               int
	InsecureSkipVerify bool
	UseSSL             bool
	SkipTLS            bool
	ClientCertificates []tls.Certificate // Adding client certificates
}

Client ..

func (*Client) Authenticate

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

Authenticate authenticates the user against the ldap backend.

func (*Client) Close

func (lc *Client) Close()

Close closes the ldap backend connection.

func (*Client) Connect

func (lc *Client) Connect() error

Connect connects to the ldap backend.

func (*Client) FindUser

func (lc *Client) FindUser(username string) (map[string]string, error)

FindUser with specified username against the ldap backend

func (*Client) GetGroupsOfUser

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

GetGroupsOfUser returns the group for a user.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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