Skip to content

Commit

Permalink
Tidied up JWT operations
Browse files Browse the repository at this point in the history
  • Loading branch information
n1474335 committed Aug 31, 2018
1 parent 100b097 commit be14d56
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 51 deletions.
6 changes: 3 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,13 @@ module.exports = function (grunt) {
"mkdir -p src/core/config/modules",
"echo 'export default {};\n' > src/core/config/modules/OpModules.mjs",
"echo '[]\n' > src/core/config/OperationConfig.json",
"node --experimental-modules src/core/config/scripts/generateOpsIndex.mjs",
"node --experimental-modules src/core/config/scripts/generateConfig.mjs",
"node --experimental-modules --no-warnings --no-deprecation src/core/config/scripts/generateOpsIndex.mjs",
"node --experimental-modules --no-warnings --no-deprecation src/core/config/scripts/generateConfig.mjs",
"echo '--- Config scripts finished. ---\n'"
].join(";")
},
tests: {
command: "node --experimental-modules test/index.mjs"
command: "node --experimental-modules --no-warnings --no-deprecation test/index.mjs"
}
},
});
Expand Down
89 changes: 83 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@
"Derive EVP key",
"Bcrypt",
"Scrypt",
"Pseudo-Random Number Generator",
"JWT Sign",
"JWT Verify",
"JWT Decode"
"JWT Decode",
"Pseudo-Random Number Generator"
]
},
{
Expand Down
17 changes: 11 additions & 6 deletions src/core/operations/JWTDecode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import Operation from "../Operation";
import jwt from "jsonwebtoken";
import OperationError from "../errors/OperationError";

/**
* JWT Decode operation
Expand All @@ -20,12 +21,11 @@ class JWTDecode extends Operation {

this.name = "JWT Decode";
this.module = "Crypto";
this.description = "Decodes a JSON Web Token without checking whether the provided secret / private key is valid.";
this.infoURL = "https://jwt.io";
this.description = "Decodes a JSON Web Token <b>without</b> checking whether the provided secret / private key is valid. Use 'JWT Verify' to check if the signature is valid as well.";
this.infoURL = "https://wikipedia.org/wiki/JSON_Web_Token";
this.inputType = "string";
this.outputType = "JSON";
this.args = [
];
this.args = [];
}

/**
Expand All @@ -35,9 +35,14 @@ class JWTDecode extends Operation {
*/
run(input, args) {
try {
return jwt.decode(input);
const decoded = jwt.decode(input, {
json: true,
complete: true
});

return decoded.payload;
} catch (err) {
return err;
throw new OperationError(err);
}
}

Expand Down
22 changes: 16 additions & 6 deletions src/core/operations/JWTSign.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import Operation from "../Operation";
import jwt from "jsonwebtoken";
import OperationError from "../errors/OperationError";

/**
* JWT Sign operation
Expand All @@ -20,18 +21,18 @@ class JWTSign extends Operation {

this.name = "JWT Sign";
this.module = "Crypto";
this.description = "Signs a JSON object as a JSON Web Token using a provided secret / private key.";
this.infoURL = "https://jwt.io/";
this.description = "Signs a JSON object as a JSON Web Token using a 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";
this.inputType = "JSON";
this.outputType = "string";
this.args = [
{
name: "Private / Secret Key",
name: "Private/Secret Key",
type: "text",
value: "secret_cat"
value: "secret"
},
{
name: "Signing Algorithm",
name: "Signing algorithm",
type: "option",
value: [
"HS256",
Expand All @@ -56,7 +57,16 @@ class JWTSign extends Operation {
*/
run(input, args) {
const [key, algorithm] = args;
return jwt.sign(input, key, { algorithm: algorithm === "None" ? "none" : algorithm });

try {
return jwt.sign(input, key, {
algorithm: algorithm === "None" ? "none" : algorithm
});
} catch (err) {
throw new OperationError(`Error: Have you entered the key correctly? The key should be either the secret for HMAC algorithms or the PEM-encoded private key for RSA and ECDSA.
${err}`);
}
}

}
Expand Down
19 changes: 13 additions & 6 deletions src/core/operations/JWTVerify.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import Operation from "../Operation";
import jwt from "jsonwebtoken";
import OperationError from "../errors/OperationError";

/**
* JWT Verify operation
Expand All @@ -20,15 +21,15 @@ class JWTVerify extends Operation {

this.name = "JWT Verify";
this.module = "Crypto";
this.description = "Verifies that a JSON Web Token is valid and has been signed with the provided secret / private key.";
this.infoURL = "https://jwt.io/";
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";
this.inputType = "string";
this.outputType = "JSON";
this.args = [
{
name: "Private / Secret Key",
name: "Private/Secret Key",
type: "text",
value: "secret_cat"
value: "secret"
},
];
}
Expand All @@ -42,14 +43,20 @@ class JWTVerify extends Operation {
const [key] = args;

try {
return jwt.verify(input, key, { algorithms: [
const verified = jwt.verify(input, key, { algorithms: [
"HS256",
"HS384",
"HS512",
"none"
]});

if (verified.hasOwnProperty("name") && verified.name === "JsonWebTokenError") {
throw new OperationError(verified.message);
}

return verified;
} catch (err) {
return err;
throw new OperationError(err);
}
}

Expand Down
8 changes: 4 additions & 4 deletions test/tests/operations/JWTDecode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ const outputObject = JSON.stringify({
String: "SomeString",
Number: 42,
iat: 1
});
}, null, 4);

TestRegister.addTests([
{
name: "JSON Decode: HS",
name: "JWT Decode: HS",
input: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJTdHJpbmciOiJTb21lU3RyaW5nIiwiTnVtYmVyIjo0MiwiaWF0IjoxfQ.0ha6-j4FwvEIKPVZ-hf3S_R9Hy_UtXzq4dnedXcUrXk",
expectedOutput: outputObject,
recipeConfig: [
Expand All @@ -27,7 +27,7 @@ TestRegister.addTests([
],
},
{
name: "JSON Decode: RS",
name: "JWT Decode: RS",
input: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJTdHJpbmciOiJTb21lU3RyaW5nIiwiTnVtYmVyIjo0MiwiaWF0IjoxfQ.MjEJhtZk2nXzigi24piMzANmrj3mILHJcDl0xOjl5a8EgdKVL1oaMEjTkMQp5RA8YrqeRBFaX-BGGCKOXn5zPY1DJwWsBUyN9C-wGR2Qye0eogH_3b4M9EW00TPCUPXm2rx8URFj7Wg9VlsmrGzLV2oKkPgkVxuFSxnpO3yjn1Y",
expectedOutput: outputObject,
recipeConfig: [
Expand All @@ -38,7 +38,7 @@ TestRegister.addTests([
],
},
{
name: "JSON Decode: ES",
name: "JWT Decode: ES",
input: "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJTdHJpbmciOiJTb21lU3RyaW5nIiwiTnVtYmVyIjo0MiwiaWF0IjoxfQ.WkECT51jSfpRkcpQ4x0h5Dwe7CFBI6u6Et2gWp91HC7mpN_qCFadRpsvJLtKubm6cJTLa68xtei0YrDD8fxIUA",
expectedOutput: outputObject,
recipeConfig: [
Expand Down
Loading

0 comments on commit be14d56

Please sign in to comment.