const Unit = require('libflitter/Unit') const LDAP = require('ldapjs') class LDAPServerUnit extends Unit { static get name() { return 'ldap_server' } static get services() { return [...super.services, 'configs', 'express', 'output'] } auth_dn() { return this.build_dn(this.config.schema.authentication_base) } anonymous() { return LDAP.parseDN('cn=anonymous') } build_dn(...parts) { parts = parts.flat() parts.push(this.config.schema.base_dc) return LDAP.parseDN(parts.join(',')) } async go(app) { this.config = this.configs.get('ldap:server') const server_config = {} // If Flitter is configured to use an SSL certificate, // use it to enable LDAPS in the server. if ( this.express.use_ssl() ) { this.output.info('Using configured SSL certificate. The LDAP server will require an ldaps:// connection.') server_config.certificate = await this.express.ssl_certificate() server_config.key = await this.express.ssl_key() } this.server = LDAP.createServer(server_config) if ( this.config.max_connections ) { this.server.maxConnections = this.config.max_connections } this.output.info(`Will listen on ${this.config.interface}:${this.config.port}`) await new Promise((res, rej) => { this.server.listen(this.config.port, this.config.interface, () => { this.output.success(`LDAP server listening on port ${this.config.port}...`) res() }) }) } async cleanup(app) { this.server.close() } } module.exports = exports = LDAPServerUnit