Skip to content

starting TLS on LDAP connection #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -52,7 +52,7 @@ const users = await ldap.search(filter, attributes);

### `ldap.search(filter, attributes)`

Parameters
#### Parameters

- `filter`: filters results.
- `attributes`: a list of attributes to return
@@ -64,3 +64,14 @@ Returns
### `ldap.destroy()`

Destroys the connection to the LDAP server. Use when all done with LDAP client.

### `ldap.starttls(opts)`

#### Parameters

- `opts`: Object of TLS Options documented on [LDAPjs's Client API page](http://ldapjs.org/client.html#starttls)
```
const opts = {
ca: [fs.readFileSync('mycacert.pem')]
};
```
24 changes: 24 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -108,4 +108,28 @@ export default class SimpleLDAPSearch {
});
});
}

/**
* Secures the LDAP object by using the STARTTLS verb
* @param {Object} opts - STARTTLS options
* @returns {Promise|Error} - Resolves the promise or returns the error from ldapjs
*/
async starttls(opts) {
const self = this;

return new Promise((resolve, reject) => {
// add listener to ldapjs client for error
addListenerIfNotAdded(this.client, 'error', reject);

// starts TLS on the LDAP
self.client.starttls(opts, undefined, (err, res) => {
// if we receive an error back
if (err) {
return reject(new Error(`STARTTLS failed: ${err.message}`));
}
// since ldapjs doesn't return timing, we just resolve if we have no error
resolve(res);
});
});
}
}
2 changes: 1 addition & 1 deletion lib/arrayIncludesFunction.js
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
*/
export default function arrayIncludesFunction(arr, fn) {
if (typeof fn !== 'function') {
throw TypeError(`Function '${fn} is not a function`);
throw new TypeError(`Function '${fn}' is not a function`);
}
return !!arr.find((el) => el.toString() === fn.toString());
}
4 changes: 4 additions & 0 deletions lib/arrayIncludesFunction.test.js
Original file line number Diff line number Diff line change
@@ -35,4 +35,8 @@ describe('arrayIncludesFunction', () => {

expect(arrayIncludesFunction(arr, print2)).toBe(false);
});

it('throws TypeError if fn is not a function', () => {
expect(() => arrayIncludesFunction([1, 2, 3], 5)).toThrow(TypeError);
});
});