From 291a278dfa9947fa138e3fae926319c62502e798 Mon Sep 17 00:00:00 2001 From: Daniel Weber Date: Mon, 19 May 2025 20:57:20 +0200 Subject: [PATCH] Add possibility to show/hide human readable dates based on given timestamps of jwt token. --- src/core/operations/JWTDecode.mjs | 25 ++++++++++++++++++++++++- tests/operations/tests/JWTDecode.mjs | 23 ++++++++++++++++++++--- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/core/operations/JWTDecode.mjs b/src/core/operations/JWTDecode.mjs index b6356b5a..95fb5396 100644 --- a/src/core/operations/JWTDecode.mjs +++ b/src/core/operations/JWTDecode.mjs @@ -7,6 +7,7 @@ import Operation from "../Operation.mjs"; import jwt from "jsonwebtoken"; import OperationError from "../errors/OperationError.mjs"; +import moment from "moment-timezone"; /** * JWT Decode operation @@ -25,7 +26,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": "Show/Hide dates", + "type": "boolean", + "value": false + } + ]; this.checks = [ { pattern: "^ey([A-Za-z0-9_-]+)\\.ey([A-Za-z0-9_-]+)\\.([A-Za-z0-9_-]+)$", @@ -42,11 +49,27 @@ class JWTDecode extends Operation { */ run(input, args) { try { + const formatAsDate = args[0]; const decoded = jwt.decode(input, { json: true, complete: true }); + if (formatAsDate) { + const payload = {...decoded.payload}; + if (payload.iat) { + payload.iat = payload.iat + " ("+moment.unix(payload.iat).tz("UTC").format("ddd D MMMM YYYY HH:mm:ss") + " UTC)"; + } + if (payload.exp) { + payload.exp = payload.exp + " ("+moment.unix(payload.exp).tz("UTC").format("ddd D MMMM YYYY HH:mm:ss") + " UTC)"; + } + if (payload.nbf) { + payload.nbf = payload.nbf + " ("+moment.unix(payload.nbf).tz("UTC").format("ddd D MMMM YYYY HH:mm:ss") + " UTC)"; + } + + return payload; + } + 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..1bc742e2 100644 --- a/tests/operations/tests/JWTDecode.mjs +++ b/tests/operations/tests/JWTDecode.mjs @@ -14,7 +14,24 @@ const outputObject = JSON.stringify({ iat: 1 }, null, 4); +const formattedIatOutputObject = JSON.stringify({ + String: "SomeString", + Number: 42, + iat: "1 (Thu 1 January 1970 00:00:01 UTC)" +}, null, 4); + TestRegister.addTests([ + { + name: "JWT Decode: HS with formatted iat", + input: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJTdHJpbmciOiJTb21lU3RyaW5nIiwiTnVtYmVyIjo0MiwiaWF0IjoxfQ.0ha6-j4FwvEIKPVZ-hf3S_R9Hy_UtXzq4dnedXcUrXk", + expectedOutput: formattedIatOutputObject, + recipeConfig: [ + { + op: "JWT Decode", + args: [true], + } + ], + }, { name: "JWT Decode: HS", input: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJTdHJpbmciOiJTb21lU3RyaW5nIiwiTnVtYmVyIjo0MiwiaWF0IjoxfQ.0ha6-j4FwvEIKPVZ-hf3S_R9Hy_UtXzq4dnedXcUrXk", @@ -22,7 +39,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "JWT Decode", - args: [], + args: [false], } ], }, @@ -33,7 +50,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "JWT Decode", - args: [], + args: [false], } ], }, @@ -44,7 +61,7 @@ TestRegister.addTests([ recipeConfig: [ { op: "JWT Decode", - args: [], + args: [false], } ], }