ldapClient

package module
v0.0.0-...-50d7d1d Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2021 License: MIT Imports: 4 Imported by: 0

README

go-ldap-client

codecov.io Go Report Card Build Status

A simple GoLang LDAP client for authenticating users. It is effectively a wrapper around gopkg.in/ldap.v2 and aims at making LDAP easier to use.

At the moment the main aim is for making user authentication as easy as possible by providing an Authenticate function that looks up the user, tests their password and then resets the bind user back to the base config. One bit of useful functionality is that the client exposes ldap.Client from gopkg.in/ldap.v2 via ldapClient.Conn, this should allow you to use any functionality of the base package and use this a simpler initilaiser.

Usage

Go Doc

Example
package main

import (
  "fmt"
  "os"
  "github.com/FidelityInternational/go-ldap-client"
)

func main() {
  config := &ldapClient.Config{
    Base:         "dc=example,dc=com",
    Host:         "ldap.example.com",
    Port:         389,
    UseSSL:       false,
    BindDN:       "uid=exampleUser,ou=examplePeople,dc=example,dc=com",
    BindPassword: "exampleUserPassword",
    UserFilter:   "(userName=%s)",
    GroupFilter:  "(groupName=%s)",
    Attributes:   []string{"userName", "sn", "mail", "id"},
  }
  client, err := ldapClient.New(config)
  if err != nil {
    fmt.Println(err)
    os.Exit(1)
  }
  defer client.Close()
  authenticated, user, err := client.Authenticate("aUsername", "aPassword")
  if err != nil {
    fmt.Println(err)
    os.Exit(1)
  }
  if !authenticated {
    fmt.Printf("Authentication failed for user: %v\n", "aUsername")
  }
  fmt.Printf("Authentication successful for user: %v\n", "aUsername")
  fmt.Printf("%+v\n", user)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Conn   ldap.Client
	Config *Config
	// contains filtered or unexported fields
}

Client - the ldap client

func New

func New(config *Config) (*Client, error)

New - Creates a new ldap client

func (*Client) Authenticate

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

Authenticate - authenticates a user against ldap

func (*Client) Bind

func (c *Client) Bind() error

Bind - bind to LDAP as the Config user

func (*Client) Close

func (c *Client) Close()

Close - close the backend ldap connection

type Config

type Config struct {
	Attributes         []string
	Base               string
	BindDN             string
	BindPassword       string
	GroupFilter        string // e.g. "(memberUid=%s)"
	Host               string
	UserFilter         string // e.g. "(uid=%s)"
	Port               int
	InsecureSkipVerify bool
	UseSSL             bool
	ClientCertificates []tls.Certificate // Adding client certificates
	CACertificates     []byte
}

Config - ldap client config

type LDAPClient

type LDAPClient interface {
	Bind() error
	Authenticate(string, string) (bool, map[string]string, error)
	Close()
}

LDAPClient - the ldap client interface

Jump to

Keyboard shortcuts

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