{"version":3,"file":"index.mjs","sources":["../../../../src/auth/index.ts"],"sourcesContent":["import {NextFunction, Request, RequestHandler, Response} from 'express';\n\nimport {ApiAndConfigParams} from '../types';\nimport {redirectToAuth} from '../redirect-to-auth';\n\nimport {authCallback} from './auth-callback';\nimport {AuthMiddleware} from './types';\n\nexport function auth({api, config}: ApiAndConfigParams): AuthMiddleware {\n  const usesTokenExchange = () =>\n    Boolean(config.future?.tokenExchange && api.config.isEmbeddedApp);\n\n  return {\n    begin(): RequestHandler {\n      return async (req: Request, res: Response) => {\n        if (usesTokenExchange()) {\n          config.logger.error(\n            'auth.begin() was called while token exchange is enabled. Embedded apps using token exchange do not use the Auth Code flow routes.',\n          );\n          res\n            .status(400)\n            .send(\n              'This app uses token exchange (tokenExchange). The OAuth auth routes are not used in this mode.',\n            );\n          return;\n        }\n\n        return redirectToAuth({req, res, api, config});\n      };\n    },\n    callback(): RequestHandler {\n      return async (req: Request, res: Response, next: NextFunction) => {\n        if (usesTokenExchange()) {\n          config.logger.error(\n            'auth.callback() was called while token exchange is enabled. Embedded apps using token exchange do not use the Auth Code flow routes.',\n          );\n          res\n            .status(400)\n            .send(\n              'This app uses token exchange (tokenExchange). The OAuth auth routes are not used in this mode.',\n            );\n          return;\n        }\n\n        config.logger.info('Handling request to complete OAuth process');\n\n        const oauthCompleted = await authCallback({\n          req,\n          res,\n          api,\n          config,\n        });\n\n        if (oauthCompleted) {\n          next();\n        }\n      };\n    },\n  };\n}\n"],"names":[],"mappings":";;;SAQgB,IAAI,CAAC,EAAC,GAAG,EAAE,MAAM,EAAqB,EAAA;AACpD,IAAA,MAAM,iBAAiB,GAAG,MACxB,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,aAAa,IAAI,GAAG,CAAC,MAAM,CAAC,aAAa,CAAC;IAEnE,OAAO;QACL,KAAK,GAAA;AACH,YAAA,OAAO,OAAO,GAAY,EAAE,GAAa,KAAI;gBAC3C,IAAI,iBAAiB,EAAE,EAAE;AACvB,oBAAA,MAAM,CAAC,MAAM,CAAC,KAAK,CACjB,mIAAmI,CACpI;oBACD;yBACG,MAAM,CAAC,GAAG;yBACV,IAAI,CACH,gGAAgG,CACjG;oBACH;gBACF;AAEA,gBAAA,OAAO,cAAc,CAAC,EAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAC,CAAC;AAChD,YAAA,CAAC;QACH,CAAC;QACD,QAAQ,GAAA;YACN,OAAO,OAAO,GAAY,EAAE,GAAa,EAAE,IAAkB,KAAI;gBAC/D,IAAI,iBAAiB,EAAE,EAAE;AACvB,oBAAA,MAAM,CAAC,MAAM,CAAC,KAAK,CACjB,sIAAsI,CACvI;oBACD;yBACG,MAAM,CAAC,GAAG;yBACV,IAAI,CACH,gGAAgG,CACjG;oBACH;gBACF;AAEA,gBAAA,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,4CAA4C,CAAC;AAEhE,gBAAA,MAAM,cAAc,GAAG,MAAM,YAAY,CAAC;oBACxC,GAAG;oBACH,GAAG;oBACH,GAAG;oBACH,MAAM;AACP,iBAAA,CAAC;gBAEF,IAAI,cAAc,EAAE;AAClB,oBAAA,IAAI,EAAE;gBACR;AACF,YAAA,CAAC;QACH,CAAC;KACF;AACH;;;;"}