Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
d55a315
commit 2813fed
Showing
4 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const jwt = require('jsonwebtoken'); | ||
require('dotenv').config(); | ||
const chalk = require('chalk'); | ||
|
||
const defaultExpiry = '30 days'; | ||
|
||
const parsePayload = (payloadArray = []) => { | ||
const result = {}; | ||
|
||
payloadArray.forEach((entry = '') => { | ||
const [key, value] = entry.split('='); | ||
if (key && value) result[key] = value; | ||
}); | ||
|
||
return result; | ||
}; | ||
|
||
const token = async (options = {}) => { | ||
const { expiry = defaultExpiry, secret = process.env.CUBEJS_API_SECRET } = options; | ||
if (!secret) throw new Error('No app secret found').message; | ||
|
||
const extraOptions = {}; | ||
if (expiry !== "0") extraOptions.expiresIn = expiry; | ||
|
||
const payload = parsePayload(options.payload); | ||
|
||
console.log("Generating Cube.js JWT token"); | ||
console.log("Expires in: ", chalk.green(expiry)); | ||
console.log("Payload: ", chalk.green(JSON.stringify(payload))); | ||
console.log(""); | ||
|
||
const signedToken = jwt.sign(payload, secret, extraOptions); | ||
console.log(signedToken); | ||
return signedToken; | ||
}; | ||
|
||
const collect = (val, memo) => [val, ...memo]; | ||
|
||
exports.token = token; | ||
exports.defaultExpiry = defaultExpiry; | ||
exports.collect = collect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters