{"version":3,"file":"basic.mjs","names":["YError","parseHTTPHeadersQuotedKeyValueSet","buildHTTPHeadersQuotedKeyValueSet","REQUIRED_WWW_AUTHENTICATE_KEYS","AUTHORIZED_WWW_AUTHENTICATE_KEYS","BASIC","type","parseWWWAuthenticateRest","rest","buildWWWAuthenticateRest","data","parseAuthorizationRest","username","password","decodeHash","hash","buildAuthorizationRest","computeHash","Buffer","from","toString","passwordParts","split","join"],"sources":["../../src/mechanisms/basic.ts"],"sourcesContent":["/**\n * @module http-auth-utils/mechanisms/basic\n */\n\nimport YError from 'yerror';\n\nimport {\n  parseHTTPHeadersQuotedKeyValueSet,\n  buildHTTPHeadersQuotedKeyValueSet,\n} from '../utils';\n\nconst REQUIRED_WWW_AUTHENTICATE_KEYS = ['realm'];\nconst AUTHORIZED_WWW_AUTHENTICATE_KEYS = REQUIRED_WWW_AUTHENTICATE_KEYS;\ntype BasicWWWAuthenticateData = {\n  realm: string;\n};\ntype BasicAuthorizationData = {\n  username: string;\n  password: string;\n};\n\n/* Architecture Note #1.2: Basic mechanism\n\nSee the following [RFC](https://tools.ietf.org/html/rfc7617).\n\n*/\n\n/**\n * Basic authentication mechanism.\n * @type {Object}\n * @see http://tools.ietf.org/html/rfc2617#section-2\n */\nconst BASIC = {\n  /**\n   * The Basic auth mechanism prefix.\n   * @type {String}\n   */\n  type: 'Basic',\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   *   BASIC.parseWWWAuthenticateRest('realm=\"perlinpinpin\"'), {\n   *     realm: 'perlinpinpin'\n   *   }\n   * );\n   * @api public\n   */\n  parseWWWAuthenticateRest: function parseWWWAuthenticateRest(\n    rest: string,\n  ): BasicWWWAuthenticateData {\n    return parseHTTPHeadersQuotedKeyValueSet(\n      rest,\n      AUTHORIZED_WWW_AUTHENTICATE_KEYS,\n      REQUIRED_WWW_AUTHENTICATE_KEYS,\n    ) as BasicWWWAuthenticateData;\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   *   BASIC.buildWWWAuthenticateRest({\n   *     realm: 'perlinpinpin'\n   *   }),\n   *   'realm=\"perlinpinpin\"'\n   * );\n   * @api public\n   */\n  buildWWWAuthenticateRest: function buildWWWAuthenticateRest(\n    data: BasicWWWAuthenticateData,\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   *   BASIC.parseAuthorizationRest('QWxpIEJhYmE6b3BlbiBzZXNhbWU='), {\n   *     hash: 'QWxpIEJhYmE6b3BlbiBzZXNhbWU=',\n   *     username: 'Ali Baba',\n   *     password: 'open sesame'\n   *   }\n   * );\n   * @api public\n   */\n  parseAuthorizationRest: function parseAuthorizationRest(\n    rest: string,\n  ): BasicAuthorizationData & { hash: string } {\n    if (!rest) {\n      throw new YError('E_EMPTY_AUTH');\n    }\n    const { username, password } = BASIC.decodeHash(rest);\n\n    return {\n      hash: rest,\n      username,\n      password,\n    };\n  },\n\n  /**\n   * Build the Authorization header rest.\n   * @param  {Object} content The content from wich to build the rest.\n   * @return {String}         The rest built.\n   * @example\n   * assert.equal(\n   *   BASIC.buildAuthorizationRest({\n   *     hash: 'QWxpIEJhYmE6b3BlbiBzZXNhbWU='\n   *   }),\n   *   'QWxpIEJhYmE6b3BlbiBzZXNhbWU='\n   * );\n   * @api public\n   */\n  buildAuthorizationRest: function buildAuthorizationRest({\n    hash,\n    username,\n    password,\n  }:\n    | ({\n        hash: string;\n      } & Partial<BasicAuthorizationData>)\n    | (BasicAuthorizationData & {\n        hash?: string;\n      })): string {\n    if (username && password) {\n      return BASIC.computeHash({\n        username,\n        password,\n      });\n    }\n    if (!hash) {\n      throw new YError('E_NO_HASH');\n    }\n    return hash;\n  },\n\n  /**\n   * Compute the Basic authentication hash from the given credentials.\n   * @param  {Object} credentials The credentials to encode {username, password}.\n   * @return {String}             The hash representing the credentials.\n   * @example\n   * assert.equal(\n   *   BASIC.computeHash({\n   *     username: 'Ali Baba',\n   *     password: 'open sesame'\n   *   }),\n   *   'QWxpIEJhYmE6b3BlbiBzZXNhbWU='\n   * );\n   * @api public\n   */\n  computeHash: function computeHash({\n    username,\n    password,\n  }: BasicAuthorizationData): string {\n    return Buffer.from(username + ':' + password).toString('base64');\n  },\n\n  /**\n   * Decode the Basic hash and return the corresponding credentials.\n   * @param  {String} hash The hash.\n   * @return {Object}      Object representing the credentials {username, password}.\n   * @example\n   * assert.deepEqual(\n   *   BASIC.decodeHash('QWxpIEJhYmE6b3BlbiBzZXNhbWU='), {\n   *     username: 'Ali Baba',\n   *     password: 'open sesame'\n   *   }\n   * );\n   * @api public\n   */\n  decodeHash: function decodeHash(hash: string): BasicAuthorizationData {\n    const [username, ...passwordParts] = Buffer.from(hash, 'base64')\n      .toString()\n      .split(':');\n\n    return {\n      username,\n      password: passwordParts.join(':'),\n    };\n  },\n};\n\nexport default BASIC;\n"],"mappings":"AAIA,OAAOA,MAAP,MAAmB,QAAnB;AAEA,SACEC,iCADF,EAEEC,iCAFF,QAGO,UAHP;AAKA,MAAMC,8BAA8B,GAAG,CAAC,OAAD,CAAvC;AACA,MAAMC,gCAAgC,GAAGD,8BAAzC;AAoBA,MAAME,KAAK,GAAG;EAKZC,IAAI,EAAE,OALM;EAmBZC,wBAAwB,EAAE,SAASA,wBAAT,CACxBC,IADwB,EAEE;IAC1B,OAAOP,iCAAiC,CACtCO,IADsC,EAEtCJ,gCAFsC,EAGtCD,8BAHsC,CAAxC;EAKD,CA3BW;EA0CZM,wBAAwB,EAA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