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