mirror of
https://github.com/gchq/CyberChef.git
synced 2025-06-14 10:14:53 -04:00
Add possibility to show/hide human readable dates based on given timestamps of jwt token.
This commit is contained in:
parent
c57556f49f
commit
291a278dfa
2 changed files with 44 additions and 4 deletions
|
@ -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);
|
||||
|
|
|
@ -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],
|
||||
}
|
||||
],
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue