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