{"version":3,"file":"bearer.mjs","names":["YError","parseHTTPHeadersQuotedKeyValueSet","buildHTTPHeadersQuotedKeyValueSet","AUTHORIZED_ERROR_CODES","REQUIRED_WWW_AUTHENTICATE_KEYS","AUTHORIZED_WWW_AUTHENTICATE_KEYS","BEARER","type","parseWWWAuthenticateRest","rest","buildWWWAuthenticateRest","data","error","indexOf","parseAuthorizationRest","hash","buildAuthorizationRest"],"sources":["../../src/mechanisms/bearer.ts"],"sourcesContent":["/**\n * @module http-auth-utils/mechanisms/bearer\n */\n\nimport YError from 'yerror';\n\nimport {\n  parseHTTPHeadersQuotedKeyValueSet,\n  buildHTTPHeadersQuotedKeyValueSet,\n} from '../utils';\n\nconst AUTHORIZED_ERROR_CODES = [\n  'invalid_request',\n  'invalid_token',\n  'insufficient_scope',\n] as const;\nconst REQUIRED_WWW_AUTHENTICATE_KEYS = ['realm'];\nconst AUTHORIZED_WWW_AUTHENTICATE_KEYS = [\n  ...REQUIRED_WWW_AUTHENTICATE_KEYS,\n  'scope',\n  'error',\n  'error_description',\n];\ntype BearerWWWAuthenticateData = {\n  realm: string;\n  scope?: string;\n  error?: typeof AUTHORIZED_ERROR_CODES[number];\n  error_description?: string;\n};\ntype BearerAuthorizationData = {\n  hash: string;\n};\n\ntype BearerAuthorizedErrorCodes = typeof AUTHORIZED_ERROR_CODES[number];\n\n/* Architecture Note #1.1: Bearer mechanism\n\nSee the following [RFC](https://tools.ietf.org/html/rfc6750).\n\n*/\n\n/**\n * Bearer authentication mechanism.\n * @type {Object}\n * @see https://tools.ietf.org/html/rfc6750#section-3\n */\nconst BEARER = {\n  /**\n   * The Bearer auth mechanism prefix.\n   * @type {String}\n   */\n  type: 'Bearer',\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   *   BEARER.parseWWWAuthenticateRest(\n   *     'realm=\"testrealm@host.com\", ' +\n   *     'scope=\"openid profile email\"'\n   *   ), {\n   *     realm: 'testrealm@host.com',\n   *     scope: 'openid profile email',\n   *   }\n   * );\n   * @api public\n   */\n  parseWWWAuthenticateRest: function parseWWWAuthenticateRest(\n    rest: string,\n  ): BearerWWWAuthenticateData {\n    return parseHTTPHeadersQuotedKeyValueSet(\n      rest,\n      AUTHORIZED_WWW_AUTHENTICATE_KEYS as unknown as string[],\n      [],\n    ) as BearerWWWAuthenticateData;\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   *   BEARER.buildWWWAuthenticateRest({\n   *     realm: 'testrealm@host.com',\n   *     error: 'invalid_request',\n   *     error_description: 'The access token expired',\n   *   }),\n   *   'realm=\"testrealm@host.com\", ' +\n   *   'error=\"invalid_request\", ' +\n   *   'error_description=\"The access token expired\"'\n   * );\n   * @api public\n   */\n  buildWWWAuthenticateRest: function buildWWWAuthenticateRest(\n    data: BearerWWWAuthenticateData,\n  ): string {\n    if (\n      data.error &&\n      -1 ===\n        AUTHORIZED_ERROR_CODES.indexOf(\n          data.error as unknown as BearerAuthorizedErrorCodes,\n        )\n    ) {\n      throw new YError('E_INVALID_ERROR', data.error, AUTHORIZED_ERROR_CODES);\n    }\n    return buildHTTPHeadersQuotedKeyValueSet(\n      data,\n      AUTHORIZED_WWW_AUTHENTICATE_KEYS as unknown as string[],\n      [],\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   *   BEARER.parseAuthorizationRest('mF_9.B5f-4.1JqM'), {\n   *     hash: 'mF_9.B5f-4.1JqM',\n   *   }\n   * );\n   * @api public\n   */\n  parseAuthorizationRest: function parseAuthorizationRest(\n    rest: string,\n  ): BearerAuthorizationData {\n    if (!rest) {\n      throw new YError('E_EMPTY_AUTH');\n    }\n    return {\n      hash: rest,\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   *   BEARER.buildAuthorizationRest({\n   *     hash: 'mF_9.B5f-4.1JqM'\n   *   }),\n   *   'mF_9.B5f-4.1JqM=='\n   * );\n   * @api public\n   */\n  buildAuthorizationRest: function buildAuthorizationRest({\n    hash,\n  }: BearerAuthorizationData): string {\n    if (!hash) {\n      throw new YError('E_NO_HASH');\n    }\n    return hash;\n  },\n};\n\nexport default BEARER;\n"],"mappings":"AAIA,OAAOA,MAAP,MAAmB,QAAnB;AAEA,SACEC,iCADF,EAEEC,iCAFF,QAGO,UAHP;AAKA,MAAMC,sBAAsB,GAAG,CAC7B,iBAD6B,EAE7B,eAF6B,EAG7B,oBAH6B,CAA/B;AAKA,MAAMC,8BAA8B,GAAG,CAAC,OAAD,CAAvC;AACA,MAAMC,gCAAgC,GAAG,CACvC,GAAGD,8BADoC,EAEvC,OAFuC,EAGvC,OAHuC,EAIvC,mBAJuC,CAAzC;AA6BA,MAAME,MAAM,GAAG;EAKbC,IAAI,EAAE,QALO;EAuBbC,wBAAwB,EAAE,SAASA,wBAAT,CACxBC,IADwB,EAEG;IAC3B,OAAOR,iCAAiC,CACtCQ,IADsC,EAEtCJ,gCAFsC,EAGtC,EAHsC,CAAxC;EAKD,CA/BY;EAkDbK,wBAAwB,EAAE,SAASA,wBAAT,CACxBC,IADwB,EAEhB;IACR,IACEA,IAAI,CAACC,KAAL,IACA,CAAC,CAAD,KACET,sBAAsB,CAACU,OAAvB,CACEF,IAAI,CAACC,KADP,CAHJ,EAME;MACA,MAAM,IAAIZ,MAAJ,CAAW,iBAAX,EAA8BW,IAAI,CAACC,KAAnC,EAA0CT,sBAA1C,CAAN;IACD;;IACD,OAAOD,iCAAiC,CACtCS,IADsC,EAEtCN,gCAFsC,EAGtC,EAHsC,CAAxC;EAKD,CAnEY;EAiFbS,sBAAsB,EAAE,SAASA,sBAAT,CACtBL,IADsB,EAEG;IACzB,IAAI,CAACA,IAAL,EAAW;MACT,MAAM,IAAIT,MAAJ,CAAW,cAAX,CAAN;IACD;;IACD,OAAO;MACLe,IAAI,EAAEN;IADD,CAAP;EAGD,CA1FY;EAyGbO,sBAAsB,EAAE,SAASA,sBAAT,CAAgC;IACtDD;EADsD,CAAhC,EAEY;IAClC,IAAI,CAACA,IAAL,EAAW;MACT,MAAM,IAAIf,MAAJ,CAAW,WAAX,CAAN;IACD;;IACD,OAAOe,IAAP;EACD;AAhHY,CAAf;AAmHA,eAAeT,MAAf"}