@ilb/node_ldap
Advanced tools
Comparing version 1.0.17 to 1.1.0
{ | ||
"name": "@ilb/node_ldap", | ||
"version": "1.0.17", | ||
"version": "1.1.0", | ||
"description": "", | ||
"author": "github@bystrobank.ru", | ||
"author": "github@iconicompany.com", | ||
"license": "ISC", | ||
"type": "module", | ||
"main": "src/LDAPFactory.js", | ||
"scripts": { | ||
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js" | ||
"test": "node --use-openssl-ca --experimental-vm-modules node_modules/jest/bin/jest.js" | ||
}, | ||
@@ -20,2 +19,3 @@ "browser": { | ||
"dependencies": { | ||
"debug": "^4.3.4", | ||
"ldapjs-client": "^0.1.1" | ||
@@ -26,7 +26,7 @@ }, | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-jest": "^25.2.2", | ||
"eslint-plugin-jest": "^27.2.1", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"jest": "^27.3.1", | ||
"jest": "^29.5.0", | ||
"prettier": "^2.4.1" | ||
} | ||
} |
@@ -1,3 +0,3 @@ | ||
import LDAPResource from './LDAPResource.js'; | ||
import LDAPCache from './LDAPCache.js'; | ||
const LDAPResource = require('./LDAPResource.js'); | ||
const LDAPCache = require('./LDAPCache.js'); | ||
@@ -7,3 +7,3 @@ /** | ||
*/ | ||
export default class CacheableLDAPResource { | ||
class CacheableLDAPResource { | ||
static async getInstance(ldapClient, base) { | ||
@@ -29,1 +29,3 @@ const ldapCache = await LDAPCache.getInstance(ldapClient); | ||
} | ||
module.exports = CacheableLDAPResource; |
@@ -1,2 +0,2 @@ | ||
import LDAPLastMod from './LDAPLastMod.js'; | ||
const LDAPLastMod = require('./LDAPLastMod.js'); | ||
/** | ||
@@ -6,3 +6,3 @@ * LastMod based LdapCache | ||
*/ | ||
export default class LDAPCache { | ||
class LDAPCache { | ||
static async getInstance(ldapClient) { | ||
@@ -21,3 +21,3 @@ await LDAPCache.invalidateCache(ldapClient); | ||
// invalidate cache | ||
if (LDAPCache.cacheDate && LDAPCache.cacheDate < lmdt) { | ||
if (LDAPCache.cacheDate && lmdt && LDAPCache.cacheDate < lmdt) { | ||
LDAPCache.cache = {}; | ||
@@ -44,1 +44,3 @@ LDAPCache.cacheDate = lmdt; | ||
} | ||
module.exports = LDAPCache; |
@@ -1,2 +0,2 @@ | ||
import { readFileSync } from 'fs'; | ||
const { readFileSync } = require('fs'); | ||
@@ -10,3 +10,3 @@ /** | ||
if (ldapConfig.caCert) { | ||
this.tlsOptions = [readFileSync(ldapConfig.caCert)]; | ||
this.tlsOptions = { ca: readFileSync(ldapConfig.caCert) }; | ||
} | ||
@@ -16,2 +16,2 @@ } | ||
export default LDAPClientConfig; | ||
module.exports = LDAPClientConfig; |
@@ -1,4 +0,5 @@ | ||
import LdapClient from 'ldapjs-client'; | ||
//import ShutdownHook from 'shutdown-hook'; | ||
const LdapClient = require('ldapjs-client'); | ||
//import Ldap from 'ldap-async'; | ||
/** | ||
@@ -12,9 +13,2 @@ * LDAP client factory fith connection reuse | ||
this.connections = {}; | ||
// const shutdownHook = new ShutdownHook({}); | ||
//shutdownHook.add(_ => this.close(), {}) | ||
//shutdownHook.add(_ => console.log('shutdown!!!'), {}) | ||
// const exitHook = require('exit-hook'); | ||
// exitHook(() => { | ||
// console.log('Exiting!!!'); | ||
// }); | ||
} | ||
@@ -36,2 +30,2 @@ | ||
export default LDAPClientFactory; | ||
module.exports = LDAPClientFactory; |
@@ -1,8 +0,8 @@ | ||
import { existsSync, readFileSync } from 'fs'; | ||
import OpenLDAPConfig from './OpenLDAPConfig.js'; | ||
import URILDAPConfig from './URILDAPConfig.js'; | ||
import LDAPClientConfig from './LDAPClientConfig.js'; | ||
import LDAPClientFactory from './LDAPClientFactory.js'; | ||
import CacheableLDAPResource from './CacheableLDAPResource.js'; | ||
import createDebug from 'debug'; | ||
const { existsSync, readFileSync } = require('fs'); | ||
const OpenLDAPConfig = require('./OpenLDAPConfig.js'); | ||
const URILDAPConfig = require('./URILDAPConfig.js'); | ||
const LDAPClientConfig = require('./LDAPClientConfig.js'); | ||
const LDAPClientFactory = require('./LDAPClientFactory.js'); | ||
const CacheableLDAPResource = require('./CacheableLDAPResource.js'); | ||
const createDebug = require('debug'); | ||
@@ -13,60 +13,52 @@ const debug = createDebug('node_ldap'); | ||
*/ | ||
export default class LDAPFactory { | ||
constructor(ldapConfPath = '/etc/openldap/ldap.conf') { | ||
this.ldapConfig = null; | ||
if (process.env.LDAP_URL) { | ||
//configure using LDAP_URL variable if set | ||
this.ldapConfig = new URILDAPConfig(process.env.LDAP_URL, process.env.NODE_EXTRA_CA_CERTS); | ||
debug( | ||
'configured using LDAP_URL (%s) isConfigured=%o', | ||
process.env.LDAP_URL, | ||
this.ldapConfig.isConfigured() | ||
); | ||
} else if (existsSync(ldapConfPath)) { | ||
//configure using openldap configuration file | ||
this.ldapConfig = new OpenLDAPConfig(readFileSync(ldapConfPath, 'utf8')); | ||
debug( | ||
'configured using ldap config file (%s) isConfigured=%o', | ||
ldapConfPath, | ||
this.ldapConfig.isConfigured() | ||
); | ||
} | ||
this.ldapClientFactory = new LDAPClientFactory(); | ||
this.ldapClient = null; | ||
function LDAPFactory(ldapConfPath = '/etc/openldap/ldap.conf') { | ||
this.ldapConfig = null; | ||
if (process.env.LDAP_URL) { | ||
//configure using LDAP_URL variable if set | ||
this.ldapConfig = new URILDAPConfig(process.env.LDAP_URL, process.env.NODE_EXTRA_CA_CERTS); | ||
debug('configured using LDAP_URL', this.ldapConfig); | ||
} else if (existsSync(ldapConfPath)) { | ||
//configure using openldap configuration file | ||
this.ldapConfig = new OpenLDAPConfig(readFileSync(ldapConfPath, 'utf8')); | ||
debug('configured using ldap config file', ldapConfPath, this.ldapConfig); | ||
} | ||
this.ldapClientFactory = new LDAPClientFactory(); | ||
this.ldapClient = null; | ||
} | ||
/** | ||
* check if ldapFactory configured | ||
*/ | ||
isConfigured() { | ||
return !!(this.ldapConfig && this.ldapConfig.isConfigured()); | ||
/** | ||
* check if ldapFactory configured | ||
*/ | ||
LDAPFactory.prototype.isConfigured = function () { | ||
return !!(this.ldapConfig && this.ldapConfig.isConfigured()); | ||
}; | ||
/** | ||
* lazy-initalization method to get ldapClient | ||
*/ | ||
LDAPFactory.prototype.getLDAPClient = function () { | ||
if (this.ldapClient === null) { | ||
const ldapClientConfig = new LDAPClientConfig(this.ldapConfig); | ||
this.ldapClient = this.ldapClientFactory.getLDAPClient(ldapClientConfig); | ||
} | ||
/** | ||
* lazy-initalization method to get ldapClient | ||
*/ | ||
getLDAPClient() { | ||
if (this.ldapClient === null) { | ||
const ldapClientConfig = new LDAPClientConfig(this.ldapConfig); | ||
this.ldapClient = this.ldapClientFactory.getLDAPClient(ldapClientConfig); | ||
} | ||
return this.ldapClient; | ||
} | ||
return this.ldapClient; | ||
}; | ||
/** | ||
* get autoconfigured ldap resource | ||
*/ | ||
async getLDAPResource() { | ||
const ldapResource = await CacheableLDAPResource.getInstance( | ||
this.getLDAPClient(), | ||
this.ldapConfig.base | ||
); | ||
return ldapResource; | ||
} | ||
/** | ||
* get autoconfigured ldap resource | ||
*/ | ||
LDAPFactory.prototype.getLDAPResource = async function () { | ||
const ldapResource = await CacheableLDAPResource.getInstance( | ||
this.getLDAPClient(), | ||
this.ldapConfig.base | ||
); | ||
return ldapResource; | ||
}; | ||
/** | ||
* closes open connections | ||
*/ | ||
close() { | ||
this.ldapClientFactory.close(); | ||
} | ||
} | ||
/** | ||
* closes open connections | ||
*/ | ||
LDAPFactory.prototype.close = function () { | ||
this.ldapClientFactory.close(); | ||
}; | ||
module.exports = LDAPFactory; |
@@ -1,13 +0,18 @@ | ||
export default class LDAPLastMod { | ||
constructor(ldapClient) { | ||
this.ldapClient = ldapClient; | ||
} | ||
function LDAPLastMod(ldapClient, base, options) { | ||
this.ldapClient = ldapClient; | ||
this.base = base || 'cn=lastmod,c=ru'; | ||
this.options = options || { | ||
filter: '(objectClass=lastmod)', | ||
attributes: ['modifyTimestamp'] | ||
}; | ||
LDAPLastMod.prototype.getLastMod = async function () { | ||
let entries = []; | ||
try { | ||
entries = await this.ldapClient.search(this.base, this.options); | ||
} catch (ex) { | ||
if (ex.name !== 'NoSuchObjectError') { | ||
throw new Error('LDAP lookup failed ' + ex); | ||
} | ||
} | ||
async getLastMod() { | ||
const options = { | ||
filter: '(objectClass=lastmod)', | ||
attributes: ['modifyTimestamp'] | ||
}; | ||
const entries = await this.ldapClient.search('cn=lastmod,c=ru', options); | ||
let dateLastMod = null; | ||
@@ -28,3 +33,5 @@ if (entries.length === 1 && entries[0].modifyTimestamp) { | ||
return dateLastMod; | ||
} | ||
}; | ||
} | ||
module.exports = LDAPLastMod; |
@@ -1,29 +0,31 @@ | ||
export default class LDAPResource { | ||
constructor(ldapClient, base) { | ||
this.ldapClient = ldapClient; | ||
this.base = base; | ||
this.lookupCount = 0; | ||
} | ||
function LDAPResource(ldapClient, base) { | ||
this.ldapClient = ldapClient; | ||
this.base = base; | ||
this.lookupCount = 0; | ||
} | ||
async lookup(name, base) { | ||
base = base || this.base; | ||
const options = { | ||
filter: `(&(objectClass=applicationProcess)(cn=${name}))`, | ||
scope: 'sub', | ||
attributes: ['labeledURI'] | ||
}; | ||
let entries = null; | ||
try { | ||
entries = await this.ldapClient.search(base, options); | ||
} catch (ex) { | ||
LDAPResource.prototype.lookup = async function (name, base) { | ||
base = base || this.base; | ||
const options = { | ||
filter: `(&(objectClass=applicationProcess)(cn=${name}))`, | ||
scope: 'sub', | ||
attributes: ['labeledURI'] | ||
}; | ||
let entries = []; | ||
try { | ||
entries = await this.ldapClient.search(base, options); | ||
} catch (ex) { | ||
if (ex.name !== 'NoSuchObjectError') { | ||
throw new Error('LDAP lookup failed ' + ex); | ||
} | ||
//console.log('entries',entries); | ||
let result = null; | ||
if (entries.length > 0 && entries[0].labeledURI) { | ||
result = entries[0].labeledURI; | ||
} | ||
this.lookupCount++; | ||
return result; | ||
} | ||
} | ||
//console.log('entries',entries); | ||
let result = null; | ||
if (entries.length > 0 && entries[0].labeledURI) { | ||
result = entries[0].labeledURI; | ||
} | ||
this.lookupCount++; | ||
return result; | ||
}; | ||
module.exports = LDAPResource; |
@@ -1,34 +0,34 @@ | ||
import LDAPConfig from './LDAPConfig.js'; | ||
export default class OpenLDAPConfig extends LDAPConfig { | ||
constructor(config) { | ||
super(); | ||
this.ldapSchemasRegexp = /^ldaps?:\/\//; | ||
this.loadValuesFromConfig(config); | ||
function OpenLDAPConfig(config) { | ||
const ldapSchemasRegexp = /^ldaps?:\/\//; | ||
const configMap = parseConfig(config); | ||
this.uri = []; | ||
if (configMap.URI) { | ||
this.uri = configMap.URI.split(/\s+/).filter((l) => l.match(ldapSchemasRegexp)); | ||
} | ||
this.base = configMap.BASE || null; | ||
this.caCert = configMap.TLS_CACERT || null; | ||
} | ||
/** | ||
* check if this instance if configured | ||
*/ | ||
OpenLDAPConfig.prototype.isConfigured = function () { | ||
return this.uri && this.uri.length > 0; | ||
}; | ||
loadValuesFromConfig(config) { | ||
const configMap = OpenLDAPConfig.parseConfig(config); | ||
if (configMap.URI) { | ||
this.uri = configMap.URI.split(/\s+/).filter((l) => l.match(this.ldapSchemasRegexp)); | ||
} | ||
this.base = configMap.BASE || null; | ||
this.caCert = configMap.TLS_CACERT || null; | ||
} | ||
function parseConfig(config) { | ||
const alllines = config.split(/\r?\n/); | ||
const lines = alllines | ||
.map((l) => l.replace(/#.*$/, '').trim()) // remove comments and trim | ||
.filter((l) => l.length > 0); // skip empty lines | ||
//console.log(lines); | ||
const map = lines | ||
.map((l) => l.split(/\s(.*)/)) // split by first whitespace | ||
.reduce(function (map, obj) { | ||
map[obj[0]] = obj[1]; | ||
return map; | ||
}, {}); | ||
//console.log(map); | ||
return map; | ||
} | ||
static parseConfig(config) { | ||
const alllines = config.split(/\r?\n/); | ||
const lines = alllines | ||
.map((l) => l.replace(/#.*$/, '').trim()) // remove comments and trim | ||
.filter((l) => l.length > 0); // skip empty lines | ||
//console.log(lines); | ||
const map = lines | ||
.map((l) => l.split(/\s(.*)/)) // split by first whitespace | ||
.reduce(function (map, obj) { | ||
map[obj[0]] = obj[1]; | ||
return map; | ||
}, {}); | ||
//console.log(map); | ||
return map; | ||
} | ||
} | ||
module.exports = OpenLDAPConfig; |
@@ -1,4 +0,1 @@ | ||
import { parse, format } from 'url'; | ||
import LDAPConfig from './LDAPConfig.js'; | ||
/** | ||
@@ -8,11 +5,17 @@ * Configure LDAP from URI | ||
*/ | ||
export default class URILDAPConfig extends LDAPConfig { | ||
constructor(uri, caCert) { | ||
super(); | ||
const urlobj = parse(uri); | ||
this.base = urlobj.pathname.substring(1); | ||
urlobj.pathname = null; | ||
this.uri = [format(urlobj)]; | ||
this.caCert = caCert; | ||
} | ||
function URILDAPConfig(uri, caCert) { | ||
const urlobj = new URL(uri); | ||
this.base = urlobj.pathname.substring(1); | ||
urlobj.pathname = ''; | ||
this.uri = [urlobj.toString()]; | ||
this.caCert = caCert; | ||
} | ||
/** | ||
* check if this instance if configured | ||
*/ | ||
URILDAPConfig.prototype.isConfigured = function () { | ||
return this.uri && this.uri.length > 0; | ||
}; | ||
module.exports = URILDAPConfig; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6
9091
2
11
271
No
+ Addeddebug@^4.3.4
+ Addeddebug@4.3.7(transitive)
+ Addedms@2.1.3(transitive)