"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    getPassportJwtOptions: function() {
        return getPassportJwtOptions;
    },
    initializePassportStrategies: function() {
        return initializePassportStrategies;
    },
    verifyUserCallback: function() {
        return verifyUserCallback;
    }
});
const _passportjwt = require("passport-jwt");
const _passportanonymous = require("passport-anonymous");
const _containertokens = require("../container.tokens");
const _serverconstants = require("../server.constants");
const _runtimeexceptions = require("../exceptions/runtime.exceptions");
const _authorizationconstants = require("../constants/authorization.constants");
function getPassportJwtOptions(settingsStore, configService, jwtFromRequest = _passportjwt.ExtractJwt.fromAuthHeaderAsBearerToken()) {
    return {
        jwtFromRequest: jwtFromRequest,
        secretOrKeyProvider: async (req, token, done)=>{
            const { jwtSecret } = await settingsStore.getCredentialSettings();
            return done(null, jwtSecret);
        },
        audience: configService.get(_serverconstants.AppConstants.OVERRIDE_JWT_AUDIENCE, _serverconstants.AppConstants.DEFAULT_JWT_AUDIENCE),
        issuer: configService.get(_serverconstants.AppConstants.OVERRIDE_JWT_ISSUER, _serverconstants.AppConstants.DEFAULT_JWT_ISSUER)
    };
}
function verifyUserCallback(userService) {
    return function(jwt_payload, done) {
        userService.getUser(jwt_payload.userId).then((user)=>{
            if (user && user.isVerified && !user.needsPasswordChange) {
                return done(null, user);
            }
            if (user && user.needsPasswordChange) {
                console.log("Needs password change");
                return done(new _runtimeexceptions.AuthenticationError("Password change required", _authorizationconstants.AUTH_ERROR_REASON.PasswordChangeRequired), false);
            }
            if (user && !user?.isVerified) {
                console.log("Needs password change");
                return done(new _runtimeexceptions.AuthenticationError("User not verified", _authorizationconstants.AUTH_ERROR_REASON.AccountNotVerified), false);
            }
            return done(null, false);
        }).catch((err)=>{
            if (err) {
                return done(err, false);
            }
        });
    };
}
function initializePassportStrategies(passport, container) {
    const settingsStore = container.resolve(_containertokens.DITokens.settingsStore);
    const configService = container.resolve(_containertokens.DITokens.configService);
    const userService = container.resolve(_containertokens.DITokens.userService);
    const opts = getPassportJwtOptions(settingsStore, configService, _passportjwt.ExtractJwt.fromAuthHeaderAsBearerToken());
    passport.use(new _passportjwt.Strategy(opts, function(jwt_payload, done) {
        verifyUserCallback(userService)(jwt_payload, done);
    }));
    passport.use(new _passportanonymous.Strategy());
    return passport;
}

//# sourceMappingURL=passport.js.map