ldap

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: AGPL-3.0 Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ldap authenticates usernames and passwords against an LDAP or Active Directory server.

LDAP is a login-time credential verifier, NOT an auth.Authenticator: a directory bind is far too expensive to run on every request, and the directory has no notion of a session. POST /auth/login calls Verify for accounts whose store.User.AuthSource is "ldap" and, on success, mints the same server-side session a local password login produces. Everything downstream — RBAC, owner binding, WebSocket scoping — is therefore unchanged.

Index

Constants

View Source
const (
	// RoleSourceDirectory recomputes an LDAP user's role from their groups on
	// every login; the users API rejects role edits on such accounts.
	RoleSourceDirectory = rolemap.SourceDirectory
	// RoleSourceLocal seeds the role from groups at JIT-create only; admins
	// own it afterwards.
	RoleSourceLocal = rolemap.SourceLocal
)

RoleSourceDirectory and RoleSourceLocal are the two role-authority modes. They are the shared internal/auth/rolemap values under LDAP-facing names; the enum has exactly one definition so LDAP and OIDC cannot drift.

Variables

View Source
var (
	// ErrInvalidCredentials means the directory rejected the bind, or no
	// entry matched the username.
	ErrInvalidCredentials = errors.New("ldap: invalid credentials")
	// ErrNoRoleMatch means the user authenticated but matched no role
	// mapping and DefaultRole is empty, so the deployment refuses the login.
	ErrNoRoleMatch = errors.New("ldap: no role mapping matched and no default role is configured")
	// ErrUnavailable means the directory could not be reached or the service
	// account bind failed — an infrastructure fault, not a bad password.
	ErrUnavailable = errors.New("ldap: directory unavailable")
)

Errors returned by a Verifier. Callers must not distinguish ErrInvalidCredentials from a missing directory entry in what they send the client: both are a generic 401, or the endpoint enumerates usernames.

Functions

func EscapeUsernameDN

func EscapeUsernameDN(username string) string

EscapeUsernameDN escapes a username for interpolation into UserDNTemplate. Without it a username containing a comma could graft extra RDNs onto the template and bind as a different entry entirely.

go-ldap/v3's EscapeDN follows the RFC 4514 grammar strictly, which does not require escaping "=" inside a value (only the top-level type/value separator is significant to a conformant parser). UserDNTemplate is built by plain %s substitution rather than a structured DN encoder, so this implements escaping locally, treating "=" as sensitive too: defense in depth over relying on every consumer of the template being a fully RFC-conformant DN parser.

func EscapeUsernameFilter

func EscapeUsernameFilter(username string) string

EscapeUsernameFilter escapes a username for interpolation into a search filter. This is not optional hygiene: UserFilter is operator-supplied with a %s placeholder, so an unescaped `*)(uid=*` would rewrite "(uid=%s)" into a filter matching every entry in the base DN — an authentication bypass, not merely a malformed query.

Types

type Config

type Config struct {
	URL             string
	StartTLS        bool
	TLSSkipVerify   bool
	CAFile          string
	Timeout         time.Duration
	BindDN          string
	BindPassword    string
	BaseDN          string
	UserFilter      string
	NestedGroups    bool
	UserDNTemplate  string
	UsernameAttr    string
	DisplayNameAttr string
	// UniqueIDAttr names the attribute holding the entry's stable,
	// non-reusable identifier: objectGUID on Active Directory, entryUUID on
	// OpenLDAP and other RFC 4530 servers. Required — there is no value
	// correct on both, and auto-detection would silently guess wrong on a
	// server exposing both.
	UniqueIDAttr string
	RoleSource   string
	RoleMap      []rolemap.Mapping
	DefaultRole  string
	// Logger receives the operational warnings this package cannot resolve on
	// its own — currently only a failed nested-group expansion, which degrades
	// silently to flat memberOf and would otherwise demote admins who hold
	// admin solely through a nested group with no signal anywhere. Optional:
	// nil is safe and disables that logging.
	Logger *slog.Logger
}

Config configures directory authentication. It mirrors config.LDAPConfig; the server converts between them so this package stays independent of the config loader.

func (Config) MapRole

func (c Config) MapRole(groups []string) (string, bool)

MapRole returns the role for the entry's groups. Delegates to internal/auth/rolemap so LDAP and OIDC cannot drift apart on precedence.

func (Config) TemplateMode

func (c Config) TemplateMode() bool

TemplateMode reports whether this config selects template bind (bind directly as the user) rather than search-then-bind.

type Identity

type Identity struct {
	// DN is the entry's distinguished name.
	DN string
	// Username is the login name as the directory spells it. Preferred over
	// the string the caller typed, so casing is consistent across logins.
	Username string
	// DisplayName is the human-facing label. Consumed only at JIT-create.
	DisplayName string
	// ExternalID is the entry's stable, non-reusable identifier, read from the
	// attribute named by Config.UniqueIDAttr. Empty means the directory did not
	// return it, which the login path treats as a failure rather than falling
	// back to name matching.
	ExternalID string
	// Groups are the group DNs the entry belongs to.
	Groups []string
}

Identity is what a successful Verify reports about the directory entry.

type RoleMapping

type RoleMapping = rolemap.Mapping

RoleMapping is one group-DN → role rule. It is an alias for rolemap.Mapping, not a distinct type: the two were field-identical, and keeping them separate meant converting the whole slice on every login purely to change its name.

type Verifier

type Verifier interface {
	Verify(ctx context.Context, username, password string) (Identity, error)
}

Verifier authenticates a username/password against a directory and reports the entry's DN and group memberships.

Implementations must return ErrInvalidCredentials for anything the user could have caused (bad password, no such entry) and ErrUnavailable for infrastructure faults, so the caller can log the second and stay silent about the first.

func New

func New(cfg Config) (Verifier, error)

New returns the Verifier selected by cfg: template bind when UserDNTemplate is set, search-then-bind otherwise. Config validation has already rejected the ambiguous and empty cases.

Jump to

Keyboard shortcuts

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