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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import dbg from 'debug'; import fetch, { RequestInit, Response } from 'node-fetch'; import { URL } from 'url'; import { Props } from '../../../lib/api'; import { AbstractHandler } from './AbstractHandler'; import { ClientCookie } from './ClientCookie'; import { ClientInfo } from './ClientInfo'; import { md5 } from './misc'; interface DigestChallenge { realm: string; qop: string; nonce: string; opaque?: string; algorithm: string; } interface DigestAnswer { username: string; realm: string; nonce: string; uri: string; algorithm: string; qop: string; nc: string; cnonce: string; response: string; opaque?: string; } const debug = dbg('node-expose-sspi:client'); // https://tools.ietf.org/html/rfc7616 export class DigestHandler extends AbstractHandler { async handle( clientInfo: ClientInfo, clientCookie: ClientCookie, response: Response, resource: string, init: RequestInit = {} ): Promise<Response> { debug('digest handler'); // get the info from the WWW-Authenticate header. const digestHeader = response.headers .get('www-authenticate') ?.substring('Digest '.length); Iif (!digestHeader) { return response; } debug('digestHeader: ', digestHeader); const digestChallenge = (digestHeader.split(/, */).reduce((acc, prop) => { const [key, value] = prop.split('='); acc[key] = value.replace(/^"?(.*?)"?$/, '$1'); return acc; }, {} as Props) as unknown) as DigestChallenge; debug('digestChallenge: ', digestChallenge); const requestInit: RequestInit = { ...init }; // client nonce. const cnonce = md5(Math.round(Math.random() * 1e10).toString(16)).substr( 0, 16 ); // HA1 = MD5(username:realm:password) const ha1 = getHA1(clientInfo, digestChallenge, cnonce); // HA2 = MD5(method:digestURI) const method = requestInit.method ?? 'GET'; const entityBody = (requestInit.body as string) ?? ''; const digestURI = new URL(resource).pathname; const ha2 = getHA2(digestChallenge.qop, method, digestURI, entityBody); // response = MD5(HA1:nonce:nonceCount:cnonce:qop:HA2) const nonceCount = '00000001'; const qop = 'auth'; const digestResponse = md5( `${ha1}:${digestChallenge.nonce}:${nonceCount}:${cnonce}:${qop}:${ha2}` ); debug('digestChallenge.nonce: ', digestChallenge.nonce); const digestAnswer: DigestAnswer = { username: `"${clientInfo.user}"`, realm: `"${digestChallenge.realm}"`, nonce: `"${digestChallenge.nonce}"`, uri: `"${digestURI}"`, algorithm: digestChallenge.algorithm, response: `"${digestResponse}"`, qop: qop, nc: nonceCount, cnonce: `"${cnonce}"`, }; Iif (digestChallenge.opaque) { digestAnswer.opaque = digestChallenge.opaque; } debug('digestAnswer: ', digestAnswer); requestInit.headers = { ...init.headers, Authorization: 'Digest ' + Object.keys(digestAnswer) .map((k) => `${k}=${((digestAnswer as unknown) as Props)[k]}`) .join(', '), }; clientCookie.restituteCookies(requestInit); debug('first requestInit.headers', requestInit.headers); response = await fetch(resource, requestInit); debug('first response.headers', response.headers); clientCookie.saveCookies(response); return response; } } function getHA1( clientInfo: ClientInfo, digest: DigestChallenge, cnonce: string ): string { Iif (!clientInfo.user) { throw new Error('needs a username'); } Iif (!clientInfo.password) { throw new Error('needs a password'); } const a1 = `${clientInfo.user}:${digest.realm}:${clientInfo.password}`; const ha1 = md5(a1); Iif (digest.algorithm === 'MD5-sess') { // MD5(MD5(username:realm:password):nonce:cnonce) const str2 = `${ha1}:${digest.nonce}:${cnonce}`; debug('str2: ', str2); return md5(str2); } return ha1; } function getHA2( qop: string, method: string, digestURI: string, entityBody: string ) { Iif (qop === 'auth-int') { // HA2 = MD5(method:digestURI:MD5(entityBody)) const a2 = `${method}:${digestURI}:${md5(entityBody)}`; debug('a2: ', a2); return md5(a2); } const ha2 = md5(`${method}:${digestURI}`); return ha2; } |