Skip to content
Open
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -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);
});
});