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       1666x           1666x                 1x 11x 11x 11x 11x 11x 1666x 108x 108x 108x   1666x 1666x 1666x 1666x 407x   1666x 1666x 105x   1666x 11x 11x 62x     1666x 108x 108x     11x     1x 4x 4x 4x 4x 480x 480x   4x     1x 4x 4x   4x 2x   2x     2x 2x              
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';
}