本文整理匯總了Golang中github.com/go-ldap/ldap.NewSearchRequest函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewSearchRequest函數的具體用法?Golang NewSearchRequest怎麽用?Golang NewSearchRequest使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewSearchRequest函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: SearchGroup
func (ls *LdapSource) SearchGroup(name string) (*models.Group, error) {
l, err := ls.dial()
if err != nil {
return nil, err
}
err = l.Bind(ls.BindDN, ls.Passwd)
if err != nil {
log.Printf("ERROR: Cannot bind: %s\n", err.Error())
return nil, err
}
search := ldap.NewSearchRequest(
fmt.Sprintf(groupDnFmt, name, ls.Base),
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
"(objectclass=groupOfNames)",
[]string{"member"},
nil)
sr, err := l.Search(search)
if err != nil {
// log.Printf("LDAP search error: %s", err)
return nil, err
}
vals := sr.Entries[0].GetAttributeValues("member")
members := make([]string, len(vals))
for i, dn := range vals {
members[i] = dn[strings.Index(dn, "=")+1 : strings.Index(dn, ",")]
}
return &models.Group{name, members}, nil
}
示例2: ListPaged
func (ls *LdapSource) ListPaged(limit int) (staffs []*models.Staff) {
err := ls.Bind(ls.BindDN, ls.Passwd, false)
if err != nil {
// log.Printf("ERROR: Cannot bind: %s\n", err.Error())
return nil
}
if limit < 1 {
limit = 1
}
search := ldap.NewSearchRequest(
"ou=people,"+ls.Base,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
ls.Filter,
ls.Attributes,
nil)
sr, err := ls.c.SearchWithPaging(search, uint32(limit))
if err != nil {
log.Printf("ERROR: %s for search %v\n", err, search)
return
}
if len(sr.Entries) > 0 {
staffs = make([]*models.Staff, len(sr.Entries))
for i, entry := range sr.Entries {
staffs[i] = entryToUser(entry)
}
}
return
}
示例3: Example_userAuthentication
// Example User Authentication shows how a typical application can verify a login attempt
func Example_userAuthentication() {
// The username and password we want to check
username := "someuser"
password := "userpassword"
bindusername := "readonly"
bindpassword := "password"
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", "ldap.example.com", 389))
if err != nil {
log.Fatal(err)
}
defer l.Close()
// Reconnect with TLS
err = l.StartTLS(&tls.Config{InsecureSkipVerify: true})
if err != nil {
log.Fatal(err)
}
// First bind with a read only user
err = l.Bind(bindusername, bindpassword)
if err != nil {
log.Fatal(err)
}
// Search for the given username
searchRequest := ldap.NewSearchRequest(
"dc=example,dc=com",
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf("(&(objectClass=organizationalPerson)&(uid=%s))", username),
[]string{"dn"},
nil,
)
sr, err := l.Search(searchRequest)
if err != nil {
log.Fatal(err)
}
if len(sr.Entries) != 1 {
log.Fatal("User does not exist or too many entries returned")
}
userdn := sr.Entries[0].DN
// Bind as the user to verify their password
err = l.Bind(userdn, password)
if err != nil {
log.Fatal(err)
}
// Rebind as the read only user for any futher queries
err = l.Bind(bindusername, bindpassword)
if err != nil {
log.Fatal(err)
}
}
示例4: NewSearchRequest
// NewSearchRequest creates a new search request for the LDAP query and optionally includes more attributes
func (q *LDAPQuery) NewSearchRequest(additionalAttributes []string) *ldap.SearchRequest {
return ldap.NewSearchRequest(
q.BaseDN,
int(q.Scope),
int(q.DerefAliases),
0, // allowed return size - indicates no limit
q.TimeLimit,
false, // not types only
q.Filter,
additionalAttributes,
nil, // no controls
)
}
示例5: buildDNQuery
// buildDNQuery builds the query that finds an LDAP entry with the given DN
// this is done by setting the DN to be the base DN for the search and setting the search scope
// to only consider the base object found
func (o *LDAPQueryOnAttribute) buildDNQuery(dn string, attributes []string) *ldap.SearchRequest {
return ldap.NewSearchRequest(
dn,
ldap.ScopeBaseObject, // over-ride original
int(o.DerefAliases),
0, // allowed return size - indicates no limit
o.TimeLimit,
false, // not types only
"objectClass=*", // filter that returns all values
attributes,
nil, // no controls
)
}
示例6: _getBaseDN
func (_s *LDAP) _getBaseDN(rLog SBMSystem.LogFile, search, basedn string) string {
var uattr = []string{"dn"}
lsearch := ldap.NewSearchRequest(basedn, 2, ldap.NeverDerefAliases, 0, 0, false, search, uattr, nil)
sr, err := _s.D.Search(lsearch)
if err != nil {
rLog.LogDbg(0, "LDAP::Search() ", basedn, " error: ", err)
}
if len(sr.Entries) > 0 {
for _, entry := range sr.Entries {
return entry.DN
}
}
return ""
}
示例7: getEntry
func (ls *LdapSource) getEntry(udn string) (*ldap.Entry, error) {
search := ldap.NewSearchRequest(
udn,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
ls.Filter,
ls.Attributes,
nil)
sr, err := ls.c.Search(search)
if err != nil {
log.Printf("LDAP Search '%s' Error: ", udn, err)
return nil, err
}
if len(sr.Entries) > 0 {
return sr.Entries[0], nil
}
return nil, ErrNotFound
}
示例8: inGroup
func inGroup(username, group string, config *Config, conn *ldap.Conn, attrs []string) (bool, map[string][]string, error) {
groupDN, err := getDN(group, config, conn)
if err != nil {
if config.Debug {
log.Printf("DEBUG: Error: %s\n", err)
}
return false, nil, err
}
search := ldap.NewSearchRequest(
config.BaseDN,
ldap.ScopeWholeSubtree,
ldap.DerefAlways,
1, 0,
false,
fmt.Sprintf("(sAMAccountName=%s)", username),
append(attrs, "memberOf"),
nil,
)
result, lErr := conn.Search(search)
if lErr != nil {
if config.Debug {
log.Printf("DEBUG: LDAP Error %v\n", lErr)
}
return false, nil, lErr
}
if len(result.Entries) == 1 {
entryAttrs := attrsToMap(result.Entries[0])
if groups, ok := entryAttrs["memberOf"]; ok {
for _, g := range groups {
if groupDN == g {
for _, key := range attrs {
if key == "memberOf" {
return true, entryAttrs, nil
}
}
delete(entryAttrs, "memberOf")
return true, entryAttrs, nil
}
}
}
return false, entryAttrs, nil
}
return false, nil, LDAPError("Amount of Entries returned was not one")
}
示例9: _checkGroupMember
func (_s *LDAP) _checkGroupMember(rLog SBMSystem.LogFile, userDN, groupDN, baseDN string, recurse_count int) int {
var (
uattr = []string{"memberOf"}
result = int(-1)
)
if userDN == "" || groupDN == "" {
return -1
}
if recurse_count <= 0 {
return -1
}
lsearch := ldap.NewSearchRequest(userDN, 0, ldap.NeverDerefAliases, 0, 0, false, "(objectclass=*)", uattr, nil)
sr, err := _s.D.Search(lsearch)
if err != nil {
rLog.LogDbg(0, "LDAP::Search() ", userDN, " error: ", err)
}
if len(sr.Entries) > 0 {
for _, entry := range sr.Entries {
for _, attr := range entry.Attributes {
if attr.Name == "memberOf" {
for _, x := range attr.Values {
if groupDN == x {
return 0
} else {
if x != userDN {
result = _s._checkGroupMember(rLog, x, groupDN, baseDN, recurse_count-1)
if result == 0 {
return 0
}
}
}
}
}
}
}
}
return -1
}
示例10: buildAttributeQuery
// buildAttributeQuery builds the query containing a filter that conjoins the common filter given
// in the configuration with the specific attribute filter for which the attribute value is given
func (o *LDAPQueryOnAttribute) buildAttributeQuery(attributeValue string,
attributes []string) *ldap.SearchRequest {
specificFilter := fmt.Sprintf("%s=%s",
ldap.EscapeFilter(o.QueryAttribute),
ldap.EscapeFilter(attributeValue))
filter := fmt.Sprintf("(&(%s)(%s))", o.Filter, specificFilter)
return ldap.NewSearchRequest(
o.BaseDN,
int(o.Scope),
int(o.DerefAliases),
0, // allowed return size - indicates no limit
o.TimeLimit,
false, // not types only
filter,
attributes,
nil, // no controls
)
}
示例11: searchByName
// Search LDAP by cn filter
func searchByName(l *ldap.Conn, name string) (*ldap.SearchResult, error) {
filter := fmt.Sprintf("(cn=%v)", ReplaceAccents(name))
search := ldap.NewSearchRequest(
baseDN,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
filter,
attributes,
nil)
sr, err := l.Search(search)
if err != nil {
return nil, err
}
switch {
case len(sr.Entries) == 0:
return sr, ErrNoResults
case len(sr.Entries) > 1:
return sr, ErrTooManyResults
}
return sr, nil
}
示例12: getDN
func getDN(cn string, config *Config, conn *ldap.Conn) (string, error) {
search := ldap.NewSearchRequest(
config.BaseDN,
ldap.ScopeWholeSubtree,
ldap.DerefAlways,
1, 0,
false,
fmt.Sprintf("(cn=%s)", cn),
nil,
nil,
)
result, err := conn.Search(search)
if err != nil {
if config.Debug {
log.Printf("DEBUG: LDAP Error %v\n", err)
}
return "", err
}
if len(result.Entries) > 0 {
return result.Entries[0].DN, nil
}
return "", ConfigError(fmt.Sprintf("No DN found for: %s", cn))
}
示例13: getAttrs
func getAttrs(username string, config *Config, conn *ldap.Conn, attrs []string) (map[string][]string, error) {
search := ldap.NewSearchRequest(
config.BaseDN,
ldap.ScopeWholeSubtree,
ldap.DerefAlways,
1, 0,
false,
fmt.Sprintf("(sAMAccountName=%s)", username),
attrs,
nil,
)
result, lErr := conn.Search(search)
if lErr != nil {
if config.Debug {
log.Printf("DEBUG: LDAP Error %v\n", lErr)
}
return nil, lErr
}
if len(result.Entries) == 1 {
return attrsToMap(result.Entries[0]), nil
}
return nil, LDAPError("Amount of Entries returned was not one")
}
示例14: ExampleConn_Search
// ExampleConn_Search demonstrates how to use the search interface
func ExampleConn_Search() {
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", "ldap.example.com", 389))
if err != nil {
log.Fatal(err)
}
defer l.Close()
searchRequest := ldap.NewSearchRequest(
"dc=example,dc=com", // The base dn to search
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
"(&(objectClass=organizationalPerson))", // The filter to apply
[]string{"dn", "cn"}, // A list attributes to retrieve
nil,
)
sr, err := l.Search(searchRequest)
if err != nil {
log.Fatal(err)
}
for _, entry := range sr.Entries {
fmt.Printf("%s: %v\n", entry.DN, entry.GetAttributeValue("cn"))
}
}
示例15: ldapSearch
//ldap search and return required attributes' value from searched entries
//default return entry's DN value if you leave attrs array empty
func (la *LDAPAuth) ldapSearch(l *ldap.Conn, baseDN *string, filter *string, attrs *[]string) (string, error) {
if l == nil {
return "", fmt.Errorf("No ldap connection!")
}
glog.V(2).Infof("Searching...basedDN:%s, filter:%s", *baseDN, *filter)
searchRequest := ldap.NewSearchRequest(
*baseDN,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
*filter,
*attrs,
nil)
sr, err := l.Search(searchRequest)
if err != nil {
return "", err
}
if len(sr.Entries) != 1 {
return "", fmt.Errorf("User does not exist or too many entries returned.")
}
var buffer bytes.Buffer
for _, entry := range sr.Entries {
if len(*attrs) == 0 {
glog.V(2).Infof("Entry DN = %s", entry.DN)
buffer.WriteString(entry.DN)
} else {
for _, attr := range *attrs {
values := strings.Join(entry.GetAttributeValues(attr), " ")
glog.V(2).Infof("Entry %s = %s", attr, values)
buffer.WriteString(values)
}
}
}
return buffer.String(), nil
}