ldap

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

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

Go to latest
Published: May 13, 2024 License: BSD-2-Clause Imports: 4 Imported by: 1

README

go-ldap-client

Fork of Simple ldap client to authenticate, performa basic operations on ldap servers. Provide utilities in order to authenticate, get groups, get & set attributes, and add new attributes to the user schema.

Usage

The only external dependency is gopkg.in/ldap.v3. For the usage you can refer to code below

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")
	check(err)
	if !ok {
		log.Fatalf("Authenticating failed for user %s", "username")
	}
	log.Printf("User: %+v", user)

	groups, err := client.GetUserGroups("username")
	check(err)
	log.Printf("User groups: %+v " groups)
	attr, err := GetUserAttribute("username", "sshPublicKey")
	check(err)
	log.Printf("User requested schema attribute: %+v " attr)
	_, err = ldap.SetUserAttribute("username", "sshPublicKey", "ssh-rsa 3qefbgnqn...etc...")
	check(err)
	format := "20060102150405Z"
	now := time.Now().Format(format)
	_, err = ldap.AddUserAttribute("username", "pwdAccountLockedTime", now)
	check(err)
}
func check(e error) {
	if e != nil {
		log.Fatal(e)
		panic(e)
	}
}

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?

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LDAPClient

type LDAPClient struct {
	Attributes         []string
	Base               string
	BindDN             string
	BindPassword       string
	PolicyBase         string
	GroupFilter        string // e.g. "(memberUid=%s)"
	Host               string
	ServerName         string
	UserFilter         string // e.g. "(uid=%s)"
	Conn               *ldap.Conn
	Port               int
	InsecureSkipVerify bool
	UseSSL             bool
	SkipTLS            bool
	ClientCertificates []tls.Certificate // Adding client certificates
}

func (*LDAPClient) AddUserAttribute

func (lc *LDAPClient) AddUserAttribute(username string, attribute string, value string) (string, error)

func (*LDAPClient) Authenticate

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

Authenticate authenticates the user against the ldap backend.

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) GetPolicyAttribute

func (lc *LDAPClient) GetPolicyAttribute(parameter string, attribute string) (string, error)

GetUserAttribute returns user specified attribute.

func (*LDAPClient) GetUserAttribute

func (lc *LDAPClient) GetUserAttribute(username string, attribute string) (string, error)

GetUserAttribute returns user specified attribute.

func (*LDAPClient) GetUserGroups

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

GetGroupsOfUser returns the group for a user.

func (*LDAPClient) SetUserAttribute

func (lc *LDAPClient) SetUserAttribute(username string, attribute string, newValue string) (string, error)

SetUserAttribute returns true if modification has been made successfully

Jump to

Keyboard shortcuts

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