Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JWT Verify, Decode and Sign Operators #348

Merged
merged 2 commits into from
Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 4 additions & 1 deletion src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@
"Derive EVP key",
"Bcrypt",
"Scrypt",
"Pseudo-Random Number Generator"
"Pseudo-Random Number Generator",
"JWT Sign",
"JWT Verify",
"JWT Decode"
]
},
{
Expand Down
46 changes: 46 additions & 0 deletions src/core/operations/JWTDecode.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @author gchq77703 []
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/

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

/**
* 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 without checking whether the provided secret / private key is valid.";
this.infoURL = "https://jwt.io";
this.inputType = "string";
this.outputType = "JSON";
this.args = [
];
}

/**
* @param {string} input
* @param {Object[]} args
* @returns {JSON}
*/
run(input, args) {
try {
return jwt.decode(input);
} catch (err) {
return err;
}
}

}

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

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

/**
* 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.";
this.infoURL = "https://jwt.io/";
this.inputType = "JSON";
this.outputType = "string";
this.args = [
{
name: "Private / Secret Key",
type: "text",
value: "secret_cat"
},
{
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;
return jwt.sign(input, key, { algorithm: algorithm === "None" ? "none" : algorithm });
}

}

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

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

/**
* 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.";
this.infoURL = "https://jwt.io/";
this.inputType = "string";
this.outputType = "JSON";
this.args = [
{
name: "Private / Secret Key",
type: "text",
value: "secret_cat"
},
];
}

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

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

}

export default JWTVerify;
3 changes: 3 additions & 0 deletions test/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ import "./tests/operations/SetUnion";
import "./tests/operations/SymmetricDifference";
import "./tests/operations/TranslateDateTimeFormat";
import "./tests/operations/Magic";
import "./tests/operations/JWTSign";
import "./tests/operations/JWTDecode";
import "./tests/operations/JWTVerify";

let allTestsPassing = true;
const testStatusCounts = {
Expand Down
51 changes: 51 additions & 0 deletions test/tests/operations/JWTDecode.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* JWT Decode tests
*
* @author gchq77703 []
*
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import TestRegister from "../../TestRegister";

const outputObject = JSON.stringify({
String: "SomeString",
Number: 42,
iat: 1
});

TestRegister.addTests([
{
name: "JSON Decode: HS",
input: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJTdHJpbmciOiJTb21lU3RyaW5nIiwiTnVtYmVyIjo0MiwiaWF0IjoxfQ.0ha6-j4FwvEIKPVZ-hf3S_R9Hy_UtXzq4dnedXcUrXk",
expectedOutput: outputObject,
recipeConfig: [
{
op: "JWT Decode",
args: [],
}
],
},
{
name: "JSON Decode: RS",
input: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJTdHJpbmciOiJTb21lU3RyaW5nIiwiTnVtYmVyIjo0MiwiaWF0IjoxfQ.MjEJhtZk2nXzigi24piMzANmrj3mILHJcDl0xOjl5a8EgdKVL1oaMEjTkMQp5RA8YrqeRBFaX-BGGCKOXn5zPY1DJwWsBUyN9C-wGR2Qye0eogH_3b4M9EW00TPCUPXm2rx8URFj7Wg9VlsmrGzLV2oKkPgkVxuFSxnpO3yjn1Y",
expectedOutput: outputObject,
recipeConfig: [
{
op: "JWT Decode",
args: [],
}
],
},
{
name: "JSON Decode: ES",
input: "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJTdHJpbmciOiJTb21lU3RyaW5nIiwiTnVtYmVyIjo0MiwiaWF0IjoxfQ.WkECT51jSfpRkcpQ4x0h5Dwe7CFBI6u6Et2gWp91HC7mpN_qCFadRpsvJLtKubm6cJTLa68xtei0YrDD8fxIUA",
expectedOutput: outputObject,
recipeConfig: [
{
op: "JWT Decode",
args: [],
}
],
}
]);
Loading