Skip to content

Commit

Permalink
Merge branch 'GCHQ77703-jwt'
Browse files Browse the repository at this point in the history
  • Loading branch information
n1474335 committed Aug 31, 2018
2 parents 3470dd9 + be14d56 commit 5dde1c1
Show file tree
Hide file tree
Showing 11 changed files with 573 additions and 9 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.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"jsbn": "^1.1.0",
"jsesc": "^2.5.1",
"jsonpath": "^1.0.0",
"jsonwebtoken": "^8.3.0",
"jsrsasign": "8.0.12",
"kbpgp": "^2.0.77",
"lodash": "^4.17.10",
Expand Down
3 changes: 3 additions & 0 deletions src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
"Derive EVP key",
"Bcrypt",
"Scrypt",
"JWT Sign",
"JWT Verify",
"JWT Decode",
"Pseudo-Random Number Generator"
]
},
Expand Down
51 changes: 51 additions & 0 deletions src/core/operations/JWTDecode.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @author gchq77703 []
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/

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

/**
* JWT Decode operation
*/
class JWTDecode extends Operation {

/**
* JWTDecode constructor
*/
constructor() {
super();

this.name = "JWT Decode";
this.module = "Crypto";
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 = [];
}

/**
* @param {string} input
* @param {Object[]} args
* @returns {JSON}
*/
run(input, args) {
try {
const decoded = jwt.decode(input, {
json: true,
complete: true
});

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

}

export default JWTDecode;
74 changes: 74 additions & 0 deletions src/core/operations/JWTSign.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* @author gchq77703 []
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/

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

/**
* JWT Sign operation
*/
class JWTSign extends Operation {

/**
* JWTSign constructor
*/
constructor() {
super();

this.name = "JWT Sign";
this.module = "Crypto";
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",
type: "text",
value: "secret"
},
{
name: "Signing algorithm",
type: "option",
value: [
"HS256",
"HS384",
"HS512",
"RS256",
"RS384",
"RS512",
"ES256",
"ES384",
"ES512",
"None"
]
}
];
}

/**
* @param {JSON} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const [key, algorithm] = args;

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}`);
}
}

}

export default JWTSign;
65 changes: 65 additions & 0 deletions src/core/operations/JWTVerify.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @author gchq77703 []
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/

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

/**
* JWT Verify operation
*/
class JWTVerify extends Operation {

/**
* JWTVerify constructor
*/
constructor() {
super();

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.<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",
type: "text",
value: "secret"
},
];
}

/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const [key] = args;

try {
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) {
throw new OperationError(err);
}
}

}

export default JWTVerify;
4 changes: 4 additions & 0 deletions test/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ import "./tests/operations/HaversineDistance";
import "./tests/operations/Hexdump";
import "./tests/operations/Image";
import "./tests/operations/Jump";
import "./tests/operations/JWTDecode";
import "./tests/operations/JWTSign";
import "./tests/operations/JWTVerify";
import "./tests/operations/MS";
import "./tests/operations/Magic";
import "./tests/operations/MorseCode";
Expand All @@ -67,6 +70,7 @@ import "./tests/operations/StrUtils";
import "./tests/operations/SymmetricDifference";
import "./tests/operations/ToGeohash.mjs";
import "./tests/operations/TranslateDateTimeFormat";
import "./tests/operations/Magic";

let allTestsPassing = true;
const testStatusCounts = {
Expand Down
Loading

0 comments on commit 5dde1c1

Please sign in to comment.