diff --git a/src/core/operations/JWTDecode.mjs b/src/core/operations/JWTDecode.mjs index b6356b5a..55941f4e 100644 --- a/src/core/operations/JWTDecode.mjs +++ b/src/core/operations/JWTDecode.mjs @@ -25,7 +25,13 @@ class JWTDecode extends Operation { this.infoURL = "https://wikipedia.org/wiki/JSON_Web_Token"; this.inputType = "string"; this.outputType = "JSON"; - this.args = []; + this.args = [ + { + name: "Include header", + type: "boolean", + value: false + } + ]; this.checks = [ { pattern: "^ey([A-Za-z0-9_-]+)\\.ey([A-Za-z0-9_-]+)\\.([A-Za-z0-9_-]+)$", @@ -42,12 +48,17 @@ class JWTDecode extends Operation { */ run(input, args) { try { + const headerInclude = args[0]; + const decoded = jwt.decode(input, { json: true, complete: true }); - - return decoded.payload; + if (headerInclude) { + return {header: decoded.header, payload: decoded.payload}; + } else { + return decoded.payload; + } } catch (err) { throw new OperationError(err); } diff --git a/tests/operations/tests/JWTDecode.mjs b/tests/operations/tests/JWTDecode.mjs index 1ef47f81..4d915d6d 100644 --- a/tests/operations/tests/JWTDecode.mjs +++ b/tests/operations/tests/JWTDecode.mjs @@ -47,5 +47,68 @@ TestRegister.addTests([ args: [], } ], + }, + { + name: "JWT Decode: HS", + input: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJTdHJpbmciOiJTb21lU3RyaW5nIiwiTnVtYmVyIjo0MiwiaWF0IjoxfQ.0ha6-j4FwvEIKPVZ-hf3S_R9Hy_UtXzq4dnedXcUrXk", + expectedOutput: JSON.stringify({ + header: { + alg: "HS256", + typ: "JWT" + }, + payload: { + String: "SomeString", + Number: 42, + iat: 1 + } + }, null, 4), + recipeConfig: [ + { + op: "JWT Decode", + args: [true], + } + ], + }, + { + name: "JWT Decode: RS", + input: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJTdHJpbmciOiJTb21lU3RyaW5nIiwiTnVtYmVyIjo0MiwiaWF0IjoxfQ.MjEJhtZk2nXzigi24piMzANmrj3mILHJcDl0xOjl5a8EgdKVL1oaMEjTkMQp5RA8YrqeRBFaX-BGGCKOXn5zPY1DJwWsBUyN9C-wGR2Qye0eogH_3b4M9EW00TPCUPXm2rx8URFj7Wg9VlsmrGzLV2oKkPgkVxuFSxnpO3yjn1Y", + expectedOutput: JSON.stringify({ + header: { + alg: "RS256", + typ: "JWT" + }, + payload: { + String: "SomeString", + Number: 42, + iat: 1 + } + }, null, 4), + recipeConfig: [ + { + op: "JWT Decode", + args: [true], + } + ], + }, + { + name: "JWT Decode: ES", + input: "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJTdHJpbmciOiJTb21lU3RyaW5nIiwiTnVtYmVyIjo0MiwiaWF0IjoxfQ.WkECT51jSfpRkcpQ4x0h5Dwe7CFBI6u6Et2gWp91HC7mpN_qCFadRpsvJLtKubm6cJTLa68xtei0YrDD8fxIUA", + expectedOutput: JSON.stringify({ + header: { + alg: "ES256", + typ: "JWT" + }, + payload: { + String: "SomeString", + Number: 42, + iat: 1 + } + }, null, 4), + recipeConfig: [ + { + op: "JWT Decode", + args: [true], + } + ], } ]); diff --git a/tests/operations/tests/JWTSign.mjs b/tests/operations/tests/JWTSign.mjs index a7752138..77483785 100644 --- a/tests/operations/tests/JWTSign.mjs +++ b/tests/operations/tests/JWTSign.mjs @@ -92,7 +92,7 @@ TestRegister.addTests([ }, { op: "JWT Decode", - args: [] + args: [false] } ], }, @@ -107,7 +107,7 @@ TestRegister.addTests([ }, { op: "JWT Decode", - args: [] + args: [false] } ], }, @@ -122,7 +122,7 @@ TestRegister.addTests([ }, { op: "JWT Decode", - args: [] + args: [false] } ], }, @@ -137,7 +137,7 @@ TestRegister.addTests([ }, { op: "JWT Decode", - args: [] + args: [false] } ], }, @@ -152,7 +152,7 @@ TestRegister.addTests([ }, { op: "JWT Decode", - args: [] + args: [false] } ], }, @@ -167,7 +167,7 @@ TestRegister.addTests([ }, { op: "JWT Decode", - args: [] + args: [false] } ], }