CyberChef/src/core/operations/JWTVerify.mjs

66 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-08-27 00:16:13 +02:00
/**
* @author gchq77703 []
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Operation from "../Operation";
import jwt from "jsonwebtoken";
2018-08-31 15:58:06 +02:00
import OperationError from "../errors/OperationError";
2018-08-27 00:16:13 +02:00
/**
* JWT Verify operation
*/
class JWTVerify extends Operation {
/**
* JWTVerify constructor
*/
constructor() {
super();
this.name = "JWT Verify";
this.module = "Crypto";
2018-08-31 15:58:06 +02:00
this.description = "Verifies that a JSON Web Token is valid and has been signed with the provided secret / private key.<br><br>The key should be either the secret for HMAC algorithms or the PEM-encoded private key for RSA and ECDSA.";
this.infoURL = "https://wikipedia.org/wiki/JSON_Web_Token";
2018-08-27 00:16:13 +02:00
this.inputType = "string";
this.outputType = "JSON";
this.args = [
{
2018-08-31 15:58:06 +02:00
name: "Private/Secret Key",
2018-08-29 23:43:10 +02:00
type: "text",
2018-08-31 15:58:06 +02:00
value: "secret"
2018-08-27 00:16:13 +02:00
},
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const [key] = args;
try {
2018-08-31 15:58:06 +02:00
const verified = jwt.verify(input, key, { algorithms: [
2018-08-29 23:43:10 +02:00
"HS256",
"HS384",
"HS512",
"none"
]});
2018-08-31 15:58:06 +02:00
if (verified.hasOwnProperty("name") && verified.name === "JsonWebTokenError") {
throw new OperationError(verified.message);
}
return verified;
2018-08-27 00:16:13 +02:00
} catch (err) {
2018-08-31 15:58:06 +02:00
throw new OperationError(err);
2018-08-27 00:16:13 +02:00
}
}
}
export default JWTVerify;