loopback4-authentication
Version:
A loopback-next extension for authentication feature. Various Oauth strategies supported by this package.
76 lines • 3.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.InstagramAuthStrategyFactoryProvider = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@loopback/core");
const rest_1 = require("@loopback/rest");
const https_proxy_agent_1 = require("https-proxy-agent");
const passport_instagram_1 = require("passport-instagram");
const keys_1 = require("../../keys");
let InstagramAuthStrategyFactoryProvider = class InstagramAuthStrategyFactoryProvider {
constructor(verifierInstagramAuth) {
this.verifierInstagramAuth = verifierInstagramAuth;
}
value() {
return (options, verifier) => this.getInstagramAuthStrategyVerifier(options, verifier);
}
getInstagramAuthStrategyVerifier(options, verifierPassed) {
const verifyFn = verifierPassed !== null && verifierPassed !== void 0 ? verifierPassed : this.verifierInstagramAuth;
let strategy;
if ((options === null || options === void 0 ? void 0 : options.passReqToCallback) === true) {
strategy = new passport_instagram_1.Strategy(options,
// eslint-disable-next-line @typescript-eslint/no-misused-promises
async (req, accessToken, refreshToken, profile, cb) => {
try {
const user = await verifyFn(accessToken, refreshToken, profile, cb, req);
if (!user) {
throw new rest_1.HttpErrors.Unauthorized("Invalid Credentials" /* AuthErrorKeys.InvalidCredentials */);
}
cb(undefined, user);
}
catch (err) {
cb(err);
}
});
}
else {
strategy = new passport_instagram_1.Strategy(options,
// eslint-disable-next-line @typescript-eslint/no-misused-promises
async (accessToken, refreshToken, profile, cb) => {
try {
const user = await verifyFn(accessToken, refreshToken, profile, cb);
if (!user) {
throw new rest_1.HttpErrors.Unauthorized("Invalid Credentials" /* AuthErrorKeys.InvalidCredentials */);
}
cb(undefined, user);
}
catch (err) {
cb(err);
}
});
}
this._setupProxy(strategy);
return strategy;
}
_setupProxy(strategy) {
// Setup proxy if any
let httpsProxyAgent;
if (process.env['https_proxy']) {
httpsProxyAgent = new https_proxy_agent_1.HttpsProxyAgent(process.env['https_proxy']);
strategy._oauth2.setAgent(httpsProxyAgent);
}
else if (process.env['HTTPS_PROXY']) {
httpsProxyAgent = new https_proxy_agent_1.HttpsProxyAgent(process.env['HTTPS_PROXY']);
strategy._oauth2.setAgent(httpsProxyAgent);
}
else {
//this is intentional
}
}
};
exports.InstagramAuthStrategyFactoryProvider = InstagramAuthStrategyFactoryProvider;
exports.InstagramAuthStrategyFactoryProvider = InstagramAuthStrategyFactoryProvider = tslib_1.__decorate([
tslib_1.__param(0, (0, core_1.inject)(keys_1.Strategies.Passport.INSTAGRAM_OAUTH2_VERIFIER)),
tslib_1.__metadata("design:paramtypes", [Function])
], InstagramAuthStrategyFactoryProvider);
//# sourceMappingURL=insta-auth-strategy-factory-provider.js.map