-
Notifications
You must be signed in to change notification settings - Fork 7
/
ldapjs-filter-overrides.js
81 lines (72 loc) · 2.94 KB
/
ldapjs-filter-overrides.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import ldapjs from "ldapjs";
import getAttributeValue from "@ldapjs/filter/lib/utils/get-attribute-value.js";
import testValues from "@ldapjs/filter/lib/utils/test-values.js";
export const patchLdapjsFilters = () => {
/**
* Case-insensitive matching in SubstringFilter, overrides "matches" of corresponding prototype.
* Mainly resembles source code from @ldapjs/filter 2.1.0.
*/
ldapjs.filters.SubstringFilter.prototype.matches = function (obj, strictAttrCase) {
if (Array.isArray(obj) === true) {
for (const attr of obj) {
if (Object.prototype.toString.call(attr) !== '[object LdapAttribute]') {
throw Error('array element must be an instance of LdapAttribute')
}
if (this.matches(attr, strictAttrCase) === true) {
return true
}
}
return false
}
const targetValue = getAttributeValue({
sourceObject: obj,
attributeName: this.attribute,
strictCase: strictAttrCase
})
if (targetValue === undefined || targetValue === null) {
return false
}
const escapeRegExp = str => str.replace(/[\-\[\]\/{}()*+?.\\^$|]/g, '\\$&'); // eslint-disable-line
let re = ''
if (this.initial) { re += '^' + escapeRegExp(this.initial) + '.*' }
this.any.forEach(function (s) {
re += escapeRegExp(s) + '.*'
})
if (this.final) { re += escapeRegExp(this.final) + '$' }
// Functional change here: Add case-insensitive-flag
const matcher = new RegExp(re, 'i')
return testValues({
rule: v => matcher.test(v),
value: targetValue
})
};
/**
* Case-insensitive matching in EqualityFilter, overrides "matches" of corresponding prototype.
* Mainly resembles source code from @ldapjs/filter 2.1.0.
*/
ldapjs.filters.EqualityFilter.prototype.matches = function (obj, strictAttrCase = false) {
if (Array.isArray(obj) === true) {
for (const attr of obj) {
if (Object.prototype.toString.call(attr) !== '[object LdapAttribute]') {
throw Error('array element must be an instance of LdapAttribute')
}
if (this.matches(attr, strictAttrCase) === true) {
return true
}
}
return false
}
let testValue = this.value
// Always perform case-insensitive value comparison
const targetAttribute = getAttributeValue({
sourceObject: obj,
attributeName: this.attribute,
strictCase: this.attribute.toLowerCase() !== 'objectclass' && strictAttrCase
})
testValue = testValue.toLowerCase()
return testValues({
rule: v => testValue === v?.toLowerCase(),
value: targetAttribute
})
};
};