UNPKG

@3kles/3kles-ldap

Version:
190 lines (188 loc) 9.45 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LdapAPI = void 0; var _3kles_corebe_1 = require("@3kles/3kles-corebe"); var utils = require("./ldap.utils"); var ldapts_1 = require("ldapts"); var ldap_parser_response_1 = require("./ldap.parser.response"); var LdapAPI = /** @class */ (function (_super) { __extends(LdapAPI, _super); function LdapAPI() { var _this = _super.call(this) || this; _this.responseParser = new ldap_parser_response_1.LDAPParserResponse(); return _this; } // Function to create options request data LdapAPI.prototype.buildRequest = function (params, inputRequest, data) { var options = params; console.log('Option buildRequest=', options); var body = inputRequest === null || inputRequest === void 0 ? void 0 : inputRequest.body; switch (options.type) { case 'add': options['entry'] = body.entry; options['dn'] = body.dn; break; case 'del': options.dn = body.dn || ''; break; case 'modify': options.dn = body.dn || ''; options.change = body.change; // console.log('ChangeType=', data.change.modification.type); // if (data.change.modification.type === 'unicodePwd') { // // data.change.modification.values = this.encodePassword(data.change.modification.values); // } // console.log('#Change=', data.change.modification.type); // options.change = data.change; break; case 'modifyDN': options.dn = body.dn || ''; options.newDN = body.newDN || ''; break; } // console.log("Options:", options); if (options.entry) { if (options.entry['userPassword']) { options.entry['unicodePwd'] = utils.encodePassword(options.entry['userPassword']); delete options.entry['userPassword']; } } this.client = new ldapts_1.Client(options.client); return options; }; LdapAPI.prototype.execute = function (options) { return __awaiter(this, void 0, void 0, function () { var _a, changes, e_1; var _b; return __generator(this, function (_c) { switch (_c.label) { case 0: _c.trys.push([0, 13, 14, 15]); return [4 /*yield*/, this.client.bind(options.binder.bindUser, options.binder.bindPassword)]; case 1: _c.sent(); _a = options.type; switch (_a) { case 'search': return [3 /*break*/, 2]; case 'add': return [3 /*break*/, 4]; case 'del': return [3 /*break*/, 6]; case 'modify': return [3 /*break*/, 8]; case 'modifyDN': return [3 /*break*/, 10]; } return [3 /*break*/, 12]; case 2: _b = {}; return [4 /*yield*/, this.client.search(options.binder.baseDN, options.searchOptions)]; case 3: return [2 /*return*/, (_b.body = _c.sent(), _b.headers = { 'total-count': 0 }, _b)]; case 4: return [4 /*yield*/, this.client.add(options.dn, options.entry)]; case 5: _c.sent(); return [2 /*return*/, { body: { dn: options.dn, entry: options.entry }, headers: { 'total-count': 0 } }]; case 6: return [4 /*yield*/, this.client.del(options.dn)]; case 7: _c.sent(); return [2 /*return*/, { body: { dn: options.dn }, headers: { 'total-count': 0 } }]; case 8: changes = this.parseChange(options.change); return [4 /*yield*/, this.client.modify(options.dn, changes)]; case 9: _c.sent(); // console.log('Modification done!!!!'); return [2 /*return*/, { body: { dn: options.dn, change: options.change }, headers: { 'total-count': 0 } }]; case 10: return [4 /*yield*/, this.client.modifyDN(options.dn, options.newDN)]; case 11: _c.sent(); return [2 /*return*/, { body: { dn: options.dn, newDN: options.newDN }, headers: { 'total-count': 0 } }]; case 12: return [2 /*return*/]; case 13: e_1 = _c.sent(); console.error('Error:', e_1); throw e_1; case 14: this.client.unbind(); return [7 /*endfinally*/]; case 15: return [2 /*return*/]; } }); }); }; LdapAPI.prototype.parseChange = function (options) { var changes = []; var temp = []; if (!Array.isArray(options)) { temp.push(options); } else { temp = options; } temp.forEach(function (e) { if (e.operation && e.modification) { if (e.modification.type && e.modification.values) { var ch = new ldapts_1.Change({ operation: e.operation, modification: new ldapts_1.Attribute({ type: e.modification.type, values: e.modification.values }) }); changes.push(ch); } } }); return changes; }; return LdapAPI; }(_3kles_corebe_1.AbstractGenericAPI)); exports.LdapAPI = LdapAPI; //# sourceMappingURL=ldap.api.js.map