Skip to content

Commit

Permalink
Add JSON field transformation (#163)
Browse files Browse the repository at this point in the history
This PR allows to use JSONPath transformation on fields.

JSONPath system (quite similar to jq) is explained here:
https://github.com/JSONPath-Plus/JSONPath#syntax-through-examples
  • Loading branch information
Mikescops authored Aug 10, 2023
1 parent 830749a commit 7f7e3bb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"got": "^11.8.6",
"inquirer": "^8.2.5",
"inquirer-search-list": "^1.2.6",
"jsonpath-plus": "^7.2.0",
"otplib": "^12.0.1",
"winston": "^3.10.0",
"xml-js": "^1.6.11",
Expand Down
24 changes: 10 additions & 14 deletions src/utils/secretPath.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isUuid } from './strings';
import { transformOtp } from './secretTransformation';
import { transformJsonPath, transformOtp } from './secretTransformation';
import { ParsedPath } from '../types';
import { InvalidDashlanePathError } from '../errors';

Expand All @@ -17,12 +17,10 @@ export const parsePath = (path: string): ParsedPath => {
throw new InvalidDashlanePathError();
}

const queryParams = path.split('?');
if (queryParams.length > 2) {
throw new InvalidDashlanePathError();
}
const [, ...queryParamsSplitted] = path.split('?');
const queryParams = queryParamsSplitted.join('?');

const cleanPath = path.slice(5, path.length - (queryParams.length === 2 ? queryParams[1].length + 1 : 0));
const cleanPath = path.slice(5, path.length - (queryParams.length > 0 ? queryParams.length + 1 : 0));

const pathChunks = cleanPath.split('/');

Expand All @@ -46,19 +44,17 @@ export const parsePath = (path: string): ParsedPath => {

let transformation = undefined;

if (queryParams.length === 2) {
const queryParamChunks = queryParams[1].split('=');
if (queryParamChunks.length > 2) {
throw new InvalidDashlanePathError();
}

const queryParamKey = queryParamChunks[0];
// const queryParamValue = queryParamChunks[1];
if (queryParams.length) {
const [queryParamKey, ...queryParamChunks] = queryParams.split('=');
const queryParamValue = queryParamChunks.join('=');

switch (queryParamKey) {
case 'otp':
transformation = transformOtp;
break;
case 'json':
transformation = (json: string) => transformJsonPath(json, queryParamValue);
break;
default:
throw new InvalidDashlanePathError();
}
Expand Down
13 changes: 13 additions & 0 deletions src/utils/secretTransformation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { JSONPath } from 'jsonpath-plus';
import { authenticator } from 'otplib';

export const transformOtp = (secret: string) => {
return authenticator.generate(secret);
};

export const transformJsonPath = (json: string, path: string) => {
const result = JSONPath<unknown>({ path, json: JSON.parse(json) as object, wrap: false });
if (result === undefined) {
throw new Error(`No matching json path found for "${path}"`);
}

if (typeof result === 'string') {
return result;
}
return JSON.stringify(result);
};
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ __metadata:
husky: ^8.0.3
inquirer: ^8.2.5
inquirer-search-list: ^1.2.6
jsonpath-plus: ^7.2.0
mocha: ^10.2.0
otplib: ^12.0.1
pkg: ^5.8.1
Expand Down Expand Up @@ -3432,6 +3433,13 @@ __metadata:
languageName: node
linkType: hard

"jsonpath-plus@npm:^7.2.0":
version: 7.2.0
resolution: "jsonpath-plus@npm:7.2.0"
checksum: 05f447339d29be861e307d6e812aec1b9b88a3ba6bba286966a4e8bed3e752bee3d715eabfc21dce968be85ccb48bf79d2c1af78da7b9b74cd1b446d4d5d02f5
languageName: node
linkType: hard

"keyv@npm:^4.0.0":
version: 4.5.3
resolution: "keyv@npm:4.5.3"
Expand Down

0 comments on commit 7f7e3bb

Please sign in to comment.