UNPKG

adauth

Version:

Authenticate against an Active Directory domain via LDAP

79 lines (78 loc) 2.55 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.escapeADString = exports.binaryGUIDToString = exports.binarySIDToString = void 0; const long_1 = __importDefault(require("long")); const binarySIDToString = (binarySID) => { let sid = 'S-' + binarySID[0].toString(); const subAuthCount = binarySID[1] & 0xff; let authority; for (let i = 2; i <= 7; i++) { authority |= binarySID[i] << (8 * (5 - (i - 2))); } sid += '-' + authority.toString(16); let offset = 8; const size = 4; let subAuth; for (let i = 0; i < subAuthCount; i++) { subAuth = long_1.default.fromNumber(0); for (let j = 0; j < size; j++) { subAuth = subAuth.or(long_1.default.fromNumber(binarySID[offset + j] & 0xff).shiftLeft(8 * j)); } sid += '-' + subAuth.toString(); offset += size; } return sid; }; exports.binarySIDToString = binarySIDToString; const binaryGUIDToString = (binaryGUID) => { let guid = '{'; let idx; for (let i = 0; i < binaryGUID.length; i++) { if (i < 4) { idx = 3 - i; } else if (i === 4 || i === 6) { idx = i + 1; } else if (i === 5 || i === 7) { idx = i - 1; } else { idx = i; } guid += (binaryGUID[idx] < 0x10 ? '0' : '') + binaryGUID[idx].toString(16) + (i === 3 || i === 5 || i === 7 || i === 9 ? '-' : ''); } return guid + '}'; }; exports.binaryGUIDToString = binaryGUIDToString; const adUnsafeChars = /[^ a-zA-Z0-9.&\-_[\]`~|@$%^?:{}!']/g; const adSpecialChars = new Set([',', '\\', '#', '+', '<', '>', ';', '"', '=']); const escapeADString = (str) => { let hex; let es = str.replace(adUnsafeChars, match => { if (adSpecialChars[match]) { return '\\' + match; } else { hex = match.charCodeAt(0).toString(16); if (hex.length % 2 !== 0) { hex = '0' + hex; } return '\\' + hex; } }); if (es.charAt(0) === ' ') { es = '\\20' + (es.length > 1 ? es.substring(1) : ''); } if (es.charAt(es.length - 1) === ' ') { es = (es.length > 1 ? es.substring(0, es.length - 1) : '') + '\\20'; } return es; }; exports.escapeADString = escapeADString;