This commit is contained in:
Daniel Weber 2025-05-19 21:09:55 +02:00 committed by GitHub
commit b2b27a6f94
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 44 additions and 4 deletions

View file

@ -7,6 +7,7 @@
import Operation from "../Operation.mjs"; import Operation from "../Operation.mjs";
import jwt from "jsonwebtoken"; import jwt from "jsonwebtoken";
import OperationError from "../errors/OperationError.mjs"; import OperationError from "../errors/OperationError.mjs";
import moment from "moment-timezone";
/** /**
* JWT Decode operation * JWT Decode operation
@ -25,7 +26,13 @@ class JWTDecode extends Operation {
this.infoURL = "https://wikipedia.org/wiki/JSON_Web_Token"; this.infoURL = "https://wikipedia.org/wiki/JSON_Web_Token";
this.inputType = "string"; this.inputType = "string";
this.outputType = "JSON"; this.outputType = "JSON";
this.args = []; this.args = [
{
"name": "Show/Hide dates",
"type": "boolean",
"value": false
}
];
this.checks = [ this.checks = [
{ {
pattern: "^ey([A-Za-z0-9_-]+)\\.ey([A-Za-z0-9_-]+)\\.([A-Za-z0-9_-]+)$", 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) { run(input, args) {
try { try {
const formatAsDate = args[0];
const decoded = jwt.decode(input, { const decoded = jwt.decode(input, {
json: true, json: true,
complete: 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; return decoded.payload;
} catch (err) { } catch (err) {
throw new OperationError(err); throw new OperationError(err);

View file

@ -14,7 +14,24 @@ const outputObject = JSON.stringify({
iat: 1 iat: 1
}, null, 4); }, null, 4);
const formattedIatOutputObject = JSON.stringify({
String: "SomeString",
Number: 42,
iat: "1 (Thu 1 January 1970 00:00:01 UTC)"
}, null, 4);
TestRegister.addTests([ 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", name: "JWT Decode: HS",
input: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJTdHJpbmciOiJTb21lU3RyaW5nIiwiTnVtYmVyIjo0MiwiaWF0IjoxfQ.0ha6-j4FwvEIKPVZ-hf3S_R9Hy_UtXzq4dnedXcUrXk", input: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJTdHJpbmciOiJTb21lU3RyaW5nIiwiTnVtYmVyIjo0MiwiaWF0IjoxfQ.0ha6-j4FwvEIKPVZ-hf3S_R9Hy_UtXzq4dnedXcUrXk",
@ -22,7 +39,7 @@ TestRegister.addTests([
recipeConfig: [ recipeConfig: [
{ {
op: "JWT Decode", op: "JWT Decode",
args: [], args: [false],
} }
], ],
}, },
@ -33,7 +50,7 @@ TestRegister.addTests([
recipeConfig: [ recipeConfig: [
{ {
op: "JWT Decode", op: "JWT Decode",
args: [], args: [false],
} }
], ],
}, },
@ -44,7 +61,7 @@ TestRegister.addTests([
recipeConfig: [ recipeConfig: [
{ {
op: "JWT Decode", op: "JWT Decode",
args: [], args: [false],
} }
], ],
} }