Skip to content

Commit

Permalink
remove unbind and reuse client
Browse files Browse the repository at this point in the history
  • Loading branch information
shaozi committed Jun 19, 2024
1 parent f6820fc commit 45fa67b
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 60 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: [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 }}'

Expand Down
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
"name": "Debug Example",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}/example/index.js"
},
{
"type": "node",
"request": "launch",
"name": "Debug Test",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}/test/jasmine.js"
}
]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
67 changes: 13 additions & 54 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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!')
Expand All @@ -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!')
Expand Down Expand Up @@ -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,
})
Expand All @@ -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) {
Expand All @@ -110,15 +107,13 @@ async function _searchUser(
})
res.on('error', function (err) {
reject(err)
ldapClient.unbind()
})
res.on('end', function (result) {
if (result.status != 0) {
reject(new Error('ldap search status is not 0, search failed'))
} else {
resolve(user)
}
ldapClient.unbind()
})
})
})
Expand All @@ -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) {
Expand All @@ -153,15 +147,13 @@ 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) {
reject(new Error('ldap search status is not 0, search failed'))
} else {
resolve(groups)
}
ldapClient.unbind()
})
}
)
Expand All @@ -183,7 +175,7 @@ async function authenticateWithAdmin(
groupMemberUserAttribute = 'dn',
attributes = null
) {
var ldapAdminClient
let ldapAdminClient
try {
ldapAdminClient = await _ldapBind(
adminDn,
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -239,7 +219,6 @@ async function authenticateWithAdmin(
groupMemberUserAttribute
)
user.groups = groups
ldapAdminClient.unbind()
}
return user
}
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -301,7 +273,6 @@ async function authenticateWithUser(
groupMemberUserAttribute
)
user.groups = groups
ldapUserClient.unbind()
}
return user
}
Expand All @@ -320,7 +291,7 @@ async function verifyUserExists(
groupMemberUserAttribute = 'dn',
attributes = null
) {
var ldapAdminClient
let ldapAdminClient
try {
ldapAdminClient = await _ldapBind(
adminDn,
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -368,7 +328,6 @@ async function verifyUserExists(
groupMemberUserAttribute
)
user.groups = groups
ldapAdminClient.unbind()
}
return user
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion test/jasmine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
File renamed without changes.

0 comments on commit 45fa67b

Please sign in to comment.