-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
89 additions
and
4 deletions.
There are no files selected for viewing
1 change: 0 additions & 1 deletion
1
javascript/json-transform/src/__tests__/functions/TransformerFunctionJsonPatch.test.ts
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
1 change: 0 additions & 1 deletion
1
javascript/json-transform/src/__tests__/functions/TransformerFunctionJsonPath.test.ts
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
1 change: 0 additions & 1 deletion
1
javascript/json-transform/src/__tests__/functions/TransformerFunctionJsonPointer.test.ts
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
40 changes: 40 additions & 0 deletions
40
javascript/json-transform/src/__tests__/functions/TransformerFunctionJwtParse.test.ts
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,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, | ||
); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
javascript/json-transform/src/functions/TransformerFunctionJwtParse.ts
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,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; |
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