example_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package ldap_test
  2. import (
  3. "crypto/tls"
  4. "fmt"
  5. "log"
  6. "notabug.org/makenotabuggreatagain/ldap"
  7. )
  8. // ExampleConn_Bind demonstrates how to bind a connection to an ldap user
  9. // allowing access to restricted attributes that user has access to
  10. func ExampleConn_Bind() {
  11. l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", "ldap.example.com", 389))
  12. if err != nil {
  13. log.Fatal(err)
  14. }
  15. defer l.Close()
  16. err = l.Bind("cn=read-only-admin,dc=example,dc=com", "password")
  17. if err != nil {
  18. log.Fatal(err)
  19. }
  20. }
  21. // ExampleConn_Search demonstrates how to use the search interface
  22. func ExampleConn_Search() {
  23. l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", "ldap.example.com", 389))
  24. if err != nil {
  25. log.Fatal(err)
  26. }
  27. defer l.Close()
  28. searchRequest := ldap.NewSearchRequest(
  29. "dc=example,dc=com", // The base dn to search
  30. ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
  31. "(&(objectClass=organizationalPerson))", // The filter to apply
  32. []string{"dn", "cn"}, // A list attributes to retrieve
  33. nil,
  34. )
  35. sr, err := l.Search(searchRequest)
  36. if err != nil {
  37. log.Fatal(err)
  38. }
  39. for _, entry := range sr.Entries {
  40. fmt.Printf("%s: %v\n", entry.DN, entry.GetAttributeValue("cn"))
  41. }
  42. }
  43. // ExampleStartTLS demonstrates how to start a TLS connection
  44. func ExampleConn_StartTLS() {
  45. l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", "ldap.example.com", 389))
  46. if err != nil {
  47. log.Fatal(err)
  48. }
  49. defer l.Close()
  50. // Reconnect with TLS
  51. err = l.StartTLS(&tls.Config{InsecureSkipVerify: true})
  52. if err != nil {
  53. log.Fatal(err)
  54. }
  55. // Operations via l are now encrypted
  56. }
  57. // ExampleConn_Compare demonstrates how to compare an attribute with a value
  58. func ExampleConn_Compare() {
  59. l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", "ldap.example.com", 389))
  60. if err != nil {
  61. log.Fatal(err)
  62. }
  63. defer l.Close()
  64. matched, err := l.Compare("cn=user,dc=example,dc=com", "uid", "someuserid")
  65. if err != nil {
  66. log.Fatal(err)
  67. }
  68. fmt.Println(matched)
  69. }
  70. func ExampleConn_PasswordModify_admin() {
  71. l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", "ldap.example.com", 389))
  72. if err != nil {
  73. log.Fatal(err)
  74. }
  75. defer l.Close()
  76. err = l.Bind("cn=admin,dc=example,dc=com", "password")
  77. if err != nil {
  78. log.Fatal(err)
  79. }
  80. passwordModifyRequest := ldap.NewPasswordModifyRequest("cn=user,dc=example,dc=com", "", "NewPassword")
  81. _, err = l.PasswordModify(passwordModifyRequest)
  82. if err != nil {
  83. log.Fatalf("Password could not be changed: %s", err.Error())
  84. }
  85. }
  86. func ExampleConn_PasswordModify_generatedPassword() {
  87. l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", "ldap.example.com", 389))
  88. if err != nil {
  89. log.Fatal(err)
  90. }
  91. defer l.Close()
  92. err = l.Bind("cn=user,dc=example,dc=com", "password")
  93. if err != nil {
  94. log.Fatal(err)
  95. }
  96. passwordModifyRequest := ldap.NewPasswordModifyRequest("", "OldPassword", "")
  97. passwordModifyResponse, err := l.PasswordModify(passwordModifyRequest)
  98. if err != nil {
  99. log.Fatalf("Password could not be changed: %s", err.Error())
  100. }
  101. generatedPassword := passwordModifyResponse.GeneratedPassword
  102. log.Printf("Generated password: %s\n", generatedPassword)
  103. }
  104. func ExampleConn_PasswordModify_setNewPassword() {
  105. l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", "ldap.example.com", 389))
  106. if err != nil {
  107. log.Fatal(err)
  108. }
  109. defer l.Close()
  110. err = l.Bind("cn=user,dc=example,dc=com", "password")
  111. if err != nil {
  112. log.Fatal(err)
  113. }
  114. passwordModifyRequest := ldap.NewPasswordModifyRequest("", "OldPassword", "NewPassword")
  115. _, err = l.PasswordModify(passwordModifyRequest)
  116. if err != nil {
  117. log.Fatalf("Password could not be changed: %s", err.Error())
  118. }
  119. }
  120. func ExampleConn_Modify() {
  121. l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", "ldap.example.com", 389))
  122. if err != nil {
  123. log.Fatal(err)
  124. }
  125. defer l.Close()
  126. // Add a description, and replace the mail attributes
  127. modify := ldap.NewModifyRequest("cn=user,dc=example,dc=com")
  128. modify.Add("description", []string{"An example user"})
  129. modify.Replace("mail", []string{"user@example.org"})
  130. err = l.Modify(modify)
  131. if err != nil {
  132. log.Fatal(err)
  133. }
  134. }
  135. // Example User Authentication shows how a typical application can verify a login attempt
  136. func Example_userAuthentication() {
  137. // The username and password we want to check
  138. username := "someuser"
  139. password := "userpassword"
  140. bindusername := "readonly"
  141. bindpassword := "password"
  142. l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", "ldap.example.com", 389))
  143. if err != nil {
  144. log.Fatal(err)
  145. }
  146. defer l.Close()
  147. // Reconnect with TLS
  148. err = l.StartTLS(&tls.Config{InsecureSkipVerify: true})
  149. if err != nil {
  150. log.Fatal(err)
  151. }
  152. // First bind with a read only user
  153. err = l.Bind(bindusername, bindpassword)
  154. if err != nil {
  155. log.Fatal(err)
  156. }
  157. // Search for the given username
  158. searchRequest := ldap.NewSearchRequest(
  159. "dc=example,dc=com",
  160. ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
  161. fmt.Sprintf("(&(objectClass=organizationalPerson)(uid=%s))", username),
  162. []string{"dn"},
  163. nil,
  164. )
  165. sr, err := l.Search(searchRequest)
  166. if err != nil {
  167. log.Fatal(err)
  168. }
  169. if len(sr.Entries) != 1 {
  170. log.Fatal("User does not exist or too many entries returned")
  171. }
  172. userdn := sr.Entries[0].DN
  173. // Bind as the user to verify their password
  174. err = l.Bind(userdn, password)
  175. if err != nil {
  176. log.Fatal(err)
  177. }
  178. // Rebind as the read only user for any further queries
  179. err = l.Bind(bindusername, bindpassword)
  180. if err != nil {
  181. log.Fatal(err)
  182. }
  183. }
  184. func Example_beherappolicy() {
  185. l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", "ldap.example.com", 389))
  186. if err != nil {
  187. log.Fatal(err)
  188. }
  189. defer l.Close()
  190. controls := []ldap.Control{}
  191. controls = append(controls, ldap.NewControlBeheraPasswordPolicy())
  192. bindRequest := ldap.NewSimpleBindRequest("cn=admin,dc=example,dc=com", "password", controls)
  193. r, err := l.SimpleBind(bindRequest)
  194. ppolicyControl := ldap.FindControl(r.Controls, ldap.ControlTypeBeheraPasswordPolicy)
  195. var ppolicy *ldap.ControlBeheraPasswordPolicy
  196. if ppolicyControl != nil {
  197. ppolicy = ppolicyControl.(*ldap.ControlBeheraPasswordPolicy)
  198. } else {
  199. log.Printf("ppolicyControl response not available.\n")
  200. }
  201. if err != nil {
  202. errStr := "ERROR: Cannot bind: " + err.Error()
  203. if ppolicy != nil && ppolicy.Error >= 0 {
  204. errStr += ":" + ppolicy.ErrorString
  205. }
  206. log.Print(errStr)
  207. } else {
  208. logStr := "Login Ok"
  209. if ppolicy != nil {
  210. if ppolicy.Expire >= 0 {
  211. logStr += fmt.Sprintf(". Password expires in %d seconds\n", ppolicy.Expire)
  212. } else if ppolicy.Grace >= 0 {
  213. logStr += fmt.Sprintf(". Password expired, %d grace logins remain\n", ppolicy.Grace)
  214. }
  215. }
  216. log.Print(logStr)
  217. }
  218. }
  219. func Example_vchuppolicy() {
  220. l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", "ldap.example.com", 389))
  221. if err != nil {
  222. log.Fatal(err)
  223. }
  224. defer l.Close()
  225. l.Debug = true
  226. bindRequest := ldap.NewSimpleBindRequest("cn=admin,dc=example,dc=com", "password", nil)
  227. r, err := l.SimpleBind(bindRequest)
  228. passwordMustChangeControl := ldap.FindControl(r.Controls, ldap.ControlTypeVChuPasswordMustChange)
  229. var passwordMustChange *ldap.ControlVChuPasswordMustChange
  230. if passwordMustChangeControl != nil {
  231. passwordMustChange = passwordMustChangeControl.(*ldap.ControlVChuPasswordMustChange)
  232. }
  233. if passwordMustChange != nil && passwordMustChange.MustChange {
  234. log.Printf("Password Must be changed.\n")
  235. }
  236. passwordWarningControl := ldap.FindControl(r.Controls, ldap.ControlTypeVChuPasswordWarning)
  237. var passwordWarning *ldap.ControlVChuPasswordWarning
  238. if passwordWarningControl != nil {
  239. passwordWarning = passwordWarningControl.(*ldap.ControlVChuPasswordWarning)
  240. } else {
  241. log.Printf("ppolicyControl response not available.\n")
  242. }
  243. if err != nil {
  244. log.Print("ERROR: Cannot bind: " + err.Error())
  245. } else {
  246. logStr := "Login Ok"
  247. if passwordWarning != nil {
  248. if passwordWarning.Expire >= 0 {
  249. logStr += fmt.Sprintf(". Password expires in %d seconds\n", passwordWarning.Expire)
  250. }
  251. }
  252. log.Print(logStr)
  253. }
  254. }