Skip to content
Snippets Groups Projects
Select Git revision
  • 8c02839a724c9936bcbc1dfad97bd0f8a2cbeea7
  • noblogs default
  • noblogs-5.7.1
  • upstream
  • noblogs-5.7
  • noblogs-5.6new
  • upstream5.5.1
  • noblogs28dic
  • upstream28dic
  • noblogs-5.5.1
  • noblogs-5.4.2
  • noblogs-5.4_seconda
  • noblogs-5.4
  • noblogs-7c
  • wp5.2.3p3
  • mergedbconf
  • noblogs-5.7.1
  • noblogs.5.7.0p1
  • noblogs-5.7.0
  • noblogs-5.6p3
  • noblogs5.6p2
  • noblogs-5.6p1
  • noblogs-5.6
  • noblogs-5.4.2p1
  • noblogs-5.4.2
  • noblogs-5.4.1
  • noblogs-5.4
  • noblogs-p5.4
  • noblogs-5.3.2p2
  • noblogs-5.3.2p1
  • noblogs-5.3.2
  • noblogs-5.3
  • noblogs-5.2.3p4
  • noblogs-5.2.3p3
  • noblogs-5.2.3p2
  • noblogs-5.2.3p1
36 results

class-wp-http-ixr-client.php

Blame
  • ldap.go 1.37 KiB
    package policydproxy
    
    import (
    	"context"
    	"errors"
    	"strings"
    
    	"git.autistici.org/ai3/go-common/ldap"
    	"gopkg.in/ldap.v3"
    )
    
    const poolSize = 3
    
    type ldapDirector struct {
    	pool      *ldaputil.ConnectionPool
    	baseDN    string
    	filter    string
    	attr      string
    	resultFmt string
    }
    
    func NewLDAPDirector(uri, bindDN, bindPW, baseDN, filter, attr, resultFmt string) (Director, error) {
    	if !strings.Contains(resultFmt, "%s") {
    		return nil, errors.New("result_fmt does not contain the '%s' token")
    	}
    	if baseDN == "" {
    		return nil, errors.New("LDAP base DN is unset")
    	}
    	if filter == "" {
    		return nil, errors.New("LDAP filter is unset")
    	}
    	if attr == "" {
    		return nil, errors.New("LDAP query attribute is unset")
    	}
    
    	pool, err := ldaputil.NewConnectionPool(uri, bindDN, bindPW, poolSize)
    	if err != nil {
    		return nil, err
    	}
    	return &ldapDirector{
    		pool:      pool,
    		baseDN:    baseDN,
    		filter:    filter,
    		attr:      attr,
    		resultFmt: resultFmt,
    	}, nil
    }
    
    func (d *ldapDirector) Lookup(ctx context.Context, email string) (string, error) {
    	result, err := d.pool.Search(ctx, ldap.NewSearchRequest(
    		d.baseDN,
    		ldap.ScopeWholeSubtree,
    		1, 0, 0, false,
    		d.filter,
    		[]string{d.attr},
    		nil,
    	))
    	if err != nil {
    		return "", err
    	}
    	if len(result.Entries) == 0 {
    		return "", nil
    	}
    
    	return strings.Replace(d.resultFmt, "%s", result.Entries[0].GetAttributeValue(d.attr), -1), nil
    }