var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
  get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
}) : x)(function(x) {
  if (typeof require !== "undefined")
    return require.apply(this, arguments);
  throw new Error('Dynamic require of "' + x + '" is not supported');
});
var __decorateClass = (decorators, target, key, kind) => {
  var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
  for (var i = decorators.length - 1, decorator; i >= 0; i--)
    if (decorator = decorators[i])
      result = (kind ? decorator(target, key, result) : decorator(result)) || result;
  if (kind && result)
    __defProp(target, key, result);
  return result;
};
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);

// src/strategies/passport/passport-keycloak/keycloak-strategy-factory-provider.ts
import { inject } from "@loopback/core";
import { HttpErrors } from "@loopback/rest";
import { HttpsProxyAgent } from "https-proxy-agent";

// src/strategies/keys.ts
import { BindingKey } from "@loopback/core";
var Strategies;
((Strategies2) => {
  let Passport;
  ((Passport2) => {
    Passport2.LOCAL_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.localPassword"
    );
    Passport2.LOCAL_PASSWORD_VERIFIER = BindingKey.create(
      "bleco.passport.verifier.localPassword"
    );
    Passport2.OTP_AUTH_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.otpAuth"
    );
    Passport2.OTP_VERIFIER = BindingKey.create("bleco.passport.verifier.otpAuth");
    Passport2.CLIENT_PASSWORD_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.clientPassword"
    );
    Passport2.OAUTH2_CLIENT_PASSWORD_VERIFIER = BindingKey.create(
      "bleco.passport.verifier.oauth2ClientPassword"
    );
    Passport2.BEARER_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.bearer"
    );
    Passport2.BEARER_TOKEN_VERIFIER = BindingKey.create(
      "bleco.passport.verifier.bearerToken"
    );
    Passport2.RESOURCE_OWNER_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.resourceOwnerPassword"
    );
    Passport2.RESOURCE_OWNER_PASSWORD_VERIFIER = BindingKey.create(
      "bleco.passport.verifier.resourceOwnerPassword"
    );
    Passport2.GOOGLE_OAUTH2_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.googleOauth2"
    );
    Passport2.GOOGLE_OAUTH2_VERIFIER = BindingKey.create(
      "bleco.passport.verifier.googleOauth2"
    );
    Passport2.AZURE_AD_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.azureAd"
    );
    Passport2.AZURE_AD_VERIFIER = BindingKey.create("bleco.passport.verifier.azureAd");
    Passport2.KEYCLOAK_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.keycloak"
    );
    Passport2.KEYCLOAK_VERIFIER = BindingKey.create(
      "bleco.passport.verifier.keycloak"
    );
    Passport2.INSTAGRAM_OAUTH2_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.instagramOauth2"
    );
    Passport2.INSTAGRAM_OAUTH2_VERIFIER = BindingKey.create(
      "bleco.passport.verifier.instagramOauth2"
    );
    Passport2.FACEBOOK_OAUTH2_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.facebookOauth2"
    );
    Passport2.FACEBOOK_OAUTH2_VERIFIER = BindingKey.create(
      "bleco.passport.verifier.facebookOauth2"
    );
    Passport2.APPLE_OAUTH2_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.appleOauth2"
    );
    Passport2.APPLE_OAUTH2_VERIFIER = BindingKey.create(
      "bleco.passport.verifier.appleOauth2"
    );
    Passport2.COGNITO_OAUTH2_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.cognitoOauth2"
    );
    Passport2.COGNITO_OAUTH2_VERIFIER = BindingKey.create(
      "bleco.passport.verifier.cognitoOauth2"
    );
    Passport2.SAML_STRATEGY_FACTORY = BindingKey.create(
      "bleco.passport.strategyFactory.saml"
    );
    Passport2.SAML_VERIFIER = BindingKey.create("bleco.passport.verifier.saml");
  })(Passport = Strategies2.Passport || (Strategies2.Passport = {}));
})(Strategies || (Strategies = {}));

// src/strategies/passport/passport-keycloak/keycloak-strategy-factory-provider.ts
var KeycloakStrategy = __require("@exlinc/keycloak-passport");
var KeycloakStrategyFactoryProvider = class {
  constructor(verifierKeycloak) {
    this.verifierKeycloak = verifierKeycloak;
  }
  value() {
    return (options, verifier) => this.getKeycloakAuthStrategyVerifier(options, verifier);
  }
  getKeycloakAuthStrategyVerifier(options, verifierPassed) {
    const verifyFn = verifierPassed ?? this.verifierKeycloak;
    const strategy = new KeycloakStrategy(
      options,
      async (accessToken, refreshToken, profile, cb) => {
        try {
          const user = await verifyFn(accessToken, refreshToken, profile, cb);
          if (!user) {
            throw new HttpErrors.Unauthorized("Invalid Credentials" /* InvalidCredentials */);
          }
          cb(void 0, user);
        } catch (err) {
          cb(err);
        }
      }
    );
    strategy.userProfile = (accessToken, done) => {
      this._userProfileFn(strategy, accessToken, done);
    };
    this._setupProxy(strategy);
    return strategy;
  }
  _userProfileFn(strategy, accessToken, done) {
    strategy._oauth2._useAuthorizationHeaderForGET = true;
    strategy._oauth2.get(
      strategy.options.userInfoURL,
      accessToken,
      (err, body) => {
        if (err) {
          return done(err);
        }
        try {
          const json = JSON.parse(body);
          const email = json.email;
          const userInfo = {
            keycloakId: json.sub,
            fullName: json.name,
            firstName: json.given_name,
            lastName: json.family_name,
            middleName: json.middle_name,
            username: json.preferred_username,
            email,
            avatar: json.avatar,
            realm: strategy.options.realm,
            // add all attributes to userInfo
            // overridden stuff
            ...json
          };
          for (const key of [
            "sub",
            "name",
            "given_name",
            "family_name",
            "middle_name",
            "preferred_username"
          ]) {
            delete userInfo[key];
          }
          done(null, userInfo);
        } catch (e) {
          done(e);
        }
      }
    );
  }
  _setupProxy(strategy) {
    let httpsProxyAgent;
    if (process.env["https_proxy"]) {
      httpsProxyAgent = new HttpsProxyAgent(process.env["https_proxy"]);
      strategy._oauth2.setAgent(httpsProxyAgent);
    } else if (process.env["HTTPS_PROXY"]) {
      httpsProxyAgent = new HttpsProxyAgent(process.env["HTTPS_PROXY"]);
      strategy._oauth2.setAgent(httpsProxyAgent);
    }
  }
};
KeycloakStrategyFactoryProvider = __decorateClass([
  __decorateParam(0, inject(Strategies.Passport.KEYCLOAK_VERIFIER))
], KeycloakStrategyFactoryProvider);

// src/strategies/passport/passport-keycloak/keycloak-verify.provider.ts
import { HttpErrors as HttpErrors2 } from "@loopback/rest";
var KeycloakVerifyProvider = class {
  constructor() {
  }
  value() {
    return async (accessToken, refreshToken, profile, cb) => {
      throw new HttpErrors2.NotImplemented(
        `VerifyFunction.KeycloakAuthFn is not implemented`
      );
    };
  }
};
export {
  KeycloakStrategy,
  KeycloakStrategyFactoryProvider,
  KeycloakVerifyProvider
};
//# sourceMappingURL=index.js.map