ldap_auth.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package auth
  2. import (
  3. "git.qianqiusoft.com/qianqiusoft/light-apiengine/ldap"
  4. "git.qianqiusoft.com/qianqiusoft/light-apiengine/entitys"
  5. "git.qianqiusoft.com/qianqiusoft/light-apiengine/logs"
  6. "net"
  7. "fmt"
  8. )
  9. type LdapAuth struct {
  10. IAuth
  11. }
  12. func (this *LdapAuth)Login(c *entitys.CtrlContext) {
  13. }
  14. func (this *LdapAuth)Logout(c *entitys.CtrlContext){
  15. }
  16. func (this* LdapAuth)Init(){
  17. s := ldap.NewServer()
  18. // register Bind and Search function handlers
  19. handler := ldapHandler{}
  20. s.BindFunc("", handler)
  21. s.SearchFunc("", handler)
  22. // start the server
  23. listen := "0.0.0.0:389"
  24. logs.Info("Starting example LDAP server on %s", listen)
  25. if err := s.ListenAndServe(listen); err != nil {
  26. logs.Error("LDAP Server Failed: %s", err.Error())
  27. }
  28. }
  29. type ldapHandler struct {
  30. }
  31. ///////////// Allow anonymous binds only
  32. func (h ldapHandler) Bind(bindDN, bindSimplePw string, conn net.Conn) (ldap.LDAPResultCode, error) {
  33. fmt.Println(bindDN)
  34. fmt.Println(bindSimplePw)
  35. /*if bindDN == "" && bindSimplePw == "" {
  36. return ldap.LDAPResultSuccess, nil
  37. }*/
  38. return ldap.LDAPResultSuccess, nil
  39. }
  40. ///////////// Return some hardcoded search results - we'll respond to any baseDN for testing
  41. func (h ldapHandler) Search(boundDN string, searchReq ldap.SearchRequest, conn net.Conn) (ldap.ServerSearchResult, error) {
  42. fmt.Print("%s,search......%s", boundDN, searchReq)
  43. entries := []*ldap.Entry{
  44. &ldap.Entry{"cn=ned," + searchReq.BaseDN, []*ldap.EntryAttribute{
  45. &ldap.EntryAttribute{"cn", []string{"ned"}},
  46. &ldap.EntryAttribute{"uidNumber", []string{"5000"}},
  47. &ldap.EntryAttribute{"accountStatus", []string{"active"}},
  48. &ldap.EntryAttribute{"uid", []string{"ned"}},
  49. &ldap.EntryAttribute{"description", []string{"ned"}},
  50. &ldap.EntryAttribute{"objectClass", []string{"posixAccount"}},
  51. }},
  52. }
  53. return ldap.ServerSearchResult{entries, []string{}, []ldap.Control{}, ldap.LDAPResultSuccess}, nil
  54. }