// src/strategies/passport/passport-resource-owner-password/oauth2-resource-owner-password-grant.ts
import * as passport from "passport";
var Oauth2ResourceOwnerPassword;
((Oauth2ResourceOwnerPassword2) => {
  class Strategy2 extends passport.Strategy {
    constructor(options, verify) {
      super();
      if (verify) {
        this.passReqToCallback = options.passReqToCallback;
        this.verify = verify;
      } else {
        this.passReqToCallback = false;
        this.verify = options;
      }
      this.name = "oauth2-resource-owner-password";
    }
    authenticate(req, options) {
      var _a, _b, _c;
      if (
        /* eslint-disable @typescript-eslint/prefer-optional-chain */
        !req.body || !((_a = req.body) == null ? void 0 : _a["client_id"]) || !((_b = req.body) == null ? void 0 : _b["username"]) || !((_c = req.body) == null ? void 0 : _c["password"])
      ) {
        this.fail();
        return;
      }
      const clientId = req.body["client_id"];
      const clientSecret = req.body["client_secret"];
      const username = req.body["username"];
      const password = req.body["password"];
      const verified = (err, client, user) => {
        if (err) {
          this.error(err);
          return;
        }
        if (!client) {
          this.fail();
          return;
        }
        if (!user) {
          this.fail();
          return;
        }
        this.success(user);
      };
      if (this.passReqToCallback) {
        this.verify(
          req,
          clientId,
          clientSecret,
          username,
          password,
          verified
        );
      } else {
        this.verify(
          clientId,
          clientSecret,
          username,
          password,
          verified
        );
      }
    }
  }
  Oauth2ResourceOwnerPassword2.Strategy = Strategy2;
})(Oauth2ResourceOwnerPassword || (Oauth2ResourceOwnerPassword = {}));
export {
  Oauth2ResourceOwnerPassword
};
//# sourceMappingURL=oauth2-resource-owner-password-grant.js.map