issue-946.test.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. 'use strict'
  2. const tap = require('tap')
  3. const ldapjs = require('../../lib')
  4. const SCHEME = process.env.SCHEME || 'ldap'
  5. const HOST = process.env.HOST || '127.0.0.1'
  6. const PORT = process.env.PORT || 389
  7. const baseURL = `${SCHEME}://${HOST}:${PORT}`
  8. tap.test('can use password policy response', t => {
  9. const client = ldapjs.createClient({ url: baseURL })
  10. const targetDN = 'cn=Bender Bending Rodríguez,ou=people,dc=planetexpress,dc=com'
  11. client.bind('cn=admin,dc=planetexpress,dc=com', 'GoodNewsEveryone', (err, res) => {
  12. t.error(err)
  13. t.ok(res)
  14. t.equal(res.status, 0)
  15. const newPassword = 'bender2'
  16. changePassword(client, newPassword, () => {
  17. client.unbind()
  18. bindNewClient(newPassword, { error: 2 }, (client) => {
  19. const newPassword = 'bender'
  20. changePassword(client, newPassword, () => {
  21. client.unbind()
  22. bindNewClient(newPassword, { timeBeforeExpiration: 1000 }, (client) => {
  23. client.unbind(t.end)
  24. })
  25. })
  26. })
  27. })
  28. })
  29. function bindNewClient (pwd, expected, callback) {
  30. const client = ldapjs.createClient({ url: baseURL })
  31. const control = new ldapjs.PasswordPolicyControl()
  32. client.bind(targetDN, pwd, control, (err, res) => {
  33. t.error(err)
  34. t.ok(res)
  35. t.equal(res.status, 0)
  36. let error = null
  37. let timeBeforeExpiration = null
  38. let graceAuthNsRemaining = null
  39. res.controls.forEach(control => {
  40. if (control.type === ldapjs.PasswordPolicyControl.OID) {
  41. error = control.value.error ?? error
  42. timeBeforeExpiration = control.value.timeBeforeExpiration ?? timeBeforeExpiration
  43. graceAuthNsRemaining = control.value.graceAuthNsRemaining ?? graceAuthNsRemaining
  44. }
  45. })
  46. if (expected.error !== undefined) {
  47. t.equal(error, expected.error)
  48. }
  49. if (expected.timeBeforeExpiration !== undefined) {
  50. t.equal(timeBeforeExpiration, expected.timeBeforeExpiration)
  51. }
  52. if (expected.graceAuthNsRemaining !== undefined) {
  53. t.equal(graceAuthNsRemaining, expected.graceAuthNsRemaining)
  54. }
  55. callback(client)
  56. })
  57. }
  58. function changePassword (client, newPwd, callback) {
  59. const change = new ldapjs.Change({
  60. operation: 'replace',
  61. modification: new ldapjs.Attribute({
  62. type: 'userPassword',
  63. values: newPwd
  64. })
  65. })
  66. client.modify(targetDN, change, (err, res) => {
  67. t.error(err)
  68. t.ok(res)
  69. t.equal(res.status, 0)
  70. callback()
  71. })
  72. }
  73. })