All files / src/sso misc.ts

92.59% Statements 50/54
87.1% Branches 27/31
100% Functions 4/4
92.16% Lines 47/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 82 83 84 85 86 87 881x 1x     1x       6094x           6094x                 1x 48x 48x 48x 48x 48x 6094x 396x 396x 396x   6094x 6094x 6094x 6094x 1517x   6094x 6094x 384x   6094x 47x 47x 242x     6094x 396x 396x     48x     1x 22x 22x 22x 22x 2640x 2640x   22x     1x 22x 22x   22x 11x   11x     11x 11x              
import dbg from 'debug';
import { decode } from 'base64-arraybuffer';
import { MessageType } from './interfaces';
 
const debug = dbg('node-expose-sspi:misc');
 
function isPrintable(keycode: number): boolean {
  const valid =
    (keycode > 47 && keycode < 58) || // number keys
    keycode === 32 || // spacebar & return key(s) (if you want to allow carriage returns)
    (keycode > 64 && keycode < 91) || // letter keys
    (keycode > 95 && keycode < 112) || // numpad keys
    (keycode > 185 && keycode < 193) || // ;=,-./` (in order)
    (keycode > 218 && keycode < 223); // [\]' (in order)
  return valid;
}
 
/**
 * Gives a string representation of binary data.
 *
 * @param {ArrayBuffer} buffer
 * @returns {string} the string representation.
 */
export function hexDump(buffer: ArrayBuffer): string {
  const dataView = new DataView(buffer, 0);
  debug('buffer length', buffer.byteLength);
  let result = '';
  let line = '';
  for (let i = 0; i < buffer.byteLength; i++) {
    if (i % 16 === 0) {
      line = '';
      const address = '0x' + i.toString(10).padStart(8, '0') + ':';
      result += address;
    }
    const n = dataView.getUint8(i);
    result += n.toString(16).padStart(2, '0') + ' ';
    let c = '.';
    if (isPrintable(n)) {
      c = String.fromCharCode(n);
    }
    line += c;
    if (i % 16 === 7) {
      result += ' ';
    }
    if (i === buffer.byteLength - 1) {
      const spaces = '   ';
      for (let j = 0; j < 15 - (i % 16); j++) {
        result += spaces;
      }
    }
    if (i % 16 === 15 || i === buffer.byteLength - 1) {
      result += ': ' + line + '\n';
      continue;
    }
  }
  return result;
}
 
export function toHex(buffer: ArrayBuffer): string {
  const dataView = new DataView(buffer, 0);
  debug('buffer length', buffer.byteLength);
  let result = '';
  for (let i = 0; i < buffer.byteLength; i++) {
    const n = dataView.getUint8(i);
    result += n.toString(16).padStart(2, '0');
  }
  return result;
}
 
export function getMessageType(token: string): MessageType {
  const buffer = decode(token);
  const str = toHex(buffer);
  // manage NTLM
  if (str.includes('4e544c4d53535000' + '01')) {
    return 'NTLM_NEGOTIATE_01';
  }
  Iif (str.includes('4e544c4d53535000' + '02')) {
    return 'NTLM_CHALLENGE_02';
  }
  Eif (str.includes('4e544c4d53535000' + '03')) {
    return 'NTLM_AUTHENTICATE_03';
  }
  // manage Kerberos:
  if (token.startsWith('YII')) {
    return 'Kerberos_1';
  }
  return 'Kerberos_N';
}