UNPKG

ravel

Version:

Ravel Rapid Application Development Framework

113 lines (102 loc) 4.54 kB
'use strict';function _asyncToGenerator(fn) {return function () {var gen = fn.apply(this, arguments);return new Promise(function (resolve, reject) {function step(key, arg) {try {var info = gen[key](arg);var value = info.value;} catch (error) {reject(error);return;}if (info.done) {resolve(value);} else {return Promise.resolve(value).then(function (value) {step("next", value);}, function (err) {step("throw", err);});}}return step("next");});};} const passport = require('koa-passport'); const ApplicationError = require('../util/application_error'); const Metadata = require('../util/meta'); const symbols = require('./symbols'); const coreSymbols = require('../core/symbols'); /** * Encapsulates the initialization of passport.js. * * @param {Ravel} ravelInstance - A reference to an instance of a Ravel app. * @param {Object} router - A koa router. * @private */ module.exports = function (ravelInstance, router) { /** * Retrieve the first registered Module which is decorated with `@authconfig`. * * @returns {Object} The first `@authconfig` `Module`. * @private */ function getAuthMod() { let authMod = null; for (const m of Object.keys(ravelInstance[coreSymbols.modules])) { const mod = ravelInstance[coreSymbols.modules][m]; if (Metadata.getClassMetaValue(Object.getPrototypeOf(mod), '@authconfig', 'enabled', false)) { authMod = mod; break; } else { continue; } } if (authMod === null) { throw new ApplicationError.NotFound('Module annotated with @authconfig is required and was not found.'); } return authMod; } /** * Using the 'post config koa' hook, initalize passport using a reference to the internal koa app * @private */ ravelInstance.once('post config koa', function (app) { const providers = ravelInstance.authenticationProviders(); if (providers.length > 0) { app.use(passport.initialize()); app.use(passport.session()); app.use((() => {var _ref = _asyncToGenerator(function* (ctx, next) { // overwrite ctx.passport with deprecation message Object.defineProperty(ctx, 'passport', { configurable: true, get: function () { ravelInstance.log.warn('ctx.passport is deprecated. Please use ctx.state instead.'); return ctx.state; } }); yield next(); });return function (_x, _x2) {return _ref.apply(this, arguments);};})()); } }); /** * @private */ ravelInstance.once('post module init', function () { const providers = ravelInstance.authenticationProviders(); if (providers.length > 0) { ravelInstance[symbols.authConfigModule] = getAuthMod(); passport.serializeUser(function (user, done) { ravelInstance[symbols.authConfigModule].serializeUser(user). then(id => { done(null, id); }).catch(err => { done(err, null); }); }); passport.deserializeUser(function (userId, done) { ravelInstance[symbols.authConfigModule].deserializeUser(userId). then(user => { done(null, user); }).catch(err => { done(err, null); }); }); const verify = function (providerName) { return function (...verifyArgs) { const done = verifyArgs[verifyArgs.length - 1]; const args = verifyArgs.slice(0, verifyArgs.length - 1); // add provider name to the argument list for verify(), // so that @authconfig module can make different decisions // for different providers. args.unshift(providerName); ravelInstance[symbols.authConfigModule].verify(...args). then(user => { done(null, user); }).catch(err => { done(err, null); }); }; }; for (const p of providers) { p.init(router, passport, verify(p.name)); } } }); };