Skip to content
This repository has been archived by the owner on May 11, 2020. It is now read-only.

Commit

Permalink
Manage requiresAuthentication on cli conf
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisjanvier committed Nov 11, 2019
1 parent b17ecb5 commit c50f424
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 31 deletions.
26 changes: 15 additions & 11 deletions cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import rc from 'rc';
import signale from 'signale';
import boxen from 'boxen';
import fs from 'fs';
import omit from 'lodash.omit';

import { questions } from './questions.js';
import config from '../src/config.js';
import config, { getMissingEnvironmentTokens, getMissingTokensMessage } from '../src/config.js';
import { app } from '../src/app.js';

const clearWn = () => {
Expand Down Expand Up @@ -47,12 +48,7 @@ if (!globalConfiguration.config) {
signale.log(`You already have ${apis.length} api.s configured`);
}
const apiConfiguration = await questions.askApiConfiguration();
apis.push({
name: apiConfiguration.name,
url: apiConfiguration.url,
tokenKey: apiConfiguration.tokenKey || 'authorization',
tokenPrefix: apiConfiguration.tokenPrefix || 'Bearer',
});
apis.push(omit(apiConfiguration, ['continue']));
configureApi = apiConfiguration.continue;
}

Expand Down Expand Up @@ -80,13 +76,21 @@ if (!globalConfiguration.config) {

run();
} else {
app.listen(config.proxyPort, () => {
signale.info(`Web Myna is starded on port ${config.proxyPort} in environment ${config.env}`);
});
const missingTokens = getMissingEnvironmentTokens();

if (missingTokens.length) {
const message = getMissingTokensMessage(config, missingTokens);
signale.log(boxen(message, { padding: 1, margin: 1, borderColor: 'red', align: 'center' }));
process.exit();
} else {
app.listen(config.proxyPort, () => {
signale.info(`Web Myna is starded on port ${config.proxyPort} in environment ${config.env}`);
});
}
}

process.on('SIGINT', function() {
clear();
signale.log(chalk.yellow(figlet.textSync('Bye', { horizontalLayout: 'full' })));
process.exit(1);
process.exit();
});
6 changes: 3 additions & 3 deletions cli/questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,22 @@ export const questions = {
},
{
type: 'confirm',
name: 'needToken',
name: 'requiresAuthentication',
message: 'Does the API need an authentication token?',
},
{
type: 'input',
name: 'tokenKey',
message: 'What is the name of the http header with the authentication token?',
default: 'authorization',
when: value => value.needToken,
when: value => value.requiresAuthentication,
},
{
type: 'input',
name: 'tokenPrefix',
message: 'Should the token be prefixed in the header?',
default: 'Bearer',
when: value => value.needToken,
when: value => value.requiresAuthentication,
},
{
type: 'confirm',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"figlet": "1.2.4",
"http-proxy-middleware": "0.20.0",
"inquirer": "7.0.0",
"lodash.omit": "4.5.0",
"md5": "2.2.1",
"minimist": "1.2.0",
"nodemon": "1.19.4",
Expand Down
26 changes: 26 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ export const getMissingEnvironmentTokens = (apis = globalConfiguration.apis, env
return missingTokens;
};

/**
* Return readable message about missing tokens in process.env
*
* @function
* @param {object} config with apis
* @param {string[]} missingTokens an array of missing token names
* @returns {string} the message
*/
export const getMissingTokensMessage = (config, missingTokens) => {
const missingApisWithAuthentication = config.apis
.filter(api => missingTokens.includes(getApiTokenName(api)))
.map(api => api.name);
const isPlural = missingApisWithAuthentication.length > 1;
return `
You have declared ${missingApisWithAuthentication.length} api${
isPlural ? 's' : ''
} as requiring an authentication token: ${missingApisWithAuthentication.join(', ')}.
You must therefore declare the following token${isPlural ? 's' : ''} as an environment variable: ${missingTokens
.map(
token => `
=> ${token}`,
)
.join('')}`;
};

convict.addFormat({
name: 'apis',
validate: (apis, schema) => {
Expand Down
18 changes: 2 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,12 @@ import signale from 'signale';
import boxen from 'boxen';

import * as appJs from './app.js';
import config, { getApiTokenName, getMissingEnvironmentTokens } from './config.js';
import config, { getMissingEnvironmentTokens, getMissingTokensMessage } from './config.js';

const missingTokens = getMissingEnvironmentTokens();

if (missingTokens.length) {
const missingApisWithAuthentication = config.apis
.filter(api => missingTokens.includes(getApiTokenName(api)))
.map(api => api.name);
const isPlural = missingApisWithAuthentication.length > 1;
const message = `
You have declared ${missingApisWithAuthentication.length} api${
isPlural ? 's' : ''
} as requiring an authentication token: ${missingApisWithAuthentication.join(', ')}.
You must therefore declare the following token${isPlural ? 's' : ''} as an environment variable: ${missingTokens
.map(
token => `
=> ${token}`,
)
.join('')}`;
const message = getMissingTokensMessage(config, missingTokens);
signale.log(boxen(message, { padding: 1, margin: 1, borderColor: 'red', align: 'center' }));
} else {
appJs.app.listen(config.proxyPort, () => {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5751,7 +5751,7 @@ lodash.memoize@^4.1.2:
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
integrity sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=

lodash.omit@^4.5.0:
lodash.omit@4.5.0, lodash.omit@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.omit/-/lodash.omit-4.5.0.tgz#6eb19ae5a1ee1dd9df0b969e66ce0b7fa30b5e60"
integrity sha1-brGa5aHuHdnfC5aeZs4Lf6MLXmA=
Expand Down

0 comments on commit c50f424

Please sign in to comment.