ldap-authentication 3.2.1 → 3.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.devcontainer/docker-compose.yml +1 -0
- package/docker-compose.yml +1 -0
- package/index.js +48 -2
- package/package.json +1 -1
- package/test/string.spec.js +43 -0
- package/test/test.spec.js +6 -0
package/docker-compose.yml
CHANGED
package/index.js
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
1
|
const assert = require('assert')
|
|
2
2
|
const ldap = require('ldapjs')
|
|
3
3
|
|
|
4
|
+
// convert an escaped utf8 string returned from ldapjs
|
|
5
|
+
function _isHex(c) {
|
|
6
|
+
return (
|
|
7
|
+
(c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')
|
|
8
|
+
)
|
|
9
|
+
}
|
|
10
|
+
function _parseEscapedHexToUtf8(s) {
|
|
11
|
+
// convert 'cn=\\e7\\a0\\94\\e5\\8f\\91A\\e9\\83\\a8,ou=users,dc=example,dc=com'
|
|
12
|
+
// to 'cn=研发A部,ou=users,dc=example,dc=com'
|
|
13
|
+
let ret = Buffer.alloc(0)
|
|
14
|
+
let len = s.length
|
|
15
|
+
for (let i = 0; i < len; i++) {
|
|
16
|
+
let c = s[i]
|
|
17
|
+
let item
|
|
18
|
+
if (c == '\\' && i < len - 2 && _isHex(s[i + 1]) && _isHex(s[i + 2])) {
|
|
19
|
+
item = Buffer.from(s.substring(i + 1, i + 3), 'hex')
|
|
20
|
+
i += 2
|
|
21
|
+
} else {
|
|
22
|
+
item = Buffer.from(c)
|
|
23
|
+
}
|
|
24
|
+
ret = Buffer.concat([ret, item])
|
|
25
|
+
}
|
|
26
|
+
return ret.toString()
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function _recursiveParseHexString(obj) {
|
|
30
|
+
if (Array.isArray(obj)) {
|
|
31
|
+
return obj.map((ele) => _recursiveParseHexString(ele))
|
|
32
|
+
}
|
|
33
|
+
if (typeof obj == 'string') {
|
|
34
|
+
return _parseEscapedHexToUtf8(obj)
|
|
35
|
+
}
|
|
36
|
+
if (typeof obj == 'object') {
|
|
37
|
+
for (let key in obj) {
|
|
38
|
+
obj[key] = _recursiveParseHexString(obj[key])
|
|
39
|
+
}
|
|
40
|
+
return obj
|
|
41
|
+
}
|
|
42
|
+
return obj
|
|
43
|
+
}
|
|
4
44
|
// convert a SearchResultEntry object in ldapjs 3.0
|
|
5
45
|
// to a user object to maintain backward compatibility
|
|
6
46
|
|
|
@@ -11,7 +51,7 @@ function _searchResultToUser(pojo) {
|
|
|
11
51
|
user[attribute.type] =
|
|
12
52
|
attribute.values.length == 1 ? attribute.values[0] : attribute.values
|
|
13
53
|
})
|
|
14
|
-
return user
|
|
54
|
+
return _recursiveParseHexString(user)
|
|
15
55
|
}
|
|
16
56
|
// bind and return the ldap client
|
|
17
57
|
function _ldapBind(dn, password, starttls, ldapOpts) {
|
|
@@ -142,7 +182,7 @@ async function _searchUserGroups(
|
|
|
142
182
|
return
|
|
143
183
|
}
|
|
144
184
|
res.on('searchEntry', function (entry) {
|
|
145
|
-
groups.push(entry.pojo)
|
|
185
|
+
groups.push(_recursiveParseHexString(entry.pojo))
|
|
146
186
|
})
|
|
147
187
|
res.on('searchReference', function (referral) {})
|
|
148
188
|
res.on('error', function (err) {
|
|
@@ -422,3 +462,9 @@ class LdapAuthenticationError extends Error {
|
|
|
422
462
|
|
|
423
463
|
module.exports.authenticate = authenticate
|
|
424
464
|
module.exports.LdapAuthenticationError = LdapAuthenticationError
|
|
465
|
+
|
|
466
|
+
module.exports.exportForTesting = {
|
|
467
|
+
_isHex,
|
|
468
|
+
_parseEscapedHexToUtf8,
|
|
469
|
+
_recursiveParseHexString,
|
|
470
|
+
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const { exportForTesting } = require('../index.js')
|
|
2
|
+
const { _isHex, _parseEscapedHexToUtf8, _recursiveParseHexString } =
|
|
3
|
+
exportForTesting
|
|
4
|
+
|
|
5
|
+
describe('string conversion test', () => {
|
|
6
|
+
it('unescape string test', () => {
|
|
7
|
+
let s =
|
|
8
|
+
'cn=\\e7\\a0\\94\\e5\\8f\\91A\\e9\\83\\a8,ou=users,dc=example,dc=com'
|
|
9
|
+
let us = _parseEscapedHexToUtf8(s)
|
|
10
|
+
expect(us).toEqual('cn=研发A部,ou=users,dc=example,dc=com')
|
|
11
|
+
})
|
|
12
|
+
it('unescape string test2', () => {
|
|
13
|
+
let s =
|
|
14
|
+
'cn=\\e7\\a0\\94\\e5\\8f\\91A\\e9\\83\\a8\\c2\\a9,ou=users,dc=example,dc=com'
|
|
15
|
+
let us = _parseEscapedHexToUtf8(s)
|
|
16
|
+
expect(us).toEqual('cn=研发A部©,ou=users,dc=example,dc=com')
|
|
17
|
+
})
|
|
18
|
+
it('unescape string test3', () => {
|
|
19
|
+
let s = 'cn=ABC,ou=users,dc=example,dc=com'
|
|
20
|
+
let us = _parseEscapedHexToUtf8(s)
|
|
21
|
+
expect(us).toEqual('cn=ABC,ou=users,dc=example,dc=com')
|
|
22
|
+
})
|
|
23
|
+
it('convert obj', () => {
|
|
24
|
+
let target = {
|
|
25
|
+
a: ['研发A部©', 'abc'],
|
|
26
|
+
b: 'xyz',
|
|
27
|
+
c: true,
|
|
28
|
+
d: null,
|
|
29
|
+
e: '研发A部©',
|
|
30
|
+
f: 1000,
|
|
31
|
+
}
|
|
32
|
+
let obj = {
|
|
33
|
+
a: ['\\e7\\a0\\94\\e5\\8f\\91A\\e9\\83\\a8\\c2\\a9', 'abc'],
|
|
34
|
+
b: 'xyz',
|
|
35
|
+
c: true,
|
|
36
|
+
d: null,
|
|
37
|
+
e: '\\e7\\a0\\94\\e5\\8f\\91A\\e9\\83\\a8\\c2\\a9',
|
|
38
|
+
f: 1000,
|
|
39
|
+
}
|
|
40
|
+
let converted = _recursiveParseHexString(obj)
|
|
41
|
+
expect(converted).toEqual(target)
|
|
42
|
+
})
|
|
43
|
+
})
|
package/test/test.spec.js
CHANGED
|
@@ -124,6 +124,9 @@ describe('ldap-authentication test', () => {
|
|
|
124
124
|
let user = await authenticate(options)
|
|
125
125
|
expect(user).toBeTruthy()
|
|
126
126
|
expect(user.groups.length).toBeGreaterThan(0)
|
|
127
|
+
expect(user.groups[0].objectName).toEqual(
|
|
128
|
+
'cn=科学A部,ou=users,dc=example,dc=com'
|
|
129
|
+
)
|
|
127
130
|
})
|
|
128
131
|
it('Use regular user to authenticate and fetch user group information', async () => {
|
|
129
132
|
let options = {
|
|
@@ -144,6 +147,9 @@ describe('ldap-authentication test', () => {
|
|
|
144
147
|
let user = await authenticate(options)
|
|
145
148
|
expect(user).toBeTruthy()
|
|
146
149
|
expect(user.groups.length).toBeGreaterThan(0)
|
|
150
|
+
expect(user.groups[0].objectName).toEqual(
|
|
151
|
+
'cn=科学A部,ou=users,dc=example,dc=com'
|
|
152
|
+
)
|
|
147
153
|
})
|
|
148
154
|
it('Not specifying groupMemberAttribute or groupMemberUserAttribute should not cause an error and fallback to default values', async () => {
|
|
149
155
|
let options = {
|