"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/strategy-adapter.ts
var strategy_adapter_exports = {};
__export(strategy_adapter_exports, {
  StrategyAdapter: () => StrategyAdapter
});
module.exports = __toCommonJS(strategy_adapter_exports);
var import_rest = require("@loopback/rest");
var passportRequestMixin = require("passport/lib/http/request");
var StrategyAdapter = class {
  /**
   * @param strategy instance of a class which implements a passport-strategy;
   * @description http://passportjs.org/
   */
  constructor(strategy) {
    this.strategy = strategy;
  }
  /**
   * The function to invoke the contained passport strategy.
   *     1. Create an instance of the strategy
   *     2. add success and failure state handlers
   *     3. authenticate using the strategy
   * @param request The incoming request.
   */
  authenticate(request, response, options) {
    return new Promise((resolve, reject) => {
      for (const key in passportRequestMixin) {
        request[key] = passportRequestMixin[key];
      }
      const strategy = Object.create(this.strategy);
      strategy.success = (t) => {
        resolve(t);
      };
      strategy.fail = (challenge) => {
        reject(new import_rest.HttpErrors.Unauthorized(challenge));
      };
      strategy.error = (error) => {
        reject(new import_rest.HttpErrors.Unauthorized(error));
      };
      strategy.redirect = (url) => {
        if (response) {
          response.redirect(302, url);
        }
        resolve();
      };
      strategy.authenticate(request, options ?? {});
    });
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  StrategyAdapter
});
//# sourceMappingURL=strategy-adapter.cjs.map