Skip to content

Commit

Permalink
TS - add jwtparse function
Browse files Browse the repository at this point in the history
  • Loading branch information
elisherer committed Aug 28, 2024
1 parent b53e98f commit 0288ad5
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, test } from "vitest";
import { assertTransformation } from "../BaseTransformationTest";
import { BigDecimal } from "../../functions/common/FunctionHelpers";

describe("TransformerFunctionJsonPatch", () => {
test("add", async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, test } from "vitest";
import { assertTransformation } from "../BaseTransformationTest";
import { BigDecimal } from "../../functions/common/FunctionHelpers";

describe("TransformerFunctionJsonPath", () => {
test("yoDawg", async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, test } from "vitest";
import { assertTransformation } from "../BaseTransformationTest";
import { BigDecimal } from "../../functions/common/FunctionHelpers";

describe("TransformerFunctionJsonPointer", () => {
test("get", async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { describe, test } from "vitest";
import { assertTransformation } from "../BaseTransformationTest";

describe("TransformerFunctionJwtParse", () => {
test("inline", async () => {
const testJWT =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";
await assertTransformation(testJWT, "$$jwtparse:$", {
sub: "1234567890",
name: "John Doe",
iat: 1516239022,
});
//invalid
await assertTransformation(true, "$$jwtparse:$", null);
});

test("object", async () => {
const testJWT =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";
await assertTransformation(
testJWT,
{
$$jwtparse: "$",
},
{
sub: "1234567890",
name: "John Doe",
iat: 1516239022,
},
);
//invalid
await assertTransformation(
true,
{
$$jwtparse: "$",
},
null,
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import TransformerFunction from "./common/TransformerFunction";
import { ArgType } from "./common/ArgType";
import { FunctionDescription } from "./common/FunctionDescription";
import FunctionContext from "./common/FunctionContext";
import Base64 from "./utils/Base64";
import TextEncoding from "./common/TextEncoding";

const DESCRIPTION: FunctionDescription = {
aliases: ["jwtparse"],
inputType: ArgType.String,
description: "",
outputType: ArgType.Any,
};
class TransformerFunctionJwtParse extends TransformerFunction {
constructor() {
super(DESCRIPTION);
}

override async apply(context: FunctionContext): Promise<any> {
const jwt = await context.getString(null);
try {
if (jwt === null) {
return null;
}
const dot1 = jwt.indexOf(".");
if (dot1 == -1) {
throw new Error("Invalid serialized JWT object: Missing part delimiters");
}
const dot2 = jwt.indexOf(".", dot1 + 1);
if (dot2 == -1) {
throw new Error("Invalid serialized KWT object: Missing second delimiter");
}
const encodedClaimsString = jwt.substring(dot1 + 1, dot2);
const claimsString = TextEncoding.decode(Base64.decode(encodedClaimsString, "url"));
if (claimsString.startsWith("{") && claimsString.endsWith("}")) {
return JSON.parse(claimsString);
} else {
return claimsString;
}
} catch (e: any) {
console.warn(`${context.getAlias()} - Failed parsing JWT`, e);
return null;
}
}
}

export default TransformerFunctionJwtParse;
3 changes: 2 additions & 1 deletion javascript/json-transform/src/transformerFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import TransformerFunctionJsonParse from "./functions/TransformerFunctionJsonPar
import TransformerFunctionJsonPatch from "./functions/TransformerFunctionJsonPatch";
import TransformerFunctionJsonPath from "./functions/TransformerFunctionJsonPath";
import TransformerFunctionJsonPointer from "./functions/TransformerFunctionJsonPointer";
import TransformerFunctionJwtParse from "./functions/TransformerFunctionJwtParse";
import TransformerFunctionLower from "./functions/TransformerFunctionLower";
import TransformerFunctionTest from "./functions/TransformerFunctionTest";
import TransformerFunctionUpper from "./functions/TransformerFunctionUpper";
Expand Down Expand Up @@ -99,7 +100,7 @@ export class TransformerFunctions {
jsonpatch: new TransformerFunctionJsonPatch(),
jsonpath: new TransformerFunctionJsonPath(),
jsonpointer: new TransformerFunctionJsonPointer(),
jwtparse: new TransformerFunction(UNIMPLEMENTED), // TODO: new TransformerFunctionJwtParse(),
jwtparse: new TransformerFunctionJwtParse(),
length: new TransformerFunction(UNIMPLEMENTED), // TODO: new TransformerFunctionLength(),
long: new TransformerFunction(UNIMPLEMENTED), // TODO: new TransformerFunctionLong(),
lookup: new TransformerFunction(UNIMPLEMENTED), // TODO: new TransformerFunctionLookup(),
Expand Down

0 comments on commit 0288ad5

Please sign in to comment.