Skip to content

Commit

Permalink
Upgrade ldapjs 3 (#51)
Browse files Browse the repository at this point in the history
* adapt ldapjs 3.0

* Updated to ldapjsv3

* remove nodejs support of 12 and 14

* Update Readme on supported node versions
  • Loading branch information
shaozi authored Mar 28, 2023
1 parent 84a8b28 commit f5dd6a2
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x, 14.x, 15.x, 16.x, 17.x, 18.x]
node-version: [15.x, 16.x, 17.x, 18.x]

name: 'Integration Node v${{ matrix.node-version }}'

Expand Down
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Example",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}/example/index.js"
}
]
}
34 changes: 29 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,43 @@ auth()

The user object if `authenticate()` is success.

The user object has a `raw` field that has the raw data from the LDAP/AD server. It can be used to access buffer objects (profile pics for example).
Buffer data can now be accessed by `user.raw.profilePhoto`, etc, instead of `user.profilePhoto`.



In version 2, The user object has a `raw` field that has the raw data from the LDAP/AD server. It can be used to access buffer objects (profile pics for example).

Buffer data can now be accessed by `user.raw.profilePhoto`, etc, instead of `user.profilePhoto`.

In version 3, the `raw` field is no longer used. Instead, append `;binary` to the attributes you
want to get back as buffer. Check the following example on how to get a user's profile photo:

```javascript
export async function verifyLogin(email: string, password: string) {

const options = {
//...other config options
userPassword: password,
username: email,
attributes: ['thumbnailPhoto;binary', 'givenName', 'sn', 'sAMAccountName', 'userPrincipalName', 'memberOf' ]
};

try {
const ldapUser = await authenticate(options);

if (!ldapUser) {
return { error: "user not found" };
}

// accessing the image
const profilePhoto = ldapUser['thumbnailPhoto;binary'];

/* using the image
<img src={`data:image/*;base64,${user.profilePhoto}`} />
*/
return { user: ldapUser };
}
}
```

## Supported Node Versions

Version 2 supports Node version 12, 14, 15, 16, 17 and 18.

Version 3 supports Node version 15, 16, 17 and 18
15 changes: 13 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
const assert = require('assert')
const ldap = require('ldapjs')

// convert a SearchResultEntry object in ldapjs 3.0
// to a user object to maintain backward compatibility

function _searchResultToUser(pojo) {
assert(pojo.type == 'SearchResultEntry')
let user = { dn: pojo.objectName }
pojo.attributes.forEach((attribute) => {
user[attribute.type] =
attribute.values.length == 1 ? attribute.values[0] : attribute.values
})
return user
}
// bind and return the ldap client
function _ldapBind(dn, password, starttls, ldapOpts) {
return new Promise(function (resolve, reject) {
Expand Down Expand Up @@ -86,8 +98,7 @@ async function _searchUser(
return
}
res.on('searchEntry', function (entry) {
user = entry.object
user.raw = entry.raw
user = _searchResultToUser(entry.pojo)
})
res.on('searchReference', function (referral) {
// TODO: we don't support reference yet
Expand Down
151 changes: 106 additions & 45 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ldap-authentication",
"version": "2.3.3",
"version": "3.0.1",
"description": "A simple async nodejs library for LDAP user authentication",
"main": "index.js",
"types": "./index.d.ts",
Expand Down Expand Up @@ -36,9 +36,9 @@
},
"homepage": "https://github.com/shaozi/ldap-authentication#readme",
"dependencies": {
"ldapjs": "^2.3.3"
"ldapjs": "^3.0.1"
},
"devDependencies": {
"jasmine": "^4.5.0"
"jasmine": "^4.6.0"
}
}
1 change: 0 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ describe('ldap-authentication test', () => {
let user = await authenticate(options)
expect(user).toBeTruthy()
expect(user.uid).toEqual('einstein')
expect(user.raw).toBeTruthy()
})
it('Use an regular user to authenticate iteself and return attributes', async () => {
let options = {
Expand Down

0 comments on commit f5dd6a2

Please sign in to comment.