From 4b2b98041a7acf47a13da4bfa7f58e2bdcd41ad1 Mon Sep 17 00:00:00 2001 From: shaozi Date: Wed, 19 Jun 2024 17:55:02 -0500 Subject: [PATCH] remove unbind and reuse client (#65) --- .github/workflows/integration-test.yml | 2 +- .vscode/launch.json | 7 +++ README.md | 2 +- index.js | 67 +++++--------------------- package-lock.json | 4 +- package.json | 2 +- test/jasmine.js | 2 +- test/{test.js => test.spec.js} | 0 8 files changed, 26 insertions(+), 60 deletions(-) rename test/{test.js => test.spec.js} (100%) diff --git a/.github/workflows/integration-test.yml b/.github/workflows/integration-test.yml index db47ee1..47c68c7 100644 --- a/.github/workflows/integration-test.yml +++ b/.github/workflows/integration-test.yml @@ -16,7 +16,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - node-version: [16.x, 17.x, 18.x, 20.x] + node-version: [16.x, 17.x, 18.x, 20.x, 22.x] name: 'Integration Node v${{ matrix.node-version }}' diff --git a/.vscode/launch.json b/.vscode/launch.json index 9ec07bf..d003f05 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,6 +10,13 @@ "name": "Debug Example", "skipFiles": ["/**"], "program": "${workspaceFolder}/example/index.js" + }, + { + "type": "node", + "request": "launch", + "name": "Debug Test", + "skipFiles": ["/**"], + "program": "${workspaceFolder}/test/jasmine.js" } ] } diff --git a/README.md b/README.md index 15ad3f6..0542f82 100644 --- a/README.md +++ b/README.md @@ -221,4 +221,4 @@ export async function verifyLogin(email: string, password: string) { Version 2 supports Node version 12, 14, 15, 16, 17 and 18. -Version 3 supports Node version 15, 16, 17 and 18 +Version 3 supports Node version 16, 17, 18, 20 and 22, diff --git a/index.js b/index.js index 5b1af39..20279ae 100644 --- a/index.js +++ b/index.js @@ -17,7 +17,7 @@ function _searchResultToUser(pojo) { function _ldapBind(dn, password, starttls, ldapOpts) { return new Promise(function (resolve, reject) { ldapOpts.connectTimeout = ldapOpts.connectTimeout || 5000 - var client = ldap.createClient(ldapOpts) + let client = ldap.createClient(ldapOpts) client.on('connect', function () { if (starttls) { @@ -29,7 +29,6 @@ function _ldapBind(dn, password, starttls, ldapOpts) { client.bind(dn, password, function (err) { if (err) { reject(err) - client.unbind() return } ldapOpts.log && ldapOpts.log.trace('bind success!') @@ -40,7 +39,6 @@ function _ldapBind(dn, password, starttls, ldapOpts) { client.bind(dn, password, function (err) { if (err) { reject(err) - client.unbind() return } ldapOpts.log && ldapOpts.log.trace('bind success!') @@ -78,7 +76,7 @@ async function _searchUser( attributes = null ) { return new Promise(function (resolve, reject) { - var filter = new ldap.filters.EqualityFilter({ + let filter = new ldap.filters.EqualityFilter({ attribute: usernameAttribute, value: username, }) @@ -91,10 +89,9 @@ async function _searchUser( searchOptions.attributes = attributes } ldapClient.search(searchBase, searchOptions, function (err, res) { - var user = null + let user = null if (err) { reject(err) - ldapClient.unbind() return } res.on('searchEntry', function (entry) { @@ -110,7 +107,6 @@ async function _searchUser( }) res.on('error', function (err) { reject(err) - ldapClient.unbind() }) res.on('end', function (result) { if (result.status != 0) { @@ -118,7 +114,6 @@ async function _searchUser( } else { resolve(user) } - ldapClient.unbind() }) }) }) @@ -141,10 +136,9 @@ async function _searchUserGroups( scope: 'sub', }, function (err, res) { - var groups = [] + let groups = [] if (err) { reject(err) - ldapClient.unbind() return } res.on('searchEntry', function (entry) { @@ -153,7 +147,6 @@ async function _searchUserGroups( res.on('searchReference', function (referral) {}) res.on('error', function (err) { reject(err) - ldapClient.unbind() }) res.on('end', function (result) { if (result.status != 0) { @@ -161,7 +154,6 @@ async function _searchUserGroups( } else { resolve(groups) } - ldapClient.unbind() }) } ) @@ -183,7 +175,7 @@ async function authenticateWithAdmin( groupMemberUserAttribute = 'dn', attributes = null ) { - var ldapAdminClient + let ldapAdminClient try { ldapAdminClient = await _ldapBind( adminDn, @@ -194,14 +186,13 @@ async function authenticateWithAdmin( } catch (error) { throw { admin: error } } - var user = await _searchUser( + let user = await _searchUser( ldapAdminClient, userSearchBase, usernameAttribute, username, attributes ) - ldapAdminClient.unbind() if (!user || !user.dn) { ldapOpts.log && ldapOpts.log.trace( @@ -211,26 +202,15 @@ async function authenticateWithAdmin( 'user not found or usernameAttribute is wrong' ) } - var userDn = user.dn + let userDn = user.dn let ldapUserClient try { ldapUserClient = await _ldapBind(userDn, userPassword, starttls, ldapOpts) } catch (error) { throw error } - ldapUserClient.unbind() if (groupsSearchBase && groupClass && groupMemberAttribute) { - try { - ldapAdminClient = await _ldapBind( - adminDn, - adminPassword, - starttls, - ldapOpts - ) - } catch (error) { - throw error - } - var groups = await _searchUserGroups( + let groups = await _searchUserGroups( ldapAdminClient, groupsSearchBase, user, @@ -239,7 +219,6 @@ async function authenticateWithAdmin( groupMemberUserAttribute ) user.groups = groups - ldapAdminClient.unbind() } return user } @@ -266,10 +245,9 @@ async function authenticateWithUser( } if (!usernameAttribute || !userSearchBase) { // if usernameAttribute is not provided, no user detail is needed. - ldapUserClient.unbind() return true } - var user = await _searchUser( + let user = await _searchUser( ldapUserClient, userSearchBase, usernameAttribute, @@ -285,14 +263,8 @@ async function authenticateWithUser( 'user logged in, but user details could not be found. Probabaly usernameAttribute or userSearchBase is wrong?' ) } - ldapUserClient.unbind() if (groupsSearchBase && groupClass && groupMemberAttribute) { - try { - ldapUserClient = await _ldapBind(userDn, userPassword, starttls, ldapOpts) - } catch (error) { - throw error - } - var groups = await _searchUserGroups( + let groups = await _searchUserGroups( ldapUserClient, groupsSearchBase, user, @@ -301,7 +273,6 @@ async function authenticateWithUser( groupMemberUserAttribute ) user.groups = groups - ldapUserClient.unbind() } return user } @@ -320,7 +291,7 @@ async function verifyUserExists( groupMemberUserAttribute = 'dn', attributes = null ) { - var ldapAdminClient + let ldapAdminClient try { ldapAdminClient = await _ldapBind( adminDn, @@ -331,14 +302,13 @@ async function verifyUserExists( } catch (error) { throw { admin: error } } - var user = await _searchUser( + let user = await _searchUser( ldapAdminClient, userSearchBase, usernameAttribute, username, attributes ) - ldapAdminClient.unbind() if (!user || !user.dn) { ldapOpts.log && ldapOpts.log.trace( @@ -349,17 +319,7 @@ async function verifyUserExists( ) } if (groupsSearchBase && groupClass && groupMemberAttribute) { - try { - ldapAdminClient = await _ldapBind( - adminDn, - adminPassword, - starttls, - ldapOpts - ) - } catch (error) { - throw error - } - var groups = await _searchUserGroups( + let groups = await _searchUserGroups( ldapAdminClient, groupsSearchBase, user, @@ -368,7 +328,6 @@ async function verifyUserExists( groupMemberUserAttribute ) user.groups = groups - ldapAdminClient.unbind() } return user } diff --git a/package-lock.json b/package-lock.json index 8e06a28..35b14f4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ldap-authentication", - "version": "3.0.4", + "version": "3.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "ldap-authentication", - "version": "3.0.4", + "version": "3.1.0", "license": "BSD-2-Clause", "dependencies": { "ldapjs": "^3.0.7" diff --git a/package.json b/package.json index 1d3b164..7312ea8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ldap-authentication", - "version": "3.1.0", + "version": "3.2.1", "description": "A simple async nodejs library for LDAP user authentication", "main": "index.js", "types": "./index.d.ts", diff --git a/test/jasmine.js b/test/jasmine.js index 6181d69..8fa0227 100644 --- a/test/jasmine.js +++ b/test/jasmine.js @@ -3,7 +3,7 @@ var jasmine = new Jasmine() jasmine.loadConfig({ spec_dir: 'test', - spec_files: ['test.js'], + spec_files: ['**/*[sS]pec.?(m)js'], random: false, seed: null, stopSpecOnExpectationFailure: false, diff --git a/test/test.js b/test/test.spec.js similarity index 100% rename from test/test.js rename to test/test.spec.js