Skip to content

Commit

Permalink
update login token
Browse files Browse the repository at this point in the history
  • Loading branch information
王博坤 Bokun committed Aug 6, 2022
1 parent a579c8c commit 91d1ce6
Showing 1 changed file with 39 additions and 38 deletions.
77 changes: 39 additions & 38 deletions ldap/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package ldap

import (
"fmt"

"github.com/wangbokun/go/codec"
"gopkg.in/ldap.v2"
"github.com/wangbokun/go/log"
"github.com/wangbokun/go/log"
"gopkg.in/ldap.v2"
)

type Ldap struct {
Opts Options
Opts Options
LdapConn *ldap.Conn
}

Expand All @@ -17,7 +18,7 @@ func New(Opts ...Option) *Ldap {
options := NewOptions(Opts...)
return &Ldap{
Opts: options,
}
}
}

// Init init
Expand All @@ -27,108 +28,108 @@ func (l *Ldap) Init(Opts ...Option) {
}
}


func (l *Ldap) LoadConfig(v interface{}) error {
return codec.NewJSONCodec().Format(&l.Opts, v)
}

func (l *Ldap) Connect() (err error){
func (l *Ldap) Connect() (err error) {
l.LdapConn, err = ldap.Dial("tcp", l.Opts.Server)
if err != nil {
return err
return err
}
err = l.LdapConn.Bind(l.Opts.BindDn, l.Opts.BindPass)
if err != nil {
log.Error("%s",err)
log.Error("%s", err)
}
return nil
}

//AuthFilter user: (&(objectClass=organizationalPerson)(sAMAccountName=%s))
//AuthFilter all user: "(&(objectClass=user))"
func (l *Ldap) Search() (*ldap.SearchResult, error) {

searchRequest := ldap.NewSearchRequest(
searchRequest := ldap.NewSearchRequest(
l.Opts.BaseDN, // The base dn to search
ldap.ScopeWholeSubtree,ldap.NeverDerefAliases,0,0,false,
fmt.Sprintf(l.Opts.AuthFilter), //"(cn=*)" The filter to apply
l.Opts.SearchField, // A list attributes to retrieve
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf(l.Opts.AuthFilter), //"(cn=*)" The filter to apply
l.Opts.SearchField, // A list attributes to retrieve
nil,
)
// res, err := conn.Search(searchRequest)
res, err := l.LdapConn.SearchWithPaging(searchRequest,10)
if err != nil {
return nil, err
// res, err := conn.Search(searchRequest)
res, err := l.LdapConn.SearchWithPaging(searchRequest, 10)
if err != nil {
return nil, err
}
return res, nil

}

func (l *Ldap) SearchUser(baseDN string,authFilter string,searchField []string) (*ldap.SearchResult, error) {
func (l *Ldap) SearchUser(authFilter string, searchField []string) (*ldap.SearchResult, error) {

searchRequest := ldap.NewSearchRequest(
baseDN, // The base dn to search
ldap.ScopeWholeSubtree,ldap.NeverDerefAliases,0,0,false,
fmt.Sprintf(authFilter), //"(cn=*)" The filter to apply
searchField, // A list attributes to retrieve
searchRequest := ldap.NewSearchRequest(
l.Opts.BaseDN, // The base dn to search
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf(authFilter), //"(cn=*)" The filter to apply
searchField, // A list attributes to retrieve
nil,
)
// res, err := conn.Search(searchRequest)
res, err := l.LdapConn.SearchWithPaging(searchRequest,10)
if err != nil {
return nil, err
// res, err := conn.Search(searchRequest)
res, err := l.LdapConn.SearchWithPaging(searchRequest, 10)
if err != nil {
return nil, err
}
return res, nil

}

func (l *Ldap) Auth(username, password string) error {

searchRequest := ldap.NewSearchRequest(
searchRequest := ldap.NewSearchRequest(
l.Opts.BaseDN, //binddn, //"dc=example,dc=com", // The base dn to search
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
// fmt.Sprintf("(&(objectClass=organizationalPerson)(sAMAccountName=%s))", username),
fmt.Sprintf(l.Opts.AuthFilter, username),
[]string{"dn","cn"}, // A list attributes to retrieve
[]string{"dn", "cn"}, // A list attributes to retrieve
nil,
)

sr, err := l.LdapConn.Search(searchRequest)
if err != nil {
return err
return err
}

if len(sr.Entries) != 1 {
if len(sr.Entries) != 1 {
return fmt.Errorf("user is not exist")
}
fmt.Println(sr.Entries[0].DN,password)
fmt.Println(sr.Entries[0].DN, password)
if err := l.LdapConn.Bind(sr.Entries[0].DN, password); err != nil {
return err
return err
}
return nil
}


func (l *Ldap) Add(addRequest *ldap.AddRequest) error {
err := l.LdapConn.Add(addRequest)
if err != nil {
return err
}
return nil
}

// func (l *Ldap) LdapClose() error {
// l.Conn.Close()
// return nil
// }

func ChineseName(str string) (s string){
func ChineseName(str string) (s string) {
r := []rune(str)
strSlice := []string{}
cnstr := ""
for i := 0; i < len(r); i++ {
if r[i] <= 40869 && r[i] >= 19968 {
cnstr = cnstr + string(r[i])
strSlice = append(strSlice, cnstr)
}
if r[i] <= 40869 && r[i] >= 19968 {
cnstr = cnstr + string(r[i])
strSlice = append(strSlice, cnstr)
}
}
if 0 == len(strSlice) {
return ""
Expand Down

0 comments on commit 91d1ce6

Please sign in to comment.