Documentation ¶
Overview ¶
Package ldap4gin is authenticator for gin framework using ldap server
Index ¶
Constants ¶
const SessionKeyName = "ldap4gin_user"
SessionKeyName names key used to store user profile in session
Variables ¶
var ( ErrMalformed = fmt.Errorf("malformed username") ErrInvalidCredentials = fmt.Errorf("invalid credentials") ErrNotFound = fmt.Errorf("user not found") ErrMultipleAccount = fmt.Errorf("multiple user profiles found") ErrReadonlyWrongCredentials = fmt.Errorf("readonly user has wrong credentials") )
var DefaultLogDebugFunc = func(ctx context.Context, format string, a ...any) { log.Default().Printf("ldap4gin: "+format+"\n", a...) }
DefaultLogDebugFunc is used for logging by default
Functions ¶
func GetDefaultFields ¶
func GetDefaultFields() []string
GetDefaultFields returns fields we extract from LDAP by default
Types ¶
type Authenticator ¶
type Authenticator struct { // Options are runtime options as received from New Options *Options // LDAPConn is ldap connection being used LDAPConn *ldap.Conn // LogDebugFunc is function to log debug information LogDebugFunc LogDebugFunc // contains filtered or unexported fields }
Authenticator links ldap and gin context together
func New ¶
func New(opts *Options) (a *Authenticator, err error)
New creates new authenticator using options provided
func (*Authenticator) Authorize ¶
func (a *Authenticator) Authorize(c *gin.Context, username, password string) (err error)
Authorize tries to find user in ldap database, check his/her password via `bind` and populate session, if password matches
func (*Authenticator) Close ¶ added in v1.1.0
func (a *Authenticator) Close() (err error)
Close closes authenticator connection to ldap
type LogDebugFunc ¶ added in v1.2.0
LogDebugFunc used to define requirements for logging function
type Options ¶
type Options struct { // Debug outputs debugging information, better leave it to false Debug bool // TTL depicts how long user profile is cached in session, when it expires, it is reloaded from ldap TTL time.Duration //ConnectionString depicts how we dial LDAP server, something like ldap://127.0.0.1:389 or ldaps://ldap.example.org:636 ConnectionString string // TLS is configuration for encryption to use TLS *tls.Config // StartTLS shows, do we need to execute StartTLS or not StartTLS bool // ReadonlyDN is distinguished name used for authorization as readonly user, // who has access to listing groups of user. For example, "cn=readonly,dc=vodolaz095,dc=ru" ReadonlyDN string // ReadonlyPasswd is password for readonly user, who has access to listing groups ReadonlyPasswd string // UserBaseTpl is template to extract user profiles by UID, for example // "uid=%s,ou=people,dc=vodolaz095,dc=ru" or // "email=%s,ou=people,dc=vodolaz095,dc=ru" UserBaseTpl string // ExtraFields is array of fields, we also extract from database. // NOTICE - if you add too many fields, it can hit session size limits! ExtraFields []string // ExtractGroups toggles extracting groups of user ExtractGroups bool // GroupsOU depicts organization unit for groups, usually "ou=groups,dc=vodolaz095,dc=ru" GroupsOU string // LogDebugFunc is called to log debug events LogDebugFunc LogDebugFunc }
Options depicts parameters used to instantiate Authenticator
type User ¶
type User struct { // General DN string // dn: uid=sveta,ou=people,dc=vodolaz095,dc=life UID string //uid: sveta // Names GivenName string // `givenname` - Svetlana CommonName string // `cn` - Svetlana Belaya Initials string // `initials` - SA Surname string // `sn` - Belaya // work specific Organization string // o: R&D OrganizationUnit string // ou: Laboratory 47 Title string // title: developer Description string // description: writes code // Internet related Website string // labeleduri: https://vodolaz095.life Emails []string // `mail` user can have few emails // Linux specific UIDNumber uint64 // uidnumber 1000 GIDNumber uint64 // gidnumber 1000 HomeDirectory string // homedirectory: /home/sveta LoginShell string // loginshell - /bin/bash // groups Groups []Group // Raw entry extracted from LDAP Entry *ldap.Entry ExpiresAt time.Time }
User depicts profile of authorized user
func (*User) Expired ¶ added in v1.1.0
Expired returns true, if user profile should be reloaded from ldap database
func (*User) HasGroupByGID ¶ added in v1.1.0
HasGroupByGID checks, if user is a member of group with this GID
func (*User) HasGroupByName ¶ added in v1.1.0
HasGroupByName checks, if user is a member of group with this name