All files / src/sso/client NegotiateHandler.ts

96.49% Statements 55/57
76.19% Branches 16/21
100% Functions 2/2
96.49% Lines 55/57

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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 1301x 1x 1x   1x         1x     1x 1x   1x   1x 4x 4x                   4x 4x 1x   4x 4x   4x       4x 1x           4x 4x     4x     4x 4x                           4x             7x     7x 7x 3x     3x     3x 3x 3x 3x 3x       3x 3x     7x 7x 6x 6x 6x             6x 6x 6x 6x 6x       6x 6x 6x 6x 6x 6x   3x 3x      
import dbg from 'debug';
import fetch, { RequestInit, Response } from 'node-fetch';
import { negotiateParse } from '../msgParser';
 
import {
  sspi,
  InitializeSecurityContextInput,
  AcquireCredHandleInput,
} from '../../../lib/api';
import { getSPNFromURI } from './misc';
import { ClientCookie } from './ClientCookie';
import { ClientInfo } from './ClientInfo';
import { AbstractHandler } from './AbstractHandler';
import { decode, encode, hexDump } from '../misc';
 
const debug = dbg('node-expose-sspi:client');
 
export class NegotiateHandler extends AbstractHandler {
  constructor(private authenticationType = 'Negotiate') {
    super();
  }
 
  async handle(
    clientInfo: ClientInfo,
    clientCookie: ClientCookie,
    response: Response,
    resource: string,
    init: RequestInit = {}
  ): Promise<Response> {
    debug('starting negotiate auth');
    if (this.authenticationType === 'NTLM') {
      clientInfo.ssp = 'NTLM';
    }
    const packageInfo = sspi.QuerySecurityPackageInfo(clientInfo.ssp);
    debug('packageInfo: ', packageInfo);
 
    const credInput = {
      packageName: packageInfo.Name,
      credentialUse: 'SECPKG_CRED_OUTBOUND',
    } as AcquireCredHandleInput;
    if (clientInfo.user) {
      credInput.authData = {
        domain: clientInfo.domain,
        user: clientInfo.user,
        password: clientInfo.password,
      };
    }
    debug('credInput: ', credInput);
    const clientCred = sspi.AcquireCredentialsHandle(credInput);
 
    const targetName =
      clientInfo.targetName === undefined
        ? await getSPNFromURI(resource)
        : clientInfo.targetName;
    debug('targetName: ', targetName);
    const input: InitializeSecurityContextInput = {
      isFirstCall: true,
      credential: clientCred.credential,
      targetName,
      contextReq: [
        'ISC_REQ_CONNECTION',
        'ISC_REQ_CONFIDENTIALITY',
        'ISC_REQ_MUTUAL_AUTH',
        'ISC_REQ_REPLAY_DETECT',
      ],
      cbMaxToken: packageInfo.cbMaxToken,
      targetDataRep: 'SECURITY_NATIVE_DREP',
    };
    let clientSecurityContext;
    while (
      response.headers.has('www-authenticate') &&
      response.status === 401 &&
      response.headers
        .get('www-authenticate')
        ?.startsWith(this.authenticationType)
    ) {
      const wwwAuthenticateHeader = response.headers.get(
        'www-authenticate'
      ) as string;
      debug('wwwAuthenticateHeader: ', wwwAuthenticateHeader);
      if (clientSecurityContext) {
        Iif (!wwwAuthenticateHeader.startsWith(this.authenticationType + ' ')) {
          break;
        }
        const bufferStr = wwwAuthenticateHeader
          .split(',')[0]
          .substr((this.authenticationType + ' ').length);
        debug('bufferStr: ', bufferStr);
        const responseToken = negotiateParse(bufferStr);
        debug('responseToken: ', responseToken);
        const buffer = decode(bufferStr);
        input.SecBufferDesc = {
          ulVersion: 0,
          buffers: [buffer],
        };
        input.contextHandle = clientSecurityContext.contextHandle;
        input.isFirstCall = false;
      }
 
      debug('input: ', input);
      clientSecurityContext = sspi.InitializeSecurityContext(input);
      debug('clientSecurityContext: ', clientSecurityContext);
      debug(hexDump(clientSecurityContext.SecBufferDesc.buffers[0]));
      Iif (
        ['SEC_I_COMPLETE_NEEDED', 'SEC_I_COMPLETE_AND_CONTINUE'].includes(
          clientSecurityContext.SECURITY_STATUS
        )
      ) {
        debug('you should not see this');
      }
      const base64 = encode(clientSecurityContext.SecBufferDesc.buffers[0]);
      const debugObject = negotiateParse(base64);
      debug('debugObject: ', debugObject);
      const requestInit = { ...init };
      requestInit.headers = {
        ...init.headers,
        Authorization: this.authenticationType + ' ' + base64,
      };
      clientCookie.restituteCookies(requestInit);
      debug('requestInit.headers', requestInit.headers);
      response = await fetch(resource, requestInit);
      debug('response.status', response.status);
      debug('response.headers', response.headers);
      clientCookie.saveCookies(response);
    }
    debug('handleAuth: end');
    return response;
  }
}