@restmatic/jwt-session
Version:
Provides Authentication to the RestMatic platform.
108 lines (106 loc) • 4.54 kB
JavaScript
;
/**
* @file StrategyLoader
* @author Jim Bulkowski <jim.b@paperelectron.com>
* @project Authentication
* @license MIT {@link http://opensource.org/licenses/MIT}
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @file index
* @author Jim Bulkowski <jim.b@paperelectron.com>
* @project Authentication
* @license MIT {@link http://opensource.org/licenses/MIT}
*/
const fp_1 = require("lodash/fp");
const plugin_tools_1 = require("@pomegranate/plugin-tools");
const tsGenerator = `// Generated by the Pomegranate cli on {{creationDate}}
// name: {{name}}
export const PassportStrategy = (Controllers) => {
// Return a Passport Strategy
}
`;
exports.StrategyLoaderPlugin = plugin_tools_1.CreatePlugin('action')
.configuration({
name: 'Strategies',
depends: ['@restmatic/AuthenticationCore'],
optional: ['@restmatic/Controllers']
})
.directories([{ prop: 'main', path: '.' }])
.hooks({
load: (Injector, PluginLogger, PluginFiles, Authentication) => __awaiter(this, void 0, void 0, function* () {
let strategies = yield PluginFiles('main').fileList({ ext: '.js' });
let loadedCount = 0;
fp_1.each((file) => {
let required = require(file.path);
let fileName = file.getBaseName();
let mw = fp_1.get('PassportStrategy', required);
if (!mw) {
throw new Error(`Strategy file ${fileName} does not contain an export on the PassportStrategy property.`);
}
if (!fp_1.isFunction(mw)) {
throw new Error(`Strategy file ${fileName} does not export an injectable function on the PassportStrategy property.`);
}
let injectedStrategy = Injector.inject(mw);
PluginLogger.log(`Adding ${injectedStrategy.name} authentication strategy.`);
loadedCount += 1;
Authentication.use(injectedStrategy);
}, strategies);
PluginLogger.log(`${loadedCount} strategies added.`);
return null;
})
})
.commands(function (PomConfig, PluginFiles, Handlebars) {
return (yargs) => {
return yargs
.usage('usage: $0')
.command({
command: 'generate <name>',
aliases: 'g',
describe: `Generates Strategy file <name>`,
builder: (yargs) => {
return yargs
.positional('name', {
describe: 'The the filename to be created.',
default: 'index',
type: 'string'
})
.option('l', {
alias: 'language',
describe: 'Generate TypeScript or Javascript',
default: 'ts',
choices: ['ts'],
type: 'string'
})
.option('force', {
alias: 'f',
default: false,
describe: 'overwrites the specified file if it exists.',
type: 'boolean'
});
},
handler: (argv) => __awaiter(this, void 0, void 0, function* () {
let Pf = PluginFiles('main');
let file = `${argv.name}.${argv.language}`;
let exists = yield Pf.projectFileExists(file);
let compile = Handlebars.compile(tsGenerator);
let compiled = compile({ creationDate: new Date().toDateString(), name: argv.name });
if (exists && !argv.force) {
throw new Error(`${file} \n exists \n Rerun with --force to overwrite.`);
}
yield Pf.outputProjectFile(file, compiled);
console.log(`Created @restmatic/authentication Strategy file ${file}`);
})
})
.help();
};
});
//# sourceMappingURL=StrategyLoader.js.map