{"version":3,"file":"digest.mjs","names":["parseHTTPHeadersQuotedKeyValueSet","buildHTTPHeadersQuotedKeyValueSet","crypto","REQUIRED_WWW_AUTHENTICATE_KEYS","AUTHORIZED_WWW_AUTHENTICATE_KEYS","REQUIRED_AUTHORIZATION_KEYS","AUTHORIZED_AUTHORIZATION_KEYS","DIGEST","type","parseWWWAuthenticateRest","rest","buildWWWAuthenticateRest","data","parseAuthorizationRest","buildAuthorizationRest","computeHash","ha1","_computeHash","algorithm","username","realm","password","join","ha2","method","uri","nonce","nc","cnonce","qop","str","hashsum","createHash","update","digest"],"sources":["../../src/mechanisms/digest.ts"],"sourcesContent":["/**\n * @module http-auth-utils/mechanisms/digest\n */\n\nimport {\n  parseHTTPHeadersQuotedKeyValueSet,\n  buildHTTPHeadersQuotedKeyValueSet,\n} from '../utils';\n\nimport crypto from 'crypto';\n\nconst REQUIRED_WWW_AUTHENTICATE_KEYS = ['realm', 'nonce'];\nconst AUTHORIZED_WWW_AUTHENTICATE_KEYS = [\n  ...REQUIRED_WWW_AUTHENTICATE_KEYS,\n  'domain',\n  'opaque',\n  'stale',\n  'algorithm',\n  'qop',\n];\ntype DigestWWWAuthenticateData = {\n  realm: string;\n  domain?: string;\n  nonce: string;\n  opaque?: string;\n  stale?: 'true' | 'false';\n  algorithm?: 'MD5' | 'MD5-sess' | 'token';\n  qop?: 'auth' | 'auth-int' | string;\n};\nconst REQUIRED_AUTHORIZATION_KEYS = [\n  'username',\n  'realm',\n  'nonce',\n  'uri',\n  'response',\n];\nconst AUTHORIZED_AUTHORIZATION_KEYS = [\n  ...REQUIRED_AUTHORIZATION_KEYS,\n  'algorithm',\n  'cnonce',\n  'opaque',\n  'qop',\n  'nc',\n];\n\ntype DigestAuthorizationData = {\n  username: string;\n  realm: string;\n  nonce: string;\n  uri: string;\n  response: string;\n  algorithm?: string;\n  cnonce?: string;\n  opaque?: string;\n  qop?: string;\n  nc?: string;\n};\n/* Architecture Note #1.3: Digest mechanism\n\nSee the following [RFC](https://tools.ietf.org/html/rfc2617).\n\n*/\n\n/**\n * Digest authentication mechanism.\n * @type {Object}\n * @see http://tools.ietf.org/html/rfc2617#section-3\n * @see http://tools.ietf.org/html/rfc2069#section-2\n */\nconst DIGEST = {\n  /**\n   * The Digest auth mechanism prefix.\n   * @type {String}\n   */\n  type: 'Digest',\n\n  /**\n   * Parse the WWW Authenticate header rest.\n   * @param  {String} rest The header rest (string after the authentication mechanism prefix).\n   * @return {Object}      Object representing the result of the parse operation.\n   * @example\n   * assert.deepEqual(\n   *   DIGEST.parseWWWAuthenticateRest(\n   *     'realm=\"testrealm@host.com\", ' +\n   *     'qop=\"auth, auth-int\", ' +\n   *     'nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", ' +\n   *     'opaque=\"5ccc069c403ebaf9f0171e9517f40e41\"'\n   *   ), {\n   *     realm: 'testrealm@host.com',\n   *     qop: 'auth, auth-int',\n   *     nonce: 'dcd98b7102dd2f0e8b11d0f600bfb0c093',\n   *     opaque: '5ccc069c403ebaf9f0171e9517f40e41'\n   *   }\n   * );\n   * @api public\n   */\n  parseWWWAuthenticateRest: function parseWWWAuthenticateRest(\n    rest: string,\n  ): DigestWWWAuthenticateData {\n    return parseHTTPHeadersQuotedKeyValueSet(\n      rest,\n      AUTHORIZED_WWW_AUTHENTICATE_KEYS,\n      REQUIRED_WWW_AUTHENTICATE_KEYS,\n    ) as DigestWWWAuthenticateData;\n  },\n\n  /**\n   * Build the WWW Authenticate header rest.\n   * @param  {Object} data The content from wich to build the rest.\n   * @return {String}         The built rest.\n   * @example\n   * assert.equal(\n   *   DIGEST.buildWWWAuthenticateRest({\n   *     realm: 'testrealm@host.com',\n   *     qop: 'auth, auth-int',\n   *     nonce: 'dcd98b7102dd2f0e8b11d0f600bfb0c093',\n   *     opaque: '5ccc069c403ebaf9f0171e9517f40e41'\n   *   }),\n   *   'realm=\"testrealm@host.com\", ' +\n   *   'qop=\"auth, auth-int\", ' +\n   *   'nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", ' +\n   *   'opaque=\"5ccc069c403ebaf9f0171e9517f40e41\"'\n   * );\n   * @api public\n   */\n  buildWWWAuthenticateRest: function buildWWWAuthenticateRest(\n    data: DigestWWWAuthenticateData,\n  ): string {\n    return buildHTTPHeadersQuotedKeyValueSet(\n      data,\n      AUTHORIZED_WWW_AUTHENTICATE_KEYS,\n      REQUIRED_WWW_AUTHENTICATE_KEYS,\n    );\n  },\n\n  /**\n   * Parse the Authorization header rest.\n   * @param  {String} rest The header rest (string after the authentication mechanism prefix).)\n   * @return {Object}      Object representing the result of the parse operation {hash}.\n   * @example\n   * assert.deepEqual(\n   *   DIGEST.parseAuthorizationRest(\n   *     'username=\"Mufasa\",' +\n   *     'realm=\"testrealm@host.com\",' +\n   *     'nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\",' +\n   *     'uri=\"/dir/index.html\",' +\n   *     'qop=\"auth\",' +\n   *     'nc=\"00000001\",' +\n   *     'cnonce=\"0a4f113b\",' +\n   *     'response=\"6629fae49393a05397450978507c4ef1\",' +\n   *     'opaque=\"5ccc069c403ebaf9f0171e9517f40e41\"'\n   *   ), {\n   *     username: \"Mufasa\",\n   *     realm: 'testrealm@host.com',\n   *     nonce: \"dcd98b7102dd2f0e8b11d0f600bfb0c093\",\n   *     uri: \"/dir/index.html\",\n   *     qop: 'auth',\n   *     nc: '00000001',\n   *     cnonce: \"0a4f113b\",\n   *     response: \"6629fae49393a05397450978507c4ef1\",\n   *     opaque: \"5ccc069c403ebaf9f0171e9517f40e41\"\n   *   }\n   * );\n   * @api public\n   */\n  parseAuthorizationRest: function parseAuthorizationRest(\n    rest: string,\n  ): DigestAuthorizationData {\n    return parseHTTPHeadersQuotedKeyValueSet(\n      rest,\n      AUTHORIZED_AUTHORIZATION_KEYS,\n      REQUIRED_AUTHORIZATION_KEYS,\n    ) as DigestAuthorizationData;\n  },\n\n  /**\n   * Build the Authorization header rest.\n   * @param  {Object} data The content from wich to build the rest.\n   * @return {String}         The rest built.\n   * @example\n   * assert.equal(\n   *   DIGEST.buildAuthorizationRest({\n   *     username: \"Mufasa\",\n   *     realm: 'testrealm@host.com',\n   *     nonce: \"dcd98b7102dd2f0e8b11d0f600bfb0c093\",\n   *     uri: \"/dir/index.html\",\n   *     qop: 'auth',\n   *     nc: '00000001',\n   *     cnonce: \"0a4f113b\",\n   *     response: \"6629fae49393a05397450978507c4ef1\",\n   *     opaque: \"5ccc069c403ebaf9f0171e9517f40e41\"\n   *   }),\n   *   'username=\"Mufasa\", ' +\n   *   'realm=\"testrealm@host.com\", ' +\n   *   'nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", ' +\n   *   'uri=\"/dir/index.html\", ' +\n   *   'response=\"6629fae49393a05397450978507c4ef1\", ' +\n   *   'cnonce=\"0a4f113b\", ' +\n   *   'opaque=\"5ccc069c403ebaf9f0171e9517f40e41\", ' +\n   *   'qop=\"auth\", ' +\n   *   'nc=\"00000001\"'\n   * );\n   * @api public\n   */\n  buildAuthorizationRest: function buildAuthorizationRest(\n    data: DigestAuthorizationData,\n  ): string {\n    return buildHTTPHeadersQuotedKeyValueSet(\n      data,\n      AUTHORIZED_AUTHORIZATION_KEYS,\n      REQUIRED_AUTHORIZATION_KEYS,\n    );\n  },\n\n  /**\n   * Compute the Digest authentication hash from the given credentials.\n   * @param  {Object} data The credentials to encode and other encoding details.\n   * @return {String}             The hash representing the credentials.\n   * @example\n   * assert.equal(\n   *   DIGEST.computeHash({\n   *     username: 'Mufasa',\n   *     realm: 'testrealm@host.com',\n   *     password: 'Circle Of Life',\n   *     method: 'GET',\n   *     uri: '/dir/index.html',\n   *     nonce: 'dcd98b7102dd2f0e8b11d0f600bfb0c093',\n   *     nc: '00000001',\n   *     cnonce: '0a4f113b',\n   *     qop: 'auth',\n   *     algorithm: 'md5'\n   *   }),\n   *   '6629fae49393a05397450978507c4ef1'\n   * );\n   * @api public\n   */\n  computeHash: function computeHash(data: {\n    ha1?: string;\n    algorithm: string;\n    method: string;\n    uri: string;\n    realm: string;\n    username: string;\n    password: string;\n    nonce: string;\n    nc: string;\n    cnonce: string;\n    qop: string;\n  }): string {\n    const ha1 =\n      data.ha1 ||\n      _computeHash(\n        data.algorithm,\n        [data.username, data.realm, data.password].join(':'),\n      );\n    const ha2 = _computeHash(data.algorithm, [data.method, data.uri].join(':'));\n\n    return _computeHash(\n      data.algorithm,\n      [ha1, data.nonce, data.nc, data.cnonce, data.qop, ha2].join(':'),\n    );\n  },\n};\n\nfunction _computeHash(algorithm: string, str: string): string {\n  const hashsum = crypto.createHash(algorithm);\n\n  hashsum.update(str);\n  return hashsum.digest('hex');\n}\n\nexport default DIGEST;\n"],"mappings":"AAIA,SACEA,iCADF,EAEEC,iCAFF,QAGO,UAHP;AAKA,OAAOC,MAAP,MAAmB,QAAnB;AAEA,MAAMC,8BAA8B,GAAG,CAAC,OAAD,EAAU,OAAV,CAAvC;AACA,MAAMC,gCAAgC,GAAG,CACvC,GAAGD,8BADoC,EAEvC,QAFuC,EAGvC,QAHuC,EAIvC,OAJuC,EAKvC,WALuC,EAMvC,KANuC,CAAzC;AAiBA,MAAME,2BAA2B,GAAG,CAClC,UADkC,EAElC,OAFkC,EAGlC,OAHkC,EAIlC,KAJkC,EAKlC,UALkC,CAApC;AAOA,MAAMC,6BAA6B,GAAG,CACpC,GAAGD,2BADiC,EAEpC,WAFoC,EAGpC,QAHoC,EAIpC,QAJoC,EAKpC,KALoC,EAMpC,IANoC,CAAtC;AAiCA,MAAME,MAAM,GAAG;EAKbC,IAAI,EAAE,QALO;EA2BbC,wBAAwB,EAAE,SAASA,wBAAT,CACxBC,IADwB,EAEG;IAC3B,OAAOV,iCAAiC,CACtCU,IADsC,EAEtCN,gCAFsC,EAGtCD,8BAHsC,CAAxC;EAKD,CAnCY;EAwDbQ,wBAAwB,EAAE,SAASA,wBAAT,CACxBC,IADwB,EAEhB;IACR,OAAOX,iCAAiC,CACtCW,IADsC,EAEtCR,gCAFsC,EAGtCD,8BAHs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