|
1 | 1 | const ldap = require('../lib/Adapters/Auth/ldap');
|
2 | 2 | const mockLdapServer = require('./MockLdapServer');
|
| 3 | +const fs = require('fs'); |
3 | 4 | const port = 12345;
|
| 5 | +const sslport = 12346; |
4 | 6 |
|
5 | 7 | it('Should fail with missing options', done => {
|
6 | 8 | ldap
|
@@ -31,6 +33,86 @@ it('Should succeed with right credentials', done => {
|
31 | 33 | });
|
32 | 34 | });
|
33 | 35 |
|
| 36 | +it('Should succeed with right credentials when LDAPS is used and certifcate is not checked', done => { |
| 37 | + mockLdapServer(sslport, 'uid=testuser, o=example', false, true).then(server => { |
| 38 | + const options = { |
| 39 | + suffix: 'o=example', |
| 40 | + url: `ldaps://localhost:${sslport}`, |
| 41 | + dn: 'uid={{id}}, o=example', |
| 42 | + tlsOptions: { rejectUnauthorized: false } |
| 43 | + }; |
| 44 | + ldap |
| 45 | + .validateAuthData({ id: 'testuser', password: 'secret' }, options) |
| 46 | + .then(done) |
| 47 | + .catch(done.fail) |
| 48 | + .finally(() => server.close()); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +it('Should succeed when LDAPS is used and the presented certificate is the expected certificate', done => { |
| 53 | + mockLdapServer(sslport, 'uid=testuser, o=example', false, true).then(server => { |
| 54 | + const options = { |
| 55 | + suffix: 'o=example', |
| 56 | + url: `ldaps://localhost:${sslport}`, |
| 57 | + dn: 'uid={{id}}, o=example', |
| 58 | + tlsOptions: { |
| 59 | + ca: fs.readFileSync(__dirname + '/support/cert/cert.pem'), |
| 60 | + rejectUnauthorized: true |
| 61 | + } |
| 62 | + }; |
| 63 | + ldap |
| 64 | + .validateAuthData({ id: 'testuser', password: 'secret' }, options) |
| 65 | + .then(done) |
| 66 | + .catch(done.fail) |
| 67 | + .finally(() => server.close()); |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +it('Should fail when LDAPS is used and the presented certificate is not the expected certificate', done => { |
| 72 | + mockLdapServer(sslport, 'uid=testuser, o=example', false, true).then(server => { |
| 73 | + const options = { |
| 74 | + suffix: 'o=example', |
| 75 | + url: `ldaps://localhost:${sslport}`, |
| 76 | + dn: 'uid={{id}}, o=example', |
| 77 | + tlsOptions: { |
| 78 | + ca: fs.readFileSync(__dirname + '/support/cert/anothercert.pem'), |
| 79 | + rejectUnauthorized: true |
| 80 | + } |
| 81 | + }; |
| 82 | + ldap |
| 83 | + .validateAuthData({ id: 'testuser', password: 'secret' }, options) |
| 84 | + .then(done.fail) |
| 85 | + .catch(err => { |
| 86 | + jequal(err.message, 'LDAPS: Certificate mismatch'); |
| 87 | + done(); |
| 88 | + }) |
| 89 | + .finally(() => server.close()); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +it('Should fail when LDAPS is used certifcate matches but credentials are wrong', done => { |
| 94 | + mockLdapServer(sslport, 'uid=testuser, o=example', false, true).then(server => { |
| 95 | + const options = { |
| 96 | + suffix: 'o=example', |
| 97 | + url: `ldaps://localhost:${sslport}`, |
| 98 | + dn: 'uid={{id}}, o=example', |
| 99 | + tlsOptions: { |
| 100 | + ca: fs.readFileSync(__dirname + '/support/cert/cert.pem'), |
| 101 | + rejectUnauthorized: true |
| 102 | + } |
| 103 | + }; |
| 104 | + ldap |
| 105 | + .validateAuthData({ id: 'testuser', password: 'wrong!' }, options) |
| 106 | + .then(done.fail) |
| 107 | + .catch(err => { |
| 108 | + jequal(err.message, 'LDAP: Wrong username or password'); |
| 109 | + done(); |
| 110 | + }) |
| 111 | + .finally(() => server.close()); |
| 112 | + }); |
| 113 | +}); |
| 114 | + |
| 115 | + |
34 | 116 | it('Should fail with wrong credentials', done => {
|
35 | 117 | mockLdapServer(port, 'uid=testuser, o=example').then(server => {
|
36 | 118 | const options = {
|
|
0 commit comments