loopback4-authentication
Version:
A loopback-next extension for authentication feature. Various Oauth strategies supported by this package.
62 lines • 2.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StrategyAdapter = void 0;
const rest_1 = require("@loopback/rest");
const passportRequestMixin = require('passport/lib/http/request');
/**
* Adapter class to invoke passport-strategy
* 1. provides express dependencies to the passport strategies
* 2. provides shimming of requests for passport authentication
* 3. provides lifecycle similar to express to the passport-strategy
* 3. provides state methods to the strategy instance
* see: https://github.com/jaredhanson/passport
*/
class StrategyAdapter {
/**
* @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) => {
// mix-in passport additions like req.logIn and req.logOut
for (const key in passportRequestMixin) {
request[key] = passportRequestMixin[key];
}
// create a prototype chain of an instance of a passport strategy
const strategy = Object.create(this.strategy);
// add success state handler to strategy instance
strategy.success = (t) => {
resolve(t);
};
// add failure state handler to strategy instance
strategy.fail = (challenge) => {
reject(new rest_1.HttpErrors.Unauthorized(challenge));
};
// add error state handler to strategy instance
strategy.error = (error) => {
reject(new rest_1.HttpErrors.Unauthorized(error));
};
const REDIRECT_URL = 302;
strategy.redirect = (url) => {
if (response) {
response.redirect(REDIRECT_URL, url);
}
resolve();
};
// authenticate
strategy.authenticate(request, options !== null && options !== void 0 ? options : {});
});
}
}
exports.StrategyAdapter = StrategyAdapter;
//# sourceMappingURL=strategy-adapter.js.map