Skip to content

Commit 2813fed

Browse files
Timur Minulinpaveltiunov
authored andcommitted
feat(cubejs-cli): add token generation (#67)
* feat(cubejs-cli): add token generation * feat(cubejs-cli): add example generation * fix(cubejs-cli): fix zero expiry
1 parent d55a315 commit 2813fed

File tree

4 files changed

+147
-1
lines changed

4 files changed

+147
-1
lines changed

packages/cubejs-cli/cubejsCli.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const client = new Analytics('dSR8JiNYIGKyQHKid9OaLYugXLao18hA', { flushInterval
1717
const { machineIdSync } = require('node-machine-id');
1818
const { promisify } = require('util');
1919
const templates = require('./templates');
20+
const { token, defaultExpiry, collect } = require('./token');
2021

2122
const packageJson = require('./package.json');
2223

@@ -56,7 +57,7 @@ const writePackageJson = async (json) => fs.writeJson('package.json', json, {
5657
EOL: os.EOL
5758
});
5859

59-
const displayError = async (text, options) => {
60+
const displayError = async (text, options = {}) => {
6061
console.error('');
6162
console.error(chalk.cyan('Cube.js Error ---------------------------------------'));
6263
console.error('');
@@ -275,6 +276,23 @@ program
275276
console.log(' $ cubejs generate -t orders,customers');
276277
});
277278

279+
program
280+
.command('token')
281+
.option('-e, --expiry [expiry]', 'Token expiry. Set to 0 for no expiry', defaultExpiry)
282+
.option('-s, --secret [secret]', 'Cube.js app secret. Also can be set via environment variable CUBEJS_API_SECRET')
283+
.option('-p, --payload [values]', 'Payload. Example: -p foo=bar', collect, [])
284+
.description('Create JWT token')
285+
.action(
286+
(options) => token(options)
287+
.catch(e => displayError(e.stack || e))
288+
)
289+
.on('--help', () => {
290+
console.log('');
291+
console.log('Examples:');
292+
console.log('');
293+
console.log(' $ cubejs token -e "1 day" -p foo=bar -p cool=true');
294+
});
295+
278296
if (!process.argv.slice(2).length) {
279297
program.help();
280298
}

packages/cubejs-cli/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
"chalk": "^2.4.2",
2727
"commander": "^2.19.0",
2828
"cross-spawn": "^6.0.5",
29+
"dotenv": "^7.0.0",
2930
"fs-extra": "^7.0.1",
31+
"jsonwebtoken": "^8.5.1",
3032
"node-machine-id": "^1.1.10"
3133
},
3234
"devDependencies": {

packages/cubejs-cli/token.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const jwt = require('jsonwebtoken');
2+
require('dotenv').config();
3+
const chalk = require('chalk');
4+
5+
const defaultExpiry = '30 days';
6+
7+
const parsePayload = (payloadArray = []) => {
8+
const result = {};
9+
10+
payloadArray.forEach((entry = '') => {
11+
const [key, value] = entry.split('=');
12+
if (key && value) result[key] = value;
13+
});
14+
15+
return result;
16+
};
17+
18+
const token = async (options = {}) => {
19+
const { expiry = defaultExpiry, secret = process.env.CUBEJS_API_SECRET } = options;
20+
if (!secret) throw new Error('No app secret found').message;
21+
22+
const extraOptions = {};
23+
if (expiry !== "0") extraOptions.expiresIn = expiry;
24+
25+
const payload = parsePayload(options.payload);
26+
27+
console.log("Generating Cube.js JWT token");
28+
console.log("Expires in: ", chalk.green(expiry));
29+
console.log("Payload: ", chalk.green(JSON.stringify(payload)));
30+
console.log("");
31+
32+
const signedToken = jwt.sign(payload, secret, extraOptions);
33+
console.log(signedToken);
34+
return signedToken;
35+
};
36+
37+
const collect = (val, memo) => [val, ...memo];
38+
39+
exports.token = token;
40+
exports.defaultExpiry = defaultExpiry;
41+
exports.collect = collect;

packages/cubejs-cli/yarn.lock

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,11 @@ bser@^2.0.0:
515515
dependencies:
516516
node-int64 "^0.4.0"
517517

518+
buffer-equal-constant-time@1.0.1:
519+
version "1.0.1"
520+
resolved "https://registry.yarnpkg.com/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz#f8e71132f7ffe6e01a5c9697a4c6f3e48d5cc819"
521+
integrity sha1-+OcRMvf/5uAaXJaXpMbz5I1cyBk=
522+
518523
buffer-from@^1.0.0:
519524
version "1.1.1"
520525
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
@@ -953,6 +958,11 @@ domexception@^1.0.1:
953958
dependencies:
954959
webidl-conversions "^4.0.2"
955960

961+
dotenv@^7.0.0:
962+
version "7.0.0"
963+
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-7.0.0.tgz#a2be3cd52736673206e8a85fb5210eea29628e7c"
964+
integrity sha512-M3NhsLbV1i6HuGzBUH8vXrtxOk+tWmzWKDMbAVSUp3Zsjm7ywFeuwrUXhmhQyRK1q5B5GGy7hcXPbj3bnfZg2g==
965+
956966
ecc-jsbn@~0.1.1:
957967
version "0.1.2"
958968
resolved "https://registry.yarnpkg.com/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz#3a83a904e54353287874c564b7549386849a98c9"
@@ -961,6 +971,13 @@ ecc-jsbn@~0.1.1:
961971
jsbn "~0.1.0"
962972
safer-buffer "^2.1.0"
963973

974+
ecdsa-sig-formatter@1.0.11:
975+
version "1.0.11"
976+
resolved "https://registry.yarnpkg.com/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz#ae0f0fa2d85045ef14a817daa3ce9acd0489e5bf"
977+
integrity sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==
978+
dependencies:
979+
safe-buffer "^5.0.1"
980+
964981
emoji-regex@^7.0.1:
965982
version "7.0.3"
966983
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-7.0.3.tgz#933a04052860c85e83c122479c4748a8e4c72156"
@@ -2598,6 +2615,22 @@ jsonfile@^4.0.0:
25982615
optionalDependencies:
25992616
graceful-fs "^4.1.6"
26002617

2618+
jsonwebtoken@^8.5.1:
2619+
version "8.5.1"
2620+
resolved "https://registry.yarnpkg.com/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz#00e71e0b8df54c2121a1f26137df2280673bcc0d"
2621+
integrity sha512-XjwVfRS6jTMsqYs0EsuJ4LGxXV14zQybNd4L2r0UvbVnSF9Af8x7p5MzbJ90Ioz/9TI41/hTCvznF/loiSzn8w==
2622+
dependencies:
2623+
jws "^3.2.2"
2624+
lodash.includes "^4.3.0"
2625+
lodash.isboolean "^3.0.3"
2626+
lodash.isinteger "^4.0.4"
2627+
lodash.isnumber "^3.0.3"
2628+
lodash.isplainobject "^4.0.6"
2629+
lodash.isstring "^4.0.1"
2630+
lodash.once "^4.0.0"
2631+
ms "^2.1.1"
2632+
semver "^5.6.0"
2633+
26012634
jsprim@^1.2.2:
26022635
version "1.4.1"
26032636
resolved "https://registry.yarnpkg.com/jsprim/-/jsprim-1.4.1.tgz#313e66bc1e5cc06e438bc1b7499c2e5c56acb6a2"
@@ -2608,6 +2641,23 @@ jsprim@^1.2.2:
26082641
json-schema "0.2.3"
26092642
verror "1.10.0"
26102643

2644+
jwa@^1.4.1:
2645+
version "1.4.1"
2646+
resolved "https://registry.yarnpkg.com/jwa/-/jwa-1.4.1.tgz#743c32985cb9e98655530d53641b66c8645b039a"
2647+
integrity sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==
2648+
dependencies:
2649+
buffer-equal-constant-time "1.0.1"
2650+
ecdsa-sig-formatter "1.0.11"
2651+
safe-buffer "^5.0.1"
2652+
2653+
jws@^3.2.2:
2654+
version "3.2.2"
2655+
resolved "https://registry.yarnpkg.com/jws/-/jws-3.2.2.tgz#001099f3639468c9414000e99995fa52fb478304"
2656+
integrity sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==
2657+
dependencies:
2658+
jwa "^1.4.1"
2659+
safe-buffer "^5.0.1"
2660+
26112661
kind-of@^3.0.2, kind-of@^3.0.3, kind-of@^3.2.0:
26122662
version "3.2.2"
26132663
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64"
@@ -2704,11 +2754,41 @@ lodash.get@^4.4.2:
27042754
resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"
27052755
integrity sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk=
27062756

2757+
lodash.includes@^4.3.0:
2758+
version "4.3.0"
2759+
resolved "https://registry.yarnpkg.com/lodash.includes/-/lodash.includes-4.3.0.tgz#60bb98a87cb923c68ca1e51325483314849f553f"
2760+
integrity sha1-YLuYqHy5I8aMoeUTJUgzFISfVT8=
2761+
2762+
lodash.isboolean@^3.0.3:
2763+
version "3.0.3"
2764+
resolved "https://registry.yarnpkg.com/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz#6c2e171db2a257cd96802fd43b01b20d5f5870f6"
2765+
integrity sha1-bC4XHbKiV82WgC/UOwGyDV9YcPY=
2766+
2767+
lodash.isinteger@^4.0.4:
2768+
version "4.0.4"
2769+
resolved "https://registry.yarnpkg.com/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz#619c0af3d03f8b04c31f5882840b77b11cd68343"
2770+
integrity sha1-YZwK89A/iwTDH1iChAt3sRzWg0M=
2771+
2772+
lodash.isnumber@^3.0.3:
2773+
version "3.0.3"
2774+
resolved "https://registry.yarnpkg.com/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz#3ce76810c5928d03352301ac287317f11c0b1ffc"
2775+
integrity sha1-POdoEMWSjQM1IwGsKHMX8RwLH/w=
2776+
2777+
lodash.isplainobject@^4.0.6:
2778+
version "4.0.6"
2779+
resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb"
2780+
integrity sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs=
2781+
27072782
lodash.isstring@^4.0.1:
27082783
version "4.0.1"
27092784
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"
27102785
integrity sha1-1SfftUVuynzJu5XV2ur4i6VKVFE=
27112786

2787+
lodash.once@^4.0.0:
2788+
version "4.1.1"
2789+
resolved "https://registry.yarnpkg.com/lodash.once/-/lodash.once-4.1.1.tgz#0dd3971213c7c56df880977d504c88fb471a97ac"
2790+
integrity sha1-DdOXEhPHxW34gJd9UEyI+0cal6w=
2791+
27122792
lodash.sortby@^4.7.0:
27132793
version "4.7.0"
27142794
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
@@ -3785,6 +3865,11 @@ semver@5.3.0:
37853865
resolved "https://registry.yarnpkg.com/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f"
37863866
integrity sha1-myzl094C0XxgEq0yaqa00M9U+U8=
37873867

3868+
semver@^5.6.0:
3869+
version "5.7.0"
3870+
resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.0.tgz#790a7cf6fea5459bac96110b29b60412dc8ff96b"
3871+
integrity sha512-Ya52jSX2u7QKghxeoFGpLwCtGlt7j0oY9DYb5apt9nPlJ42ID+ulTXESnt/qAQcoSERyZ5sl3LDIOw0nAn/5DA==
3872+
37883873
set-blocking@^2.0.0, set-blocking@~2.0.0:
37893874
version "2.0.0"
37903875
resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7"

0 commit comments

Comments
 (0)